#include
#include
#define overflow -1
#define ok 1
#define error 0
#define maxsize 100
#define stackincrement 10
typedef char SElemType;
typedef int Status;
typedef struct
{
SElemType *base;
SElemType *top;
int StackSize;
}SqStack;
Status InitStack(SqStack *S)
{
S->base=(SElemType *)malloc(maxsize*sizeof(SElemType));
if(!S->base)
exit(overflow);
S->top=S->base;
S->StackSize=maxsize;
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->StackSize+=stackincrement;
}
*(S->top)=e;
(S->top)++;
return ok;
}
Status Pop(SqStack S,SElemType *e)
{
if(S->top==S->base)
return error;
*e=(--S->top);
return ok;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base)
return ok;
else
return error;
}
void match(char *ch)
{
SqStack S;
char *p;
char e;
InitStack(&S);
p=ch;
while(p&&(*p!='\0'))
{
switch (*p)
{
case'(':{Push(&S,*p);p++;printf("左括号(进栈成功\n");break;}
case'[':{Push(&S,*p);p++;printf("左括号[进栈成功\n");break;}
case'{':{Push(&S,*p);p++;printf("左括号}进栈成功\n");break;}
case')':
{
if(!StackEmpty(S))
{
getTop(S,&e);
if(e=='(')
{ printf("括号)匹配成功!\n");Pop(&S,&e);p++;}
else
{ printf("括号)不匹配!\n");Pop(&S,&e);p++; }
}
else
{printf("右括号)是多余的!\n");Pop(&S,&e);p++;
break;
};
case']':
{
if(!StackEmpty(S))
{
getTop(S,&e);
if(e=='[')
{ printf("括号]匹配成功!\n");Pop(&S,&e);p++;}
else
{ printf("括号]不匹配!\n");Pop(&S,&e);p++; }
}
else
{printf("右括号]是多余的!\n");Pop(&S,&e);p++;}
case'}':
{
if(!StackEmpty(S))
{
getTop(S,&e);
if(e=='{')
{ printf("括号}匹配成功!\n");Pop(&S,&e);p++;}
else
{ printf("括号}不匹配!\n");Pop(&S,&e);p++; }
}
else
{printf("右括号}是多余的!\n");Pop(&S,&e);p++;}
break;
};
default:p++;
}
}
if(StackEmpty(S))
{
printf("匹配正确");
}
else
{
printf("匹配失败");
}
}
int main()
{
while(1)
{
char ch[50];
printf("请输入要进行判断的括号字符串:");
gets(ch);
match(ch);
}
}
e:\vs2012\data\shiyan\shiyan\shiyan.c(137): error C2143: 语法错误 : 缺少“;”(在“类型”的前面)