1 #include<cstdio>
 2 #include<cstdlib>
 3 #include <iostream>
 4 #define Maxsize 50
 5 using namespace std;
 6 typedef struct
 7 {
 8     char Data[Maxsize];
 9     int top;
10 }Sqstack;
11 bool InitStack(Sqstack& L)
12 {
13     L.top = -1;
14     return true;
15 }
16 bool Push(Sqstack& L,char Elem)
17 {
18     if (L.top == Maxsize-1)
19         return false;
20     L.Data[++L.top] = Elem;
21     return true;
22 }
23 bool Pop(Sqstack& L,char& Elem)
24 {
25     if (L.top == -1) return false;
26     Elem = L.Data[L.top--];
27     return true;
28 }
29 bool StackEmpty(Sqstack L)
30 {
31     if (L.top == -1) return true;
32     return false;
33 }
34 /***********以上为栈的相关操作*********/
35 bool bracketCheck(Sqstack& L, int len,char str[])
36 {
37     for (int i = 0; i < len; i++)
38     {
39         if (str[i] == '(' || str[i] == '{' || str[i] == '[')
40             Push(L, str[i]);
41         else {
42             char Elem;
43             if (StackEmpty(L)) return false;
44             Pop(L, Elem);
45             if (str[i] == ')' && Elem != '(') return false;
46             if (str[i] == ']' && Elem != '[') return false;
47             if (str[i] == '}' && Elem != '{') return false;
48         }
49     }
50     return StackEmpty(L);
51 }
52 int main()
53 {
54     Sqstack L;
55     char Elem = 'x';
56     InitStack(L);
57     int length = 0;
58     char str[Maxsize];
59     while (true)
60     {
61         Elem = getchar();
62         if (Elem == '\n') break;
63         if(Elem=='('||Elem==')') str[length++] = Elem;
64     }
65     if (bracketCheck(L, length, str))
66         cout << "括号匹配成功" << endl;
67     else cout << "括号匹配失败" <<endl;
68     return true;
69 }