-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoRPN.cpp
47 lines (40 loc) · 1.24 KB
/
toRPN.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
#include <iostream>
#include <stack>
#include <map>
using namespace std;
map<char, int> priority;
stack<char> stack1;
int main()
{
priority.insert(pair<char, int>('*', 5));
priority.insert(pair<char, int>('/', 5));
priority.insert(pair<char, int>('+', 10));
priority.insert(pair<char, int>('-', 10));
string expr = "a+b*c+(d*e+f+g*h)-i/j+k";
string result = "";
cout << expr << endl;
for (char c : expr) {
map<char, int>::iterator cIt, topIt;
if (c == '(') {
stack1.push(c);
} else if (c == ')') {
while (stack1.top() != '(')
result += stack1.top(), stack1.pop();
stack1.pop();
} else if ((cIt = priority.find(c)) != priority.end()) {
while (stack1.size() > 0) {
topIt = priority.find(stack1.top());
if (stack1.top() != '(' && cIt->second >= topIt->second) // pop print
result += stack1.top(), stack1.pop();
else
break;
}
stack1.push(c);
} else
result += c;
}
while (stack1.size() > 0)
result += stack1.top(), stack1.pop();
cout << result << endl;
return 0;
}