代码运行结果永远是正确,弄了好久没解决
#include<stdio.h>
#define MaxSize 50
typedef struct//顺序栈
{
char data[MaxSize];
int top;
}SqStack;
bool StackEmpty(SqStack &S)//判断栈空
{
if(S.top==-1)
return true;
else
return false;
}
bool Push(SqStack &S,char x)//压栈
{
if(S.top==MaxSize)
return false;
S.data[++S.top]=x;
return true;
}
bool Pop(SqStack &S,char x)//出栈
{
if(S.top==-1)
return false;
x=S.data[S.top--];
return true;
}
bool GetTop(SqStack &S,char x)//读取栈顶元素
{
if(S.top==-1)
return false;
x=S.data[S.top];
return true;
}
void InitStack(SqStack &S)//栈的初始化
{
S.top=-1;
}
void main()
{
int i,x;
SqStack S;
char a[20];
InitStack(S);
for(i=0;i<20;i++)
{
scanf("%c",&a[i]);
}
for(i=0;i<20;i++)
{
if(a[i]=='{'||a[i]=='['||a[i]=='(')
Push(S,a[i]);
if(a[i]=='}'||a[i]==']'||a[i]==')')
if(StackEmpty(S))
{
printf("右括号比左括号多");
return;
}
else
{
GetTop(S,x);
if(x=='{'&&a[i]=='}'||x=='['&&a[i]==']'||x=='('&&a[i]==')')
{
Pop(S,x);
Pop(S,x);
continue;
}
else
{
printf("左右括号配对次序不正确");
return;
}
}
}
if(StackEmpty(S))
{
printf("左右括号配对正确");
return;
}
else
{
printf("左括号比右括号多");
return;
}
}