forked from spaghetti-source/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfix_to_postfix.cc
More file actions
51 lines (47 loc) · 1.11 KB
/
infix_to_postfix.cc
File metadata and controls
51 lines (47 loc) · 1.11 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
//
// Convert infix notation to postfix notation
//
// Description:
// 1 * 2 + 3 * (4 + 5) <- infix notation
// 1 2 * 3 4 5 + * + <- postfix notation
// Postfix notation is easy to evaluate.
//
// Algorithm:
// Shunting-yard algorithm by Dijkstra.
//
// Verified:
// SPOJ 4: ONP - Transform the Expression
//
#include <iostream>
#include <stack>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <sstream>
using namespace std;
string infix_to_postfix(string s) {
s += '_'; // terminal symbol
stringstream ss;
vector<char> op = {'_'};
auto rank = [](char c) { return string("(^/*-+)").find(c); };
for (char c: s) {
if (isalnum(c)) ss << c;
else {
for (; op.back() != '('; op.pop_back()) {
if (rank(op.back()) >= rank(c)) break;
ss << op.back();
}
if (c == ')') op.pop_back();
else op.push_back(c);
}
}
return ss.str();
}
int main() {
int ncase; scanf("%d", &ncase);
for (int icase = 0; icase < ncase; ++icase) {
char s[1024]; scanf("%s", s);
printf("%s\n", infix_to_postfix(s).c_str());
}
}