-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP727.cpp
79 lines (72 loc) · 1.35 KB
/
P727.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
78
79
#include <stdio.h>
#include <iostream>
#include <stack>
using namespace std;
bool isOp(char c) {
switch(c) {
case '+':
case '-':
case '*':
case '/':
return true;
default:
return false;
}
}
bool isOK(char c) {
return isdigit(c) || isOp(c) || c == '(' || c == ')';
}
// Dijkstra's Shunting yard algorithm. Infix -> postfix math expressions.
// Notice / and - are only left associative
int main() {
int N = 0;
char c, line[39];
gets(line);
int j = 0;
while(isdigit(c = line[j++]))
N = 10*N+(c-'0');
//scanf("%d\n", &N);
//cerr << N << " cases!" << endl;
gets(line);
for(int cas = 0; cas < N; ++cas) {
if(cas != 0)
cout << endl;
stack<char> s;
while(gets(line) && isOK(c = line[0])) {
//cerr << "Handling " << c << endl;
if(isdigit(c)) {
cout << c;
}
else if(c == '(') {
s.push(c);
}
else if(c == ')') {
while((c = s.top()) != '(') {
cout << c;
s.pop();
}
s.pop(); // '('
}
else { // + - * /
while(!s.empty()) {
char c2 = s.top();
if(!isOp(c2))
break;
if(c == '+' || c == '-' || ((c == '/' || c == '*') && !(c2 == '+' || c2 == '-'))) {
cout << c2;
s.pop();
}
else
break;
}
s.push(c);
}
} // while gets.
while(!s.empty()) {
cout << s.top();
s.pop();
}
cout << endl;
}
return 0;
}