括号匹配问题
#include <stdio.h>#include <ctype.h>
#include <io.h>
#include <stdlib.h>
#include <process.h>
#include <malloc.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int SElemType;
typedef int Status;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}Sqstack;
Status Initstack(Sqstack *S)
{
S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base)exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
Status Destroystack(Sqstack *S)
{
free(S->base);
S->base=NULL;
S->top=0;
S->stacksize=0;
return OK;
}
Status Gettop(Sqstack S,SElemType *e)
{
if(S.top==S.base) return ERROR;
e=S.top-1;
return OK;
}
Status Push(Sqstack *S,SElemType e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S->base)exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
Status pop(Sqstack *S,SElemType *e)
{
if(S->top==S->base) return ERROR;
e=--S->top;
return OK;
}
int main(void)
{
Sqstack S;
int e,j=0,b=0,c=0;
char a;
Initstack(&S);
scanf("%s",&a);
Gettop(S,&e);
switch(a)
{
case'(':Push(&S,e); ++j; break;
case'{':Push(&S,e); ++b; break;
case'[':Push(&S,e); ++c; break;
case')':pop(&S,&e); --j; break;
case'}':pop(&S,&e); --b; break;
case']':pop(&S,&e); --c; break;
default: break;
}
if(j>=1||b>=1||c>=1)
{
printf("Extra left brackets\n");
}
else if(j<=-1||b<=-1||c<=-1)
{
printf("Extra right brackets\n");
}
else if(j==0&&b==0&&c==0)
{
printf("Brackets match\n");
}
else
{
printf("Brackets not match\n");
}
Destroystack(&S);
return 0;
}问题就是输出结果永远是Extra left brackets,可能是判断条件的问题有没有大佬解决一下咧