-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostfix.cpp
More file actions
170 lines (137 loc) · 3.54 KB
/
postfix.cpp
File metadata and controls
170 lines (137 loc) · 3.54 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
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << #x << " " << x << endl;
class stkch{
private:
int size = 20, topi = -1;
char *s = (char*)malloc(sizeof(char)*size);
void sizeDoubler(){
char *temp = (char*)malloc(sizeof(char)*size*2);
for(int i = 0; i < size; i++) temp[i] = s[i];
char *temp2 = s;
s = temp;
free(temp2);
size*=2;
}
public:
char pop(){
if(topi < 0){
cout << "Stack is Empty" << endl;
}
else{
return s[topi--];
}
}
void push(char d){
while(size <= topi) sizeDoubler();
s[++topi] = d;
}
bool isEmpty(){
if(topi <= -1) return 1;
return 0;
}
char top(){
if(topi == -1) return NULL;
return s[topi];
}
};
class stkin{
private:
int size = 20, topi = -1;
int *s = (int*)malloc(sizeof(int)*size);
void sizeDoubler(){
int *temp = (int*)malloc(sizeof(int)*size*2);
for(int i = 0; i < size; i++) temp[i] = s[i];
int *temp2 = s;
s = temp;
free(temp2);
size*=2;
}
public:
int pop(){
if(topi < 0){
cout << "Stack is Empty" << endl;
}
else{
return s[topi--];
}
}
void push(int d){
while(size <= topi) sizeDoubler();
s[++topi] = d;
}
bool isEmpty(){
if(topi <= -1) return 1;
return 0;
}
int top(){
return s[topi];
}
};
int isOprator(char ch){
if(ch == '/' || ch == '*' || ch == '-' || ch == '+' || ch == '%' || ch == '^' || ch == '(' || ch == ')') return 1;
return 0;
}
int preout(char ch){
if(ch == '-' || ch == '+') return 1;
if(ch == '*' || ch == '/') return 3;
if(ch == '%') return 5;
if(ch == '(') return 9;
if(ch == ')') return 0;
if(ch == '^') return 8;
return 0;
}
int prein(char ch){
if(ch == '-' || ch == '+') return 2;
if(ch == '*' || ch == '/') return 4;
if(ch == '%') return 6;
if(ch == '(') return 0;
if(ch == '^') return 7;
return 0;
}
int cal(int a, int b, char ch){
if(ch == '+') return a+b;
if(ch == '-') return a-b;
if(ch == '*') return a*b;
if(ch == '/') return a/b;
return 0;
}
int main(){
stkch *op = new stkch();
stkin *nums = new stkin();
string exp;
cout << "Enter Expression" << endl;
cin >> exp;
int i = 0;
while(exp[i] != NULL){
if(isOprator(exp[i])){
if(prein(op->top()) > preout(exp[i])){
int t2 = nums->pop();
int t1 = nums->pop();
int t3 = cal(t1,t2,op->pop());
nums->push(t3);
}
else if(prein(op->top()) == preout(exp[i])){
op->pop();
i++;
}
else{
op->push(exp[i++]);
}
}
else{
int temp = 0;
while(!isOprator(exp[i]) && exp[i] != NULL){
temp = temp*10 + (exp[i++] - '0');
}
nums->push(temp);
}
}
while(!(op->isEmpty())){
int t2 = nums->pop();
int t1 = nums->pop();
int t3 = cal(t1,t2,op->pop());
nums->push(t3);
}
cout << nums->top();
}