括弧匹配检验
#include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<algorithm> #include<string> char a[10001] = {0}, s[256] = {0}; int main() { int i, n, t = 0; bool f; gets(s); for(int j = 0;j <= strlen(s)-1;j++) { if(s[j] == '(' || s[j] == '[') a[++t] = s[j]; // 左括号进栈 if(s[j] == ')') { if(a[t] == '(') t--; else t++; } if(s[j] == ']') { if(a[t] == '[') t--; else t++; } // 不匹配就出栈 } if(t == 0) printf("OK"); // 栈空,则匹配成功 else printf("Wrong"); // 栈未空,则匹配失败 return 0; }