括号匹配问题,不知道哪错了,求大神指点!!!
# include<stdio.h># include<malloc.h>
# include<string.h>
# define Max 10000
typedef struct stack
{
char ch[Max];
int top;
}STType;
STType *STInit()
{
STType *s;
s=(STType *)malloc(sizeof(STType));
s->top=0;
return s;
}
int EmptyST(STType *s)
{
if(s->top==0)
return 1;
return 0;
}
void PushST(STType *s,char c)
{
s->ch[++s->top]=c;
}
void PopST(STType *s)
{
s->top--;
}
int main()
{
STType *s;
char *c;
int n,i;
s=STInit();
scanf("%d",&n);
while(n--)
{
scanf("%s",c);
for(i=0;i<strlen(c);i++)
{
if(c[i]=='('||c[i]=='[')
PushST(s,c[i]);
if(c[i]==')'||c[i]==']')
{
if(EmptyST(s))
{
printf("No\n");
break;
}
else if(s->ch[s->top]-c[i]==-1||s->ch[s->top]-c[i]==-2)
{
PopST(s);
}
else
{
printf("No\n");
break;
}
}
}
if(EmptyST(s))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}