括号匹配程序怎么总是不能出现正确结果
括号匹配程序怎么总是不能出现正确结果,有了堆栈的函数,高手看看,谢谢了。。#define OK 1
#define OVERFLOW -2
#define ERROR 0
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 100
typedef struct {
char *base;
char *top;
int stacksize;
}sqstack;
initstack(sqstack *s){
s->base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
if(!s->base)exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
gettop(sqstack *s,char e){
if(s->top==s->base) return ERROR;
e=*(s->top-1);
return OK;
}
push(sqstack *s,char e){
if(s->top-s->base>=s->stacksize){
s->base=(char*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(char));
if(!s->base) exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*(s->top++)=e;
return OK;
}
pop (sqstack *s ,char *e)
{if(s->top==s->base) return ERROR;
e=--(s->top);
return OK;
}
input (sqstack *s)
{char n,e;
do
{
scanf("%c",&n);
switch (n)
{case '(':push(s,n);break;
case '[':push(s,n);break;
case ']':gettop(s,e);
if(e=='[')
pop(s,&e);
else push(s,n);
break;
case ')':gettop(s,e);
if(e=='(')
pop(s,&e);
else push(s,n);
break;
}
}while(n!='0');
}
choice(sqstack *s)
{if(s->top==s->base)
printf("correct");
else printf("wrong");
}
main()
{sqstack s1;
printf("when you are input 0,the input of sign the is over:\n");
initstack(&s1);
input(&s1);
choice(&s1);
getch();
}