给定一串字符,不超过100个字符,可能包括括号、数字、字母、标点符号、空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配。
输入格式:
输入在一行中给出一行字符串,不超过100个字符,可能包括括号、数字、字母、标点符号、空格。
输出格式:
如果括号配对,输出yes,否则输出no。
输入样例1:
sin(10+20)
输出样例1:
yes
输入样例2:
{[}]
输出样例2:
no
**不知道为什么第三个测试点总是通不过……试了一晚上,有大神知道怎么改吗?
**
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[101],check[101];
int i,top=-1,flag=1,flag2=0;
char temp;
scanf("%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]=='('||str[i]=='['||str[i]=='{')
{ flag2++;
top++;
check[top]=str[i];
}
else if(str[i]==']'||str[i]==')'||str[i]=='}')
{ flag2++;
if(top==-1)
{flag=0;break;}
temp=check[top];top--;
if ((str[i] == ')'&& temp !='(' )|| (str[i] == ']'&& temp != '[') || (str[i] == '}'&& temp!= '{'))
{flag=0;break;}
}
}
if(flag2==0)
{printf("no");return 0;}
if(flag==0||top!=-1)
printf("no");
else
printf("yes");
return 0;
}