-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfixEvolutionWithBracket.cpp
More file actions
70 lines (64 loc) · 1.91 KB
/
Copy pathInfixEvolutionWithBracket.cpp
File metadata and controls
70 lines (64 loc) · 1.91 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
#include <iostream>
#include <stack>
using namespace std;
int prio(char ch){
if(ch == '+' || ch == '-') return 1;
else return 2;
}
int solve(int val1,char ch,int val2){
if(ch == '+') return val1 + val2;
else if(ch == '-') return val1 - val2;
else if(ch == '*') return val1 * val2;
else return val1 / val2;
}
int main(){
cout << (7+9)*4/8-3 << endl;
stack<int> val;
stack<char> opr;
string str = "(7+9)*4/8-3";
for(int i = 0 ; i < str.size() ; i++){
if(str[i] >= '0' && str[i] <= '9') val.push((int)(str[i] - '0'));
else{
if(opr.empty() || str[i] == '(' || opr.top() == '(') opr.push(str[i]);
else if(str[i] ==')'){
while(opr.top() != '('){
char ch = opr.top();
opr.pop();
int val2 = val.top();
val.pop();
int val1 = val.top();
val.pop();
int ans = solve(val1,ch,val2);
val.push(ans);
}
opr.pop();
}
else if(prio(str[i]) > prio(opr.top())) opr.push(str[i]);
else{
while(opr.empty() == false && prio(opr.top()) >= prio(str[i])){
char ch = opr.top();
opr.pop();
int val2 = val.top();
val.pop();
int val1 = val.top();
val.pop();
int ans = solve(val1,ch,val2);
val.push(ans);
}
opr.push(str[i]);
}
}
}
while(opr.size() > 0){
char ch = opr.top();
opr.pop();
int val2 = val.top();
val.pop();
int val1 = val.top();
val.pop();
int ans = solve(val1,ch,val2);
val.push(ans);
}
cout << val.top() << endl;
return 0;
}