-
Notifications
You must be signed in to change notification settings - Fork 9
/
栈的应用_括号匹配_似乎有问题.cpp
146 lines (128 loc) · 1.88 KB
/
栈的应用_括号匹配_似乎有问题.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<stdio.h>
#include<stdlib.h>
#define Max 100
#define False -1
#define True 1
typedef struct Stack
{
char data[Max];
int top;
}stack;
int init_stack(stack &s)
{
s.top=-1;
return 0;
}
char push_stack(stack &s,char e)
{
if(s.top>Max-1)
{
return False;
}
s.data[++s.top]=e;
//printf("pop-->%c\n",e);
return True;
}
char pop_stack(stack &s,char e)
{
if(s.top>-1)
{
e=s.data[s.top--];
//printf("pop--%c ",e);
return e;
}
return False;
}
char top_stack(stack s)
{
if(s.top>-1)
{
return s.data[s.top];
}
return False;
}
char print_stack(stack s,char e)
{
while(s.top!=-1)
{
e=s.data[s.top--];
//printf("push-->%c\n",e);
}
return True;
}
int len_stack(stack s)
{
int m=0;
while(s.top!=-1)
{
s.data[s.top--];
//printf("push-->%c\n",s.data[s.top--]);
m++;
}
return m;
}
int empty_stack(stack s)
{
if(s.top==-1)
{
return NULL;
}
return True;
}
int main()
{
stack s;
init_stack(s);
int i,j;
char c,e,d;
while((c=getchar())!='\n')
{
if(c=='(' || c=='[')
{
push_stack(s,c);
}
else if (c==')' || c==']')
{
//printf("---------------\n ");
e=pop_stack(s,e);
printf("%c ",e);
if (top_stack(s)==')' || top_stack(s)==']')
{
pop_stack(s,e);
}
}
else if (c=='+' || c=='-'|| c=='*'|| c=='/')
{
e=top_stack(s);
if(e=='*' || e=='/')
{
e=pop_stack(s,e);
printf("%c ",e);
}
push_stack(s,c);
}
else if (c>=48 && c<=57)
{
printf("%c ",c);
}
else
{
e=pop_stack(s,e);
printf("%c ",e);
}
}
while(empty_stack(s)!=NULL)
{
if (top_stack(s)=='(' || top_stack(s)=='[')
{
e=pop_stack(s,e);
//printf("%c ",e);
}
else
{
e=pop_stack(s,e);
printf("%c ",e);
}
}
return 0;
}