#define OK 1 #define ERROR 0 #define ARRAY_SIZE 50
void bracketsMaych(char a[]) { char e; e = a[0]; //构建一个空栈,用于存放左括号 SqStack S; initSqStack(&S); int i = 0, flag = 1; while (e != '\0' && flag) { //简单遍历传入的字符数组 //若为左括号则入栈 //若为右括号则出栈,并检验是否匹配 //匹配成功flag = 1,否则flag = 0 switch (e) { case '(': pushSqStack(&S, e); break; case '[': pushSqStack(&S, e); break; case '{': pushSqStack(&S, e); break; case ')': if (!emptyOfSqStack(&S)) { e = popSqStack(&S); if (e != '(') { flag = 0; } }else flag = 0; break; case ']': if (!emptyOfSqStack(&S)) { e = popSqStack(&S); if (e != '[') { flag = 0; } }else flag = 0; break; case '}': if (!emptyOfSqStack(&S)) { e = popSqStack(&S); if (e != '{') { flag = 0; } }else flag = 0; break; default: break; } i++; e = a[i]; } if (!emptyOfSqStack(&S)) { flag = 0; } if (flag) { printf("YES"); }else printf("NO"); }
int main() { char brackets[ARRAY_SIZE]; char e; int i = 0; do { scanf("%c", &e); brackets[i] = e; i++; } while (e != '\n'); bracketsMaych(brackets); return 0; }