-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression_Conversion.cpp
More file actions
243 lines (193 loc) · 3.86 KB
/
Expression_Conversion.cpp
File metadata and controls
243 lines (193 loc) · 3.86 KB
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
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#define MAX 10
using namespace std;
//Stack class
class Stack
{
public:
string arr[MAX];
int top;
Stack()
{
top=-1;
}
public:
int isEmpty();
void push(string temp);
string pop();
};
int Stack::isEmpty()
{
if(top==-1)
return 1;
return 0;
}
void Stack::push(string temp)
{
top++;
arr[top]=temp;
}
string Stack::pop()
{
string x;
x=arr[top];
top--;
return x;
}
//Expression class
class expression
{
public:
string infix, postfix, prefix;
Stack s;
int prec(string ch)
{
if(ch=="^")
return 3;
else if(ch=="*" || ch=="/")
return 2;
else if(ch=="+" || ch=="-")
return 1;
else
return 0;
}
bool isOperator(char ch)
{
if(ch=='^' || ch=='*' || ch=='/' || ch=='+' || ch=='-')
return true;
else
return false;
}
void infix_postfix();
void postfix_prefix();
void postfix_eval();
};
void expression::infix_postfix()
{
cout<<"\nInfix = ";
cin>>infix;
for(int i=0; infix[i]!='\0'; i++)
{
//for operands
if((infix[i]>='a' && infix[i]<='z') || (infix[i]>='A' && infix[i]<='Z'))
{
postfix=postfix+infix[i];
}
//for brackets
else if(infix[i]=='(')
s.push("(");
else if(infix[i]==')')
{
while(s.arr[s.top]!="(" && !s.isEmpty())
{
string t=s.pop();
postfix=postfix+t;
}
if(s.arr[s.top]=="(")
s.pop();
}
//for operators
else
{
while((prec(string(1, infix[i])))<=prec(s.arr[s.top]) && !s.isEmpty())
{
string t=s.pop();
postfix=postfix+t;
}
s.push(string(1, infix[i]));
}
}
while(!s.isEmpty())
{
string t=s.pop();
postfix=postfix+t;
}
cout<<"\nPostfix = "<<postfix<<endl;
}
void expression::postfix_prefix()
{
cout<<"\nPostfix = ";
cin>>postfix;
for(int i=0; i<postfix.length(); i++)
{
if(isOperator(postfix[i]) && !s.isEmpty())
{
string op2=s.pop();
string op1=s.pop();
string t=postfix[i]+op1+op2;
s.push(t);
}
else
{
s.push(string(1, postfix[i]));
}
}
cout<<"\nPrefix = "<<s.pop();
}
void expression::postfix_eval()
{
cout<<"\nPostfix = ";
cin>>postfix;
for(int i=0; postfix[i]!='\0'; i++)
{
if(isOperator(postfix[i]))
{
int op_1, op_2;
int ans;
string op2=s.pop();
string op1=s.pop();
stringstream stm1(op1);
stringstream stm2(op2);
stm1>>op_1;
stm2>>op_2;
switch(postfix[i])
{
case '+':
ans=op_1+op_2;
break;
}
stringstream stm3;
stm3<< ans ;
string ans1=stm3.str();
s.push(ans1);
}
else
{
s.push(string(1,postfix[i]));
}
}
cout<<"\nAnswer = "<<s.pop();
}
int main()
{
expression e;
int choice;
do
{
cout<<"\n----------------------------------------------------------------\n";
cout<<"1]. Infix to Postfix\n2]. Postfix to Prefix\n3]. Postfix Evaluation\n0]. Exit the Program\n";
cout<<"\nEnter the Choice - ";
cin>>choice;
switch(choice)
{
case 1:
e.infix_postfix();
break;
case 2:
e.postfix_prefix();
break;
case 3:
e.postfix_eval();
break;
case 0:
cout<<"Thank You!!";
break;
default:
cout<<"\nWrong Selection!!";
break;
}
}while(choice!=0);
return 0;
}