#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main(){
struct stack{//堆
char data[20];
int top;
}s;
char str[20]={'(',')',')','[','[',']','{','}','{','(',')','}',']'};
int len=strlen(str);
s.top=-1;//初始化
for (int i = 0; i < len; ++i) {
if(str[i]=='('||str[i]=='['||str[i]=='{'){
s.top++;
s.data[s.top]=str[i];
} else{
if (s.top==-1){//失败条件之一
printf("fail\n");
return 0;
}
//失败条件之二
if (s.data[s.top]=='('&&str[i]!=')'){
printf("fail\n");
return 0;
}
if (s.data[s.top]=='['&&str[i]!=']'){
printf("fail\n");
return 0;
}
if (s.data[s.top]=='{'&&str[i]!='}'){
printf("fail\n");
return 0;
}
s.top--;//匹配成功,出栈一个
}
}
if(s.top==-1)//失败条件之三
printf("success");
else
printf("fail");
return 0;
}
括号匹配问题
最新推荐文章于 2022-10-24 21:13:26 发布