1 #include<stdio.h>
2 #include<stdlib.h>
3 #define STACKMAX 100
4 typedef struct {
5 char *base;
6 char *top;
7 int stcaksize;
8 }Seqstack;
9 void creat(Seqstack &s){
10 s.base = (char*)malloc(STACKMAX*sizeof(char));
11 if(!s.base) {
12 printf("分配失败\n"); exit(1);
13 }
14 else printf("分配成功\n");
15 s.top = s.base;
16 s.stcaksize = STACKMAX;
17 }
18 void puch(Seqstack &s,char e)
19 {
20 if(s.top-s.base<s.stcaksize){
21 *s.top++ = e;
22 }
23 }
24 int pop(Seqstack &s,char &e){
25 if(s.base==s.top){
26 return 0;
27 }
28 else{
29 e = *--s.top;
30 }
31 return 1;
32 }
33 int main()
34 {
35 Seqstack s;
36 creat(s);
37 char str[100];
38 int i = 0;
39 gets(str);
40 while(str[i]!='\0')
41 {
42 char e;
43 switch(str[i])
44 {
45 case '(':
46 case '{':
47 case '[': puch(s,str[i]); break;
48 case ')':if(pop(s,e)){
49 if(e=='(')break;
50 else printf("不匹配\n"); return 0;
51 }
52 else printf("不匹配\n"); return 0;
53 case '}':if(pop(s,e)){
54 if(e=='{')break;
55 else printf("不匹配\n"); return 0;
56 }
57 else printf("不匹配\n"); return 0;
58 case ']':
59 if(pop(s,e)){
60 if(e=='[')break;
61 else printf("不匹配\n"); return 0;
62 }
63 else printf("不匹配\n"); return 0;
64 }
65 i++;
66 }
67 printf("匹配\n");
68 return 0;
69 }