#include<stdio.h> #include<stdlib.h> #define N 20 typedef struct { char data[N]; int top; }stack; stack *L; void initstack(stack *S) { S->top=-1; } int Isemptystack(stack *S) { return S->top==-1; } int Isfullstack(stack *S) { return S->top==N-1; } void pushstack(stack *S,char ch) { if(Isfullstack(S)) { printf("overflow!!!"); exit(0); } S->data[++S->top]=ch; } void popstack(stack *S) { if(Isemptystack(S)) { printf("the stack is empty!!"); exit(0); } S->top--; } void ismatch(char a[N],stack *S) { int i; for(i=0;i<N;i++) { if(a[i]=='(') { if(S->data[S->top]==')') {printf("the wrong input format!\n"); exit(0); } pushstack(S,a[i]); } else if(a[i]==')') { if(Isemptystack(S)) { printf("the wrong input format!\n"); exit(0); } popstack(S); } } if(Isemptystack(S)) printf("success for match!\n"); else printf("cannot match\n"); } main() { char array[N]; initstack(L); printf("please input the expression(the lenghth<N):\n"); scanf("%s",array); ismatch(array,L); }

评论