由于比较简单,不需要栈实现,只需要记录三种左括号的状态,然后依次匹配加减即可。
#include<stdio.h>
#include<string.h>
int main(int argc, char){
char s[200];
gets(s);
int len = strlen(s);
int state[3] = {0,0,0};
for(int i=0; i<len; i++){
if(s[i]=='(')
state[0]++;
else if(s[i]=='[')
state[1]++;
else if(s[i]=='{')
state[2]++;
else if(s[i]==')')
state[0]--;
else if(s[i]==']')
state[1]--;
else if(s[i]=='}')
state[2]--;
}
for(int i=0; i< 3; i++){
if(state[i]){
printf("wrong");
return 0;
}
}
printf("right");
return 0;
}