#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define INFEASIBLE -1
#define ERROR 0
#define OK 1
typedef char SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int InitStack(SqStack* sq)
{
sq->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!sq->base)exit(OVERFLOW);
sq->top=sq->base;
sq->stacksize=STACK_INIT_SIZE;
return OK;
}
int GetTop(SqStack* sq,SElemType* e)
{
if(sq->base==sq->top)return ERROR;
*e=*(sq->top-1);
return OK;
} //GetTop
int Push(SqStack* sq,SElemType e)
{
if(sq->top-sq->base>=sq->stacksize)
{
sq->base=(SElemType*)realloc(sq->base,(sq->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!sq->base)exit(OVERFLOW);
sq->top=sq->base+sq->stacksize;
sq->stacksize+=STACKINCREMENT;
}
*sq->top++=e;
return OK;
}//Push
int Pop(SqStack* sq,SElemType* e)
{
if(sq->base==sq->top)return ERROR;
*e=*--sq->top;
return OK;
}//Pop
int Clear(SqStack* sq,SElemType e)
{
sq->top=sq->base;
return OK;
}//Clear
int StackEmpty(SqStack* sq)
{
if(sq->base==sq->top)return OK;
else return ERROR;
}//StackEmpty
int Match(SElemType ch,SElemType str)
{
if(ch=='('&&str==')')return OK;
else if(ch=='['&&str==']')return OK;
else if(ch=='{'&&str=='}')return OK;
else return ERROR;
}
void BracketMatch(SElemType *str)
{
SqStack S;
int i;
char ch;
InitStack(&S);
for(i=0;str[i]!='#';i++)
{
switch(str[i])
{
case '(':
case '[':
case '{':
Push(&S,str[i]);
break;
case ')':
case ']':
case '}':
if(StackEmpty(&S))
{
printf("/n右括号多余!/n");return;
}
else
{
GetTop(&S,&ch);
if(Match(ch,str[i]))Pop(&S,&ch);
else
{
printf("/n左右括号不匹配!/n");return;
}
}//else
}//switch
}//for
if(StackEmpty(&S))
printf("/n匹配成功!/n");
else
printf("/n左括号多余!/n");
}//BracketMatch
void main(){//输入以'#'结束 int i; char ch; char str[100]; gets(str); BracketMatch(&str[0]);}