#include<stdio.h>
#define MaxSize 10
typedef char ElemType;
typedef struct{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S)
{
S.top = -1;
}
bool Push(SqStack &S,ElemType x)
{
if(S.top==MaxSize-1)
return false;
S.data[++S.top] = x;
return true;
}
bool Pop(SqStack &S,ElemType &x)
{
if(S.top == -1)
return false;
x = S.data[S.top--];
return true;
}
char getTop(SqStack S)
{
if(S.top == -1)
return false;
return S.data[S.top];
}
bool judgeSign(SqStack S,char *arr,int len)
{
int i=0;
ElemType x;
while(i<len)
{
switch(arr[i]){
case '[':
Push(S,arr[i]);break;
case '(':
Push(S,arr[i]);break;
case ']':
if(getTop(S)!='[')
{
printf("括号不匹配\n");
return false;
}
else
Pop(S,x);
break;
case ')':
if(getTop(S)!='(')
{
printf("括号不匹配\n");
return false;
}
else
Pop(S,x);
}
++i;
}
printf("括号匹配!\n");
return true;
}
void main()
{
SqStack S;
InitStack(S);
char arr[5] = {'[','(',']',']',']'};
judgeSign(S,arr,5);
}