给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。
Input
输入数据有多组,每组数据不超过100个字符并含有( ,) ,[, ],{, }一个或多个。处理到文件结束。
Output
如果匹配就输出“yes”,不匹配输出“no”
Sample Input
sin(20+10)
{[}]
Sample Output
yes
no
代码如下:
#include<stdio.h>
#include<string.h>
char a[1010],b[1010];
int main()
{
while(gets(a))
{
int top=0,i,j,l;
l=strlen(a);
for(i=0;i<l;i++)
{
if(a[i]!='('&&a[i]!=')'&&a[i]!='{'&&a[i]!='}'&&a[i]!='['&&a[i]!=']')
continue;
if(top)
{
//b数组从零开始,所以才会有b[top-1];
//代表出栈
if(b[top-1]=='('&&a[i]==')')
top--;
else if(b[top-1]=='['&&a[i]==']')
top--;
else if(b[top-1]=='{'&&a[i]=='}')
top--;
else b[top++]=a[i];
}
//>>>>>>>应该从这里开始就是先else再if
else
b[top++]=a[i];
}
if(!top)
printf("yes\n");
else printf("no\n");
}
return 0;、
}