forked from Nexengineer/DSA-PREP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackOperations.cpp
368 lines (276 loc) · 4.81 KB
/
StackOperations.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include<iostream>
using namespace std;
#define MAX 50
#include<strings.h>
#include<sstream>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
class stack
{
public:
int top;
string str[MAX];
stack()
{
top=-1;
}
bool isEmpty()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if(top==MAX)
{
return true;
}
else
{
return false;
}
}
void push(string item)
{
if(isFull())
{
cout<<"Stack is overflow";
}
else
{
top++;
str[top]=item;
}
}
string pop()
{
string a = "";
if(isEmpty())
{
cout<<"Stack underflow";
return a;
}
a=str[top];
top--;
return a;
}
};
class expression
{
public:
string infix;
string postfix;
stack s; //object for stack class
void infix_to_postfix();
void postfix_to_prefix();
void eval_postfix();
bool isOperator(string s)
{
if(s == "+" || s == "-" || s == "*" || s == "/" || s == "^" )
{
return true;
}
else
{
return false;
}
}
int precedence(string op)
{
if(op == "+" || op == "-")
{
return 1;
}
else if (op == "*" || op == "/")
{
return 2;
}
else if(op == "^")
{
return 3;
}
else
{
return 0;
}
}
bool isOperand(string s)
{
if(s >= "A" && s <= "Z")
{
return true;
}
else if(s >= "a" && s <= "z")
{
return true;
}
else if(s >= "0" && s <= "9")
{
return true;
}
else
{
return false;
}
}
};
void expression :: infix_to_postfix()
{
cout<<"\nEnter infix expression: ";
cin>>infix;
for(int i=0;i<infix.length();i++)
{
if(isOperand(string(1,infix[i])))
{
postfix=postfix+infix[i];
}
else if(infix[i]=='(')
{
s.push("(");
}
else if(infix[i] == ')')
{
while(s.top!=-1 && s.str[s.top]!= "(")
{
string t= s.str[s.top];
s.pop();
postfix = postfix+t;
}
if(s.str[s.top] == "(" )
{
s.pop();
}
}
else
{
while(s.top!=-1 && (precedence(string(1,infix[i])) <= precedence(s.str[s.top])))
{
string t= s.str[s.top];
s.pop();
postfix = postfix+t;
}
s.push(string(1,infix[i]));
}
}
while(s.top!=-1)
{
string t= s.str[s.top];
s.pop();
postfix = postfix+t;
}
cout<<"\nPostfix expression is: "<<postfix;
}
void expression :: postfix_to_prefix()
{
string post;
string pref;
cout<<"\nEnter postfix expression: ";
cin>>post;
for(int i=0;i<post.length();i++)
{
if(isOperator(string(1,post[i])))
{
string op1 = s.pop();
string op2 = s.pop();
string t = post[i]+op2+op1;
s.push(t);
}
else
{
s.push(string(1,post[i]));
}
}
pref=s.pop();
cout<<"\nPrefix expression is: "<<pref;
}
void expression :: eval_postfix()
{
int ans,t1,t2;
string s1,s2;
string post1;
cout<<"\nEnter postfix Expression: ";
cin>>post1;
for(int i=0;i<post1.length();i++)
{
if(isOperator(string(1,post1[i])))
{
s2=s.pop();
s1=s.pop();
stringstream change_2(s2);
stringstream change_1(s1);
change_2 >> t2;
change_1 >> t1;
if(post1[i] == '+')
{
ans = t1+t2;
}
if(post1[i] == '-')
{
ans = t1-t2;
}
if(post1[i] == '*')
{
ans = t1*t2;
}
if(post1[i] == '/')
{
ans = t1/t2;
}
if(post1[i] == '^')
{
ans = pow(t1,t2);
}
ostringstream ans_1;
ans_1 << ans;
string ans_2 = ans_1.str();
s.push(ans_2);
}
else
{
if(isdigit(post1[i]))
{
s.push(string(1,post1[i]));
}
}
}
string result = s.pop();
cout<<"Postfix answer is: "<<result;
}
int main()
{
int ch,val;
char start='y';
stack s1;
expression exp;
while(start=='y')
{
cout<<"\n1)Infix to postfix\n2)Postfix to prefix\n3)Evaluate postfix\n";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
exp.infix_to_postfix();
break;
case 2:
exp.postfix_to_prefix();
break;
case 3:
exp.eval_postfix();
break;
default:
cout<<"\nIncorrect choice !!";
}
cout<<"\n\nDo you want to continue (y/n): ";
cin>>start;
}
cout<<"\nThankyou !!!";
return 0;
}