-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ShuntingYard.cpp
77 lines (72 loc) · 1.85 KB
/
ShuntingYard.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
#include <iostream>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <sstream>
#include <cassert>
using namespace std;
// precedences
map<char, int> prec{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
// handles +, -, *, / and parentheses
// only handles single digit, positive numbers for simplicity
int shuntyard(stringstream& ss) {
char c;
queue<char> output;
stack<char> ops;
while (ss >> c) {
if (isdigit(c)) {
output.push(c);
} else if (c == '(') {
ops.push(c);
} else if (c == ')') {
while (ops.top() != '(') {
output.push(ops.top());
ops.pop();
}
ops.pop();
} else {
assert(c == '+' || c == '-' || c == '*' || c == '/');
while (!ops.empty() && (ops.top() != '(' && prec[ops.top()] >= prec[c])) {
output.push(ops.top());
ops.pop();
}
ops.push(c);
}
}
while (!ops.empty()) {
assert(ops.top() != '(' && ops.top() != ')');
output.push(ops.top());
ops.pop();
}
stack<int> st;
while (!output.empty()) {
c = output.front();
output.pop();
if (isdigit(c)) {
st.push(c - '0');
} else {
int x, y;
y = st.top(); st.pop();
x = st.top(); st.pop();
if (c == '+') {
st.push(x + y);
} else if (c == '-') {
st.push(x - y);
} else if (c == '/') {
st.push(x / y);
} else {
st.push(x * y);
}
}
}
return st.top();
}
int main() {
string input;
getline(cin, input);
stringstream ss(input);
int val = shuntyard(ss);
cout << input << " = " << val << endl;
return 0;
}