#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef char datatype;
typedef struct{
int top;
datatype a[MAXSIZE];
}sequence_stack ;
void init(sequence_stack *st){
st->top=0;
}
void push(sequence_stack *st,datatype x){
if(st->top==MAXSIZE){
printf("\nThe sequence stack is full!\n");exit(1);
return 1;
}
st->a[st->top]=x;
st->top++;
return 0;
}
void pop(sequence_stack *st){
if(st->top==0){
printf("This sequence stack is empty!\n");exit(1);
return 1;
}
st->top--;
return 0;
}
int empty(sequence_stack st){
return(st.top? 0:1);//1表示空,0表示非空
}
datatype read(sequence_stack st){
if(empty(st)){
printf("\n栈是空的!");exit(1);
}
return st.a[st.top-1];
}
void input(sequence_stack *st){
init(st);
datatype x;
printf("请输入一组字符串,并以#为结束符\n");
scanf("%c",&x);
while(!'#'){
st->a[st->top++]=x;
scanf("%c",&x);
}
}
int match_kuohao(char c[]){
int i=0;
sequence_stack st;
init(&st);
while(c[i]!='#'){
switch(c[i]){
case'{':
case'[':
case'(':push(&st,c[i]);break;
case'}':if(!empty(st)&&read(st)=='{'){
pop(&st);break;}
else return 0;
case']':if(!empty(st)&&read(st)=='['){
pop(&st);break;}
else return 0;
case')':if(!empty(st)&&read(st)=='('){
pop(&st);break;}
else return 0;
}
i++;
}
return(empty(st));
}
int main(int argc, char *argv[]) {
sequence_stack st;
datatype x;
init(&st);
input(&st);
int m= 7;
m=match_kuohao(&st);//空为1,1表示匹配,0表示非空,0为不匹配
printf("%d",m);
return 0;
}