#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW 1
typedef int Status;
typedef char SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//顺序栈的初始化
Status InitStack(SqStack *S) {
S->base=(SElemType*)malloc(sizeof(SElemType)*MAXSIZE);
if(S->base==NULL){
return ERROR;
}
S->top=S->base;
S->stacksize=MAXSIZE;
return OK;
}
//入栈
Status Push(SqStack *S,int e){
if(S->top-S->base==S->stacksize)
return ERROR;
*(S->top)=e;
(S->top)++;
return OK;
}
//出栈
Status Pop(SqStack *S,SElemType *e){
if(S->top-S->base==S->stacksize)
return ERROR;
--(S->top);
*e=*(S->top);
return OK;
}
//判断是否为空
Status StackEmpty(SqStack S){
if(S.top==S.base)return OK;
else return ERROR;
}
int main() {
printf("请输入括号:");
char a[100];
gets(a);
SqStack S;
InitStack(&S);
int i;
char e;
for(i=0;i<strlen(a);i++){
if(a[i]=='('||a[i]=='['||a[i]=='{'){
Push(&S,a[i]);//若ch为"左括号"就存入栈内;
}
e=*(S.top-1);//栈顶元素!与左括号匹配
if(a[i]==')'||a[i]==']'||a[i]=='}'){
if(a[i]==')'&&e=='('||a[i]=='}'&&e=='{'||a[i]==']'&&e=='[')
Pop(&S,e);//匹配成功就出栈
if((!(a[i]==')'&&e=='('))&&(!(a[i]=='}'&&e=='{'))&&(!(a[i]==']'&&e=='['))){
printf("Expect left");return 0;//未匹配成功
}
}
}
if(StackEmpty(S))printf("Great");
else printf("Expect right");
return 0;
}