括号匹配,c语言来实现
#include "stdio.h"
void init(){char s[100];
int top;
char ch;
top=0;
printf("请输入符号\n");
ch=getchar();
while(ch!=EOF && ch !='\n'){
switch(ch){
case '(' :
s[top++]=ch;
break;
case '[':
s[top++]=ch;
break;
case '{' :
s[top++]=ch;
break;
case ')' :
if(top==0 || s[--top]!='(')printf("不匹配,圆括号有错误");
break;
case ']' :
if(top==0 || s[--top]!='[')
printf("不匹配,方括号有错误");
break;
case '}' :
if(top==0 || s[--top]!='{')
printf("不匹配,大方括号有错误");
break;
}
ch=getchar();
}
if(top==0) printf("匹配成功");
else printf("匹配失败");
}
void main(){
init();
}
用栈的思想完成