码迷,mamicode.com
首页 > 其他好文 > 详细

PTA 7-2 符号配对

时间:2018-10-03 21:02:39      阅读:1152      评论:0      收藏:0      [点我收藏+]

标签:realloc   style   else   turn   stat   ||   通过   error   ring   

直接用栈模拟即可,数组可做,但因为这节数据结构是栈,为了期末考试还是手写一下栈的操作,值得注意的是,这道题用gets函数在PTA上会编译错误,用scanf("%[^\n]", str)会有一个样例无法通过,最后我使用了string读入数据,应该是我对scanf格式化读入不够了解,有知道的朋友可以评论区告诉我,非常感谢

附上代码:

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 
  6 #include <iostream>
  7 #include <string>
  8 
  9 using namespace std;
 10 
 11 //函数状态码定义
 12 #define TRUE        1
 13 #define FALSE       0
 14 #define OK          1
 15 #define ERROR       0
 16 #define INFEASIBLE -1
 17 #define OVERFLOW   -2
 18 
 19 typedef int Status;
 20 typedef char SElemType;
 21 
 22 #define STACK_INIT_SIZE 500
 23 #define STACKINCREMENT 10
 24 
 25 typedef struct {
 26     SElemType *base;
 27     SElemType *top;
 28     int stacksize;
 29 }SqStack;
 30 
 31 Status InitStack(SqStack &S) {
 32     S.base = (SElemType * )malloc(STACK_INIT_SIZE * sizeof(SElemType));
 33     if(!S.base) exit(OVERFLOW);
 34     S.top = S.base;
 35     S.stacksize = STACK_INIT_SIZE;
 36     return OK;
 37 }//InitStack
 38 
 39 Status GetTop(SqStack &S, SElemType &e) {
 40     if(S.top == S.base)
 41         return ERROR;
 42     e = *(S.top - 1);
 43     return OK;
 44 }//GetTop
 45 
 46 Status Push(SqStack &S, SElemType e) {
 47     if(S.top - S.base >= S.stacksize) {
 48         S.base = (SElemType * )realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
 49         if(!S.base)
 50             exit(OVERFLOW);
 51         S.top = S.base + S.stacksize;
 52         S.stacksize += STACKINCREMENT;
 53     }
 54     *S.top++ = e;
 55     return OK;
 56 }//Push
 57 
 58 Status Pop(SqStack &S, SElemType &e) {
 59     if(S.top == S.base)
 60         return ERROR;
 61     e = *--S.top;
 62     return OK;
 63 }//Pop
 64 
 65 char change(char ch){
 66     if(ch == ()
 67         return );
 68     if(ch == {)
 69         return };
 70     if(ch == [)
 71         return ];
 72     if(ch == a)
 73         return b;
 74     if(ch == ))
 75         return (;
 76     if(ch == })
 77         return {;
 78     if(ch == ])
 79         return [;
 80     if(ch == b)
 81         return a;
 82 };
 83 
 84 int main()
 85 {
 86     SqStack sta;
 87     InitStack(sta);
 88 
 89     char ch, ptr[200] = {0}, cnt = 0;
 90 
 91     char ans =  ; // ( -> 1, { -> 2, [ -> 3, /* -> 4
 92 
 93     string str;
 94 
 95     while(getline(cin, str)) {
 96         if(str == ".")
 97             break;
 98         for(int i = 0; i < str.size(); ++i) {
 99             if(str[i] == ( || str[i] == { || str[i] == [ || str[i] == ) || str[i] == } || str[i] == ])
100                 ptr[cnt++] = str[i];
101             else if(str[i] == / && str[i + 1] == *)
102                 ptr[cnt++] = a, ++i;
103             else if(str[i] == * && str[i + 1] == /)
104                 ptr[cnt++] = b, ++i;
105         }
106     }
107 
108     for(int i = 0; i < cnt; ++i) {
109         if(ptr[i] == ( || ptr[i] == { || ptr[i] == [ || ptr[i] == a)
110             Push(sta, ptr[i]);
111         else {
112             if(GetTop(sta, ch) == OK) {
113                 if(ptr[i] == change(ch)) {
114                     Pop(sta, ch);
115                 }
116                 else {
117                     ans = ch;
118                     break;
119                 }
120             }
121             else {
122                 ans = ptr[i];
123                 break;
124             }
125         }
126     }
127 
128     if(ans ==   && GetTop(sta, ch) == OK) {
129         ans = ch;
130     }
131 
132     if(ans ==  )
133         printf("YES");
134     else {
135         printf("NO\n");
136         if(ans == ( || ans == { || ans == [)
137             printf("%c-?",ans);
138         else if(ans == a)
139             printf("/*-?");
140         else if(ans == ) || ans == } || ans == ])
141             printf("?-%c",ans);
142         else
143             printf("?-*/");
144     }
145 
146     return 0;
147 }

 

PTA 7-2 符号配对

标签:realloc   style   else   turn   stat   ||   通过   error   ring   

原文地址:https://www.cnblogs.com/fan-jiaming/p/9738920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!