Repository navigation
Expand file tree
/
Copy pathinfix_postfix_prefix.cpp
More file actions
214 lines (162 loc) · 5.08 KB
/
Copy pathinfix_postfix_prefix.cpp
File metadata and controls
214 lines (162 loc) · 5.08 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int precedence(char temp){
if(temp=='^'){return 3;}
if(temp=='/' || temp == '*'){return 2;}
if(temp=='+' || temp == '-'){return 1;}
return -1;
}
bool is_char(char temp){return (temp>='a' && temp<='z');}
bool is_operator(char temp){return (temp =='%' || temp =='*' || temp =='+' || temp =='-' || temp =='/');}
// inside stack precedence order of the operators should be in increasing order from bottom to top
string infix_to_postfix(string exp){
int len = exp.length();
string ans="";
stack<char> st;
for(int i=0;i<len;i++){
if(is_char(exp[i])){ans+=exp[i];continue;}
if(exp[i] == '('){st.push(exp[i]);continue;}
if(exp[i] == ')'){
while(!st.empty() && st.top()!='('){
ans+=st.top();
st.pop();
}
if(!st.empty() && st.top()=='('){st.pop();}
continue; // * continue
}
while(!st.empty() && precedence(st.top()) >= precedence(exp[i])){
ans+=st.top();
st.pop();
}
st.push(exp[i]);
}
while(!st.empty()){
ans+=st.top();
st.pop();
}
return ans;
}
// reverse the infix expression (taking care of '(' & ')')
// get the postfix expression corresponding to the reversed infix
// reverse the postfix expression obtained and return the same
string infix_to_prefix(string infix) {
int len = infix.size();
reverse(infix.begin(), infix.end());
for (int i = 0; i < len; i++) {
if (infix[i] == '(') { infix[i] = ')';continue;} // * continue
if (infix[i] == ')') { infix[i] = '(';continue;} // * continue
}
string prefix = infix_to_postfix(infix);
reverse(prefix.begin(), prefix.end());
return prefix;
}
string prefix_to_infix(string exp){
stack<string> st;
int len = exp.length();
string char_string;
for(int i=len-1;i>=0;i--){ // prefix : len-1 -> 0
if(is_operator(exp[i])){
string string1 = st.top(); st.pop();
string string2 = st.top(); st.pop();
string temp_string = "";
temp_string += string1; // str1 (exp) str2
temp_string += exp[i];
temp_string += string2;
st.push(temp_string);
}
else{
char_string = "";
char_string+=exp[i];
st.push(char_string);
}
}
return st.top();
}
string postfix_to_infix(string exp){
stack<string> st;
int len = exp.length();
string char_string;
for(int i=0;i<len;i++){ // postfix : 0 -> len-1
if(is_operator(exp[i])){
string string1 = st.top(); st.pop();
string string2 = st.top(); st.pop();
string temp_string = "";
temp_string += string2; // str2 (exp) str1
temp_string += exp[i];
temp_string += string1;
st.push(temp_string);
}
else{
char_string = "";
char_string+=exp[i];
st.push(char_string);
}
}
return st.top();
}
string prefix_to_postfix(string exp){
stack<string> st;
int len = exp.length();
string char_string;
for(int i=len-1;i>=0;i--){ // prefix : len-1 -> 0
if(is_operator(exp[i])){
string string1 = st.top(); st.pop();
string string2 = st.top(); st.pop();
string temp_string = ""; // str1 str2 (exp)
temp_string += string1;
temp_string += string2;
temp_string += exp[i];
st.push(temp_string);
}
else{
char_string = "";
char_string+=exp[i];
st.push(char_string);
}
}
return st.top();
}
string postfix_to_prefix(string exp){
stack<string> st;
int len = exp.length();
string char_string;
for(int i=0;i<len;i++){ // postfix : 0 -> len-1
if(is_operator(exp[i])){
string string1 = st.top(); st.pop();
string string2 = st.top(); st.pop();
string temp_string = "";
temp_string += exp[i]; // (exp) str2 str1
temp_string += string2;
temp_string += string1;
st.push(temp_string);
}
else{
char_string = "";
char_string+=exp[i];
st.push(char_string);
}
}
return st.top();
}
int main(){
string exp = "a+b*(c^d-e)^(f+g*h)-i";
cout<<infix_to_postfix(exp);
cout<<"\n";
exp = "(a-b/c)*(a/k-l)";
cout<<infix_to_prefix(exp);
cout<<"\n";
exp = "*-A/BC-/AKL";
cout<<prefix_to_infix(exp);
cout<<"\n";
exp = "*-A/BC-/AKL";
cout<<prefix_to_postfix(exp);
cout<<"\n";
exp = "ABC/-AK/L-*";
cout<<postfix_to_prefix(exp);
cout<<"\n";
exp = "ab*c+";
cout<<postfix_to_infix(exp);
cout<<"\n";
return 0;
}