-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_to_post.cpp
259 lines (222 loc) · 5.01 KB
/
in_to_post.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
//we have isdigit() here by using <cctype>.
//push_back() function valid for string.
//this code is only for single digit numbers..................
#include<iostream>
#include<string>
#include<cctype> //for isdigit
#include<cmath>
using namespace std;
int precedence(char ch){
switch(ch){
case '^' : return 4;
break;
case '*' : return 3;
break;
case '/' : return 3;
break;
case '-' : return 2;
break;
case '+' : return 2;
break;
case '=' : return 1;
break;
default : return 0;
break;
}
}
int number(char ch){
switch(ch){
case '1' : return 1;
break;
case '2' : return 2;
break;
case '3' : return 3;
break;
case '4' : return 4;
break;
case '5' : return 5;
break;
case '6' : return 6;
break;
case '7' : return 7;
break;
case '8' : return 8;
break;
case '9' : return 9;
break;
default : return 0;
break;
}
}
void push(char item,char *stack,int &top,int size){
if(top==size-1)
cout<<"\noverflow";
else{
top+=1;
stack[top]=item;
cout<<"\nitem added to stack:"<<stack[top];
}
}
char pop(int &top,char *stack){
char item;
if(top==-1)
cout<<"\nunderflow";
else{
item=stack[top];
top=top-1;
return item;
}
return '0';
}
void display(char* stack,int &top){
cout<<"\nstack is:";
while(top!=-1){
cout<<stack[top];
top-=1;
}
}
string rev(string input){
string preinput;
for(int i=input.length()-1;i>=0;i--){
if(input[i]=='(')
preinput.push_back(')');
else if(input[i]==')')
preinput.push_back('(');
else
preinput.push_back(input[i]);
}
return preinput;
}
string postfix(string input){
input.push_back('$');
cout<<"\ninput string after push back:"<<input<<"\n";
string post_exp;
char stack[input.length()],k;
int top=0;
stack[top]='$';
cout<<"\nstack[top]:"<<stack[top]<<"\n";
for(int i=0;input[i]!='$';i++){
if(isdigit(input[i])){
cout<<"\nisdigit(input["<<i<<"]):"<<isdigit(input[i])<<"\n";
post_exp.push_back(input[i]);
}
//checkfrom here;
else if(input[i]==')'){
while(stack[top]!='('){
post_exp.push_back(stack[top]);
cout<<"\npost_fix expressioin:"<<post_exp<<"\n";
k=pop(top,stack);
cout<<"\nitem popped out:"<<k;
}
k=pop(top,stack);
cout<<"\nitem popped out:"<<k;
}
else if(input[i]!='(' && stack[top]!='('){
if(precedence(input[i])<=precedence(stack[top])){
post_exp.push_back(stack[top]);
cout<<"\npost_fix expressioin:"<<post_exp<<"\n";
k=pop(top,stack);
cout<<"\nitem popped out:"<<k;
push(input[i],stack,top,input.length());
}
else
push(input[i],stack,top,input.length());
}
else
push(input[i],stack,top,input.length());
}
while(stack[top]!='$'){
post_exp.push_back(stack[top]);
k=pop(top,stack);
cout<<"\nitem popped out:"<<k;
}
k=pop(top,stack);
cout<<"\nitem popped out:"<<k;
display(stack,top);
cout<<"\ntop is:"<<top;
return post_exp;
}
string prefix(string input){
string prefixstr;
prefixstr=rev(postfix(rev(input)));
return prefixstr;
}
//for evaluation
int operate(char b,char a,char op){
switch(op){
case '*' : return (b*a);
break;
case '+' : return (b+a);
break;
case '-' : return (b-a);
break;
case '/' : return (b/a);
break;
case '^' : return pow(b,a);
break;
default : return 0;
break;
}
}
void pushint(int item,int* stack,int& top){
top+=1;
stack[top]=item;
}
int popint(int& top,int* stack){
int item =stack[top];
top-=1;
return item;
}
void evaluate(string postexp){
int stack[postexp.length()];
int top=-1;
int a,b,c;
for(int i=0;i<postexp.length();i++){
if(isdigit(postexp[i]))
pushint(number(postexp[i]),stack,top);
else{
cout<<"\nstack's top is:"<<stack[top];
a=(popint(top,stack));
cout<<"\na is:"<<a;
cout<<"\nstack's top is:"<<stack[top];
b=(popint(top,stack));
cout<<"\nb is:"<<b;
c=(operate(b,a,postexp[i]));
cout<<"\nc is:"<<c;
pushint(c,stack,top);
}
}
cout<<"\nevaluated answer is:"<<stack[top];
}
void evaluate_pre(string postexp){
int stack[postexp.length()];
int top=-1;
int a,b,c;
for(int i=postexp.length()-1;i>=0;i--){
if(isdigit(postexp[i]))
pushint(number(postexp[i]),stack,top);
else{
cout<<"\nstack's top is:"<<stack[top];
a=(popint(top,stack));
cout<<"\na is:"<<a;
cout<<"\nstack's top is:"<<stack[top];
b=(popint(top,stack));
cout<<"\nb is:"<<b;
c=(operate(a,b,postexp[i]));
cout<<"\nc is:"<<c;
pushint(c,stack,top);
}
}
cout<<"\nevaluated answer is:"<<stack[top];
}
int main(){
string post_str,input_str;
cout<<"\nenter the input expression:";
cin>>input_str;
//post_str=postfix(input_str);
//cout<<"\n"<<"postfix:"<<post_str;
//evaluate(post_str);
cout<<"\n"<<prefix(input_str);
evaluate_pre(prefix(input_str));
return 0;
}