-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack_InfixToPostfix.cpp
228 lines (206 loc) · 4.68 KB
/
Stack_InfixToPostfix.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <iostream>
using namespace std;
class inpo {
char in[20], po[20], stk[10];
int i, j, is, ic, top, top1;
int stk1[10];
public:
inpo() {
i = j = is = ic = 0;
top = top1 = -1;
}
bool IsOperand(char C) {
if (C >= '0' && C <= '9') return true;
if (C >= 'a' && C <= 'z') return true;
if (C >= 'A' && C <= 'Z') return true;
return false;
}
void getinfix() {
cout << "\nEnter valid infix expression: ";
cin >> in;
}
void showinfix() {
cout << "\t" << in;
}
int isempty() {
if (top == -1)
return 1;
else
return 0;
}
int isfull() {
if (top == 9)
return 1;
else
return 0;
}
void push1(int x1) {
top = top + 1;
stk1[top] = x1;
}
int pop1() {
int s1;
s1 = stk1[top];
top = top - 1;
return s1;
}
void push(char x) {
top = top + 1;
stk[top] = x;
}
char pop() {
char s;
s = stk[top];
top = top - 1;
return s;
}
void showpostfix() {
cout << "\t" << po;
}
void convert();
int instackprio();
int incomingprio(char);
void postfixExpEval();
};
void inpo::postfixExpEval() {
i = 0;
char ch;
int op1, op2, res, tot;
while (po[i] != '\0') {
ch = po[i];
if ((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/') ||
(ch == '^')) {
switch (ch) {
case '+':
op2 = pop1();
op1 = pop1();
res = op1 + op2;
push1(res);
break;
case '-':
op2 = pop1();
op1 = pop1();
res = op1 - op2;
push1(res);
break;
case '*':
op2 = pop1();
op1 = pop1();
res = op1 * op2;
push1(res);
break;
case '/':
op2 = pop1();
op1 = pop1();
res = op1 / op2;
push1(res);
break;
case '^':
op2 = pop1();
op1 = pop1();
res = op1;
while (op2 > 1) {
res = res * op1;
op2--;
}
push1(res);
break;
}
} else if (IsOperand(ch)) {
push1(ch - '0');
}
i = i + 1;
}
tot = pop1();
cout << "\nResult is:" << tot;
}
void inpo::convert() {
i = j = 0;
char p, k;
while (in[i] != '\0') {
p = in[i];
if ((p == '(') || (p == '+') || (p == '-') || (p == '*') || (p == '/') || (p == '^') || (p == ')')) {
if (isempty()) {
push(p);
} else if (p ==
')') {
k = pop();
while (k != '(') {
po[j] = k;
j++;
k = pop();
}
} else {
is = instackprio();
ic = incomingprio(p);
if (is > ic) {
k = pop();
po[j] = k;
j++;
push(p);
} else {
push(p);
}
}
} else {
po[j] = p;
j++;
}
i = i + 1;
}
if (in[i] == '\0') {
while (!isempty()) {
k = pop();
po[j] = k;
j++;
}
}
po[j] = '\0';
}
int inpo::instackprio() {
char b;
b = stk[top];
switch (b) {
case '(':
return 0;
case '+':
return 2;
case '-':
return 2;
case '*':
return 4;
case '/':
return 4;
case '^':
return 5;
}
return 0;
}
int inpo::incomingprio(char ch) {
switch (ch) {
case '(':
return 9;
case '+':
return 1;
case '-':
return 1;
case '*':
return 3;
case '/':
return 3;
case '^':
return 6;
}
return 0;
}
int main() {
inpo p1;
p1.getinfix();
p1.showinfix();
cout << "\nAfter conversion from infix to postfix: ";
p1.convert();
p1.showpostfix();
cout << "\nPostfix expression is as follows: ";
p1.postfixExpEval();
return 0;
}