PTA 7-2 符號配對
阿新 • • 發佈:2018-10-03
-- exit ble pre type 代碼 malloc mes 期末考試
直接用棧模擬即可,數組可做,但因為這節數據結構是棧,為了期末考試還是手寫一下棧的操作,值得註意的是,這道題用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; 20typedef 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 符號配對