求大神帮看看程序哪里有问题(数据结构括号匹配),程序能运行,但是不管输入什么都显示匹配?
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
const int STACK_INIT_SIZE=100;
const int STACKINCREMENT=10;
typedef char SElemType;
typedef struct
{
SElemType *elem;
int top;
int stacksize;
int increment;
}SqStack;
// 初始化栈
void InitStack(SqStack&S,int maxsize=STACK_INIT_SIZE,int incresize=STACKINCREMENT)
{
S.elem=new SElemType[STACK_INIT_SIZE];
S.top=-1;
S.stacksize=maxsize;
S.increment=incresize;
}
//入栈
void Push(SqStack&S,SElemType e)
{
if(S.top==S.stacksize)
{
S.stacksize=S.stacksize+STACKINCREMENT;
SElemType *elem=new SElemType[S.stacksize];
for(int i=0;i<=S.top;i++)
elem[i]=S.elem[i];
delete []S.elem;
S.elem=elem;
}
S.elem[++S.top]=e;
}
//检查空栈
bool StackEmpty(SqStack S)
{
if(S.top==-1)
return true;
else return false;
}
//取栈顶元素
bool GetTop(SqStack S,SElemType &e)
{
if(S.top==-1) return false;
e=S.elem[S.top];
return true;
}
//让栈顶元素出栈
bool Pop(SqStack &S,SElemType&e)
{
if(S.top==-1) return false;
e=S.elem[S.top];
S.top--;
return true;
}
//括号匹配
bool matching(char exp[])
{
SqStack S;
InitStack(S);
int state=1;
char e;
char ch=*exp++;
while(ch != '#' &&state)
{switch(ch)
{
switch (ch) {
case'(':
case'[':
case'{':
{
Push(S, ch);
break;
}
case')':
{
if (!StackEmpty(S) && GetTop(S,e)== '(')
Pop(S, e);
else state = 0;
break;}
case']':
{
if (!StackEmpty(S) && GetTop(S,e) == '[')
Pop(S, e);
else state = 0;
break;}
case'}':
{
if (!StackEmpty(S) && GetTop(S,e) == '{')
Pop(S, e);
else state = 0;
break;}
}
default:break;
}
ch=*exp++;
}
if(state&&StackEmpty(S))
return true;
else return false;
}
//主函数
int main ()
{
int max=50;//数组容量
char a[max];
cout<<"输入括号表达式(以“#”结尾)"<<endl;
cin>>a;
if(matching(a))
{
cout<<"括号匹配"<<endl;
cout<<"\n";
}
else cout<<"括号不匹配"<<endl;
}