{}[]()的匹配问题,哪里错了,高手看下
#include <stdio.h>#include <stdlib.h>
#define SIZE 100
struct stack{
char *top;
char *base;
int stacksize;};
init (struct stack *s)
{s->base=(char*)malloc(SIZE *sizeof(char) );
if(!s->base){
printf ("创建失败");
exit(0);
}
s->top=s->base;
s->stacksize=SIZE;
return 1;}
pop (stack *s,char*e)
{
if(s->top=s->base) {
printf("匹配失败");
exit(0);
}
*e=*--s->top;
return 1;
}
push (struct stack*s,char e)
{if(s->top-s->base>=s->stacksize)
{printf("空间已满");
exit(0);}
*s->top++=e;
return 1;
}
void main (){
struct stack s;
init(&s);
char ch,a,e;
printf("请输入计算式,以回车结束\n");
while((ch=getchar())!='\n')
{if(ch=='('||ch=='['||ch=='{')
push(&s,ch);
if(ch==')'||ch==']'||ch=='}')
{
a=pop(&s,&e);
if(a=='[')
a=']';
if (a=='(')
a=')';
if (a=='{')
a='}';
if(a!=ch){
printf("匹配失败");
exit(0);}
}
}
if(s.top=s.base)printf("计算式中符号匹配");
else
printf("匹配失败");
}