#include
#include
#include
#define Chushi 100
#define Zengliang 10
#define SElemType int
#define ERROR 0
#define OK 1
typedef struct
{
SElemType base;
SElemType top;
char data[100];
int StackSize;
}SqStack;
void JianStack(SqStack *S)
{
S->top=0;
S->base=0;
S->StackSize = Chushi;
}
int Push(SqStack *S,char x)
{
if(S->top-S->base>=S->StackSize-1)
{
S->StackSize+=Zengliang;
}
S->data[S->top]=x;
S->top++;
return OK;
}
int GetTop(SqStack *S,char e)
{
if(S->top==S->base) return ERROR;
else
{
e = S->data[S->top-1];
return OK;
}
}
int DeleteTop(SqStack *S,char d)
{
if(S->base==S->top) return ERROR;
else
{
d=S->data[S->top-1];
S->top--;
return OK;
}
}
int main(void)
{
SqStack S;
int count=0,i,j;
char kuohao[10],e='1',x,d;
JianStack(&S);
printf("Please input bracket?:");
scanf("%s",&kuohao);
do
{
count++;
}while(kuohao[count]!='\0');
printf("%d",&count);
for(j=0;j<count;j++)
{
printf("%s",kuohao[j]);
}
for(i=0;i<count;i++)
{
if(i==count-1)
{
if(kuohao[i]=='('||kuohao[i]=='[')
{
i=i-1;
printf("ERROR!");
break;
}
}
if(kuohao[i]=='(')
{
GetTop(&S,e);
if(e==')') DeleteTop(&S,d);
else
{
x = kuohao[i];
Push(&S,x);
}
}
else if(kuohao[i]=='[')
{
GetTop(&S,e);
if(e==']') DeleteTop(&S,d);
else
{
x = kuohao[i];
Push(&S,x);
}
}
else if(kuohao[i]==')')
{
GetTop(&S,e);
if(e=='(') DeleteTop(&S,d);
else
{
printf("ERROR!");
break;
}
}
else if(kuohao[i]==']')
{
GetTop(&S,e);
if(e=='[') DeleteTop(&S,d);
else
{
printf("ERROR!");
break;
}
}
}
if(i+1==count)printf("YES!");
else printf("NO!");
}