-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionConverter.cpp
484 lines (436 loc) · 13.7 KB
/
ExpressionConverter.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <math.h>
using namespace std;
#define MAX 50
template<class T>
class stack{
private:
int top = -1;
T arr[MAX];
public:
void push(T t){
if( isFull() ){
cout << "Stack is full!" << endl;
}
arr[++top] = t;
}
T pop(){
if( isEmpty() ) {
cout << " Stack is empty! " << endl;
}
return arr[top--];
}
bool isEmpty() {
if( top < 0) {
return true;
}
return false;
}
bool isFull() {
if( top > MAX) {
return true;
}
return false;
}
T topOfStack() {
return arr[top];
}
};
class stack2{
private:
string arr[MAX];
int top = -1;
public:
void push(string t){
arr[++top] = t;
}
string pop(){
if( isEmpty() ) {
cout << " Stack is empty! " << endl;
}
return arr[top--];
}
bool isEmpty() {
if( top < 0) {
return true;
}
return false;
}
string topOfStack() {
return arr[top];
}
};
int precedence(char symbol) {
switch (symbol) {
case '^':
return 3;
case '/':
case '*':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
void selectExpression(string& expression, vector<string>& expressions){
for(int i=0;i<expressions.size();i++){
cout<<i+1<<"."<<expressions[i]<<endl;
}
int x;
cin>>x;
if(x > expressions.size() || x == 0){
cout<<"Enter valid number!"<<endl;
}
else{
expression = expressions[x-1];
}
}
bool isOperator(char x) {
if(x == '+' || x == '-' || x == '*' || x == '/' || x == '^') {
return true;
}
return false;
}
bool isOperand(char ch){
if(isalpha(ch) || isdigit(ch)){
return true;
}
return false;
}
class Postfix {
protected:
string infixExpression;
stack<char> s;
stack<float> p;
void map(vector<float>& m) {
for(int i=0; i<infixExpression.length(); i++){
char ch = infixExpression[i];
int x;
if( isOperand(ch) ){
cout<<"Enter value of "<<ch<<": ";
cin>>x;
m[ch - 'a'] = x;
}
}
}
public:
Postfix(string e){
infixExpression = e;
}
string convertToPostfix(){
string ans;
int j = 0;
char symbol, next;
for (int i = 0; i < infixExpression.length(); i++) {
symbol = infixExpression[i];
if (!isspace(symbol)) {
switch (symbol) {
case '(':
s.push(symbol);
break;
case ')':
while ((next = s.pop()) != '(')
ans += next;
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while (!s.isEmpty() && precedence(s.topOfStack()) >= precedence(symbol))
ans += s.pop();
s.push(symbol);
break;
default:
ans += symbol;
}
}
}
while (!s.isEmpty())
ans += s.pop();
return ans;
}
float evaluatePostfix(string& e) {
vector<float> m(26, 0);
map(m);
for(int i=0; i<e.length(); i++) {
char ch = e[i];
if( isOperator(ch) ){
int a = p.pop();
int b = p.pop();
if(ch == '+') {
p.push(a+b);
}
else if(ch == '-') {
p.push(a-b);
}
else if(ch == '*') {
p.push(a*b);
}
else if(ch == '/') {
p.push(a/b);
}
else if(ch == '^') {
p.push(pow(a, b));
}
}
else if( isOperand(ch) ) {
p.push(m[ch - 'a']);
}
}
return p.pop();
}
string postfixToInfix(string postfixExpression) {
stack2 p;
for(int i=0;i<postfixExpression.length();i++){
char ch = postfixExpression[i];
if(isOperand(ch)){
string str(1, ch);
p.push(str);
}
else if( isOperator(ch)){
string s2 = p.pop(), s1 = p.pop();
string s3 = "(" + s1 + ch + s2 + ")";
p.push(s3);
}
}
string answer = p.pop();
return answer;
}
~Postfix(){}
};
class Prefix: protected Postfix{
private:
void changePar(string& str){
for(int i=0;i<str.length();i++){
if(str[i] == '('){
str[i]=')';
}
else if(str[i] == ')'){
str[i]='(';
}
}
}
void reverse(string& str){
int i=0, j=str.length()-1;
while(i<=j){
swap(str[i], str[j]);
i++;
j--;
}
}
public:
Prefix(string e): Postfix(e){}
string convertToPrefix(){
string ans;
reverse(infixExpression);
changePar(infixExpression);
ans = convertToPostfix();
reverse(ans);
return ans;
}
float evaluatePrefix(string& e){
vector<float> m(26, 0);
map(m);
for(int i=e.length()-1; i>=0; i--) {
char ch = e[i];
if( isOperator(ch) ){
int a = p.pop();
int b = p.pop();
if(ch == '+') {
p.push(a+b);
}
else if(ch == '-') {
p.push(a-b);
}
else if(ch == '*') {
p.push(a*b);
}
else if(ch == '/') {
p.push(a/b);
}
else if(ch == '^') {
p.push(pow(a, b));
}
}
else if( isOperand(ch) ) {
p.push(m[ch - 'a']);
}
}
return p.pop();
}
string prefixToInfix(string prefixExpression){
reverse(prefixExpression);
string answer = postfixToInfix(prefixExpression);
reverse(answer);
changePar(answer);
return answer;
}
~Prefix(){}
};
void menu() {
cout << "********* MENU **********" << endl;
cout << "1.Enter a new expression" <<endl
<< "2.Select an expression" <<endl
<< "3.Postfix Calculations" <<endl
<< "4.Prefix Calculations" <<endl
<< "5.EXIT" << endl;
cout << "*************************" << endl;
}
bool alreadyExists(string& exp, vector<string>& expressions) {
for(int i=0;i<expressions.size();i++) {
if(exp == expressions[i]) {
return true;
}
}
return false;
}
int main() {
int x;
vector<string> infixExpressions = {"a+b","(a+b)*(c+d)"};
vector<string> prefixExpressions = {"+ab", "*+ab+cd"};
vector<string> postfixExpressions = {"ab+", "ab+cd+*"};
string infixExpression = infixExpressions[0];
string postfixExpression = postfixExpressions[0];
string prefixExpression = prefixExpressions[0];
do {
menu();
cin >> x;
switch (x) {
case 1: {
string exp;
cout << "Enter a new expression: ";
cin.ignore();
getline(cin, exp);
if(alreadyExists(exp, infixExpressions)) {
cout<<"Expression already exists!" <<endl;
}
else{
infixExpression = exp;
infixExpressions.push_back(exp);
}
break;
}
case 2: {
selectExpression(infixExpression, infixExpressions);
break;
}
case 3: {
if(infixExpression == ""){
cout << "WARNING: No expression selected! Select an expression first!" <<endl;
break;
}
int q;
cout<<"****** POSTFIX MENU *****" << endl
<<"1.Convert to postfix" << endl
<< "2.Evaluate postfix expression" << endl
<< "3.Postfix to infix conversion" << endl
<<"*************************" << endl;
cin>>q;
Postfix p1(infixExpression);
string answer = p1.convertToPostfix();
postfixExpressions.push_back(answer);
postfixExpression = answer;
switch(q) {
case 1: {
cout<<"Performing postfix operation on expression: "<<infixExpression<<endl;
cout << "Postfix expression: " << answer << endl;
break;
}
case 2: {
cout<<"Evaluating postfix expression: "<<answer<<endl;
float x = p1.evaluatePostfix(answer);
cout << "Evaluation answer: " << x <<endl;
break;
}
case 3: {
int z;
cout<< "****************************" <<endl;
cout<<"1. Convert expression :" << postfixExpression << endl;
cout<<"2. Select other postfix expression " <<endl;
cout<< "****************************" <<endl;
cin>>z;
switch(z) {
case 1: {
cout<<"Infix Expression: " << p1.postfixToInfix(postfixExpression) <<endl;
break;
}
case 2: {
selectExpression(postfixExpression, postfixExpressions);
cout<<"Converting postfix expression: " << postfixExpression << endl;
cout<<"Infix Expression: " << p1.postfixToInfix(postfixExpression) <<endl;
break;
}
}
break;
}
}
break;
}
case 4: {
if(infixExpression == ""){
cout << "WARNING: No expression selected! Select an expression first!" <<endl;
break;
}
int q;
cout<<"****** PREFIX MENU ******" << endl
<<"1.Convert to prefix" << endl
<< "2.Evaluate prefix expression" << endl
<< "3.Prefix to infix conversion" << endl
<<"*************************" << endl;
cin>>q;
Prefix p2(infixExpression);
string answer = p2.convertToPrefix();
prefixExpressions.push_back(answer);
prefixExpression = answer;
switch(q) {
case 1: {
cout<<"Performing prefix operation on expression: "<<infixExpression<<endl;
cout << "Prefix expression: " << answer << endl;
break;
}
case 2: {
cout<<"Evaluating prefix expression: "<<answer<<endl;
int x = p2.evaluatePrefix(answer);
cout << "Evaluation answer: " << x <<endl;
break;
}
case 3: {
int z;
cout<< "****************************" <<endl;
cout << "1. Convert expression: "<< prefixExpression << endl;
cout << "2. Select new prefix expression: " << endl;
cout<< "****************************" <<endl;
cin>>z;
switch(z) {
case 1: {
cout<<"Infix Expression: " << p2.prefixToInfix(prefixExpression) <<endl;
break;
}
case 2: {
selectExpression(prefixExpression, prefixExpressions);
cout<<"Converting prefix expression: " << prefixExpression << endl;
cout<<"Infix Expression: " << p2.prefixToInfix(prefixExpression) <<endl;
break;
}
}
break;
}
}
break;
}
default: {
if( x != 5){
cout << "Invalid choice!" <<endl;
break;
}
}
}
} while (x != 5);
return 0;
}