括号匹配为什么不成功
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
#define stack_size 1000
#define stack_increment 10
typedef struct sqstack
{
char *base;
char *top;
int size;
}sqstack;
void IntiStack(sqstack *s)
{
s->base=(char *)malloc(sizeof(char)*stack_size);
if(!s->base) printf("分配内存失败!");
s->top=s->base;
s->size=stack_size;
}
void Push(sqstack *s,char c)
{
if(s->top-s->base>=stack_size)
{
s->base=(char*)realloc(s->base,(s->size+stack_increment)*sizeof(char));
if(!s->base) printf("增加空间失败!");
s->top=s->base+stack_size;
s->size+=stack_increment;
}
*s->top++=c;
}
void Pop(sqstack *s,char e)
{
if(s->top==s->base) printf("栈为空\n");
e=*--s->top;
}
int StackEmpty(sqstack *s)
{
if(s->top!=s->base) return 0;
return 1;
}
void Bracket(sqstack *s,char *str)
{
int i=0,flag1=0,flag2;
char e;
while(str[i]!='\0'){
switch(str[i]){
case '(':Push(s,'(');break; //'('进栈
case '[':Push(s,'[');break; //'['进栈
case ')':{Pop(s,e);
if(e!='(') flag1=1; break;} //出栈,判断是否为'('
case ']':{Pop(s,e);
if(e!='[') flag1=1;break;} //出栈,判断是否为'['
default: break;
}
if(flag1) break; //出现不匹配,立即结束循环
i++;
}
flag2=StackEmpty(s); //flag2判断堆栈是否为空
if(!flag1 && flag2) printf("括号匹配!\n");
else printf("括号不匹配!\n");
}
int main()
{
sqstack s;char str[100],temp;
printf("请输入字符串:\n");
scanf("%s",str);
scanf("%c",&temp);//接受输入的回车键
IntiStack(&s);
Bracket(&s,str);
return 0;
}