-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathONP.cpp
69 lines (60 loc) · 1.53 KB
/
ONP.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
// Goal: To convert infix to Reverse Polish Notation (Postfix).
#include <bits/stdc++.h>
using namespace std;
bool isOperator(char c) {
// isalpha() is used to check if input is alphabet or not.
return !isalpha(c) && !isdigit(c);
}
int getPriority(char c) {
if (c == '-' || c == '+')
return 1;
else if (c == '*' || c == '/')
return 2;
else if (c == '^')
return 3;
return 0;
}
string infixToPostfix(string infix) {
infix = '(' + infix + ')';
stack<char> char_stack;
string postfix;
for (int i = 0; i < infix.length(); ++i) {
if (isalpha(infix[i]) || isdigit(infix[i])) {
// If character is operand, add it to postfix.
postfix += infix[i];
} else if (infix[i] == '(') {
// If character is '(', push it to stack.
char_stack.push('(');
} else if (infix[i] == ')') {
// If character is ')', pop untill ')' comes.
while (char_stack.top() != '(') {
postfix += char_stack.top();
char_stack.pop();
}
char_stack.pop(); // Remove '(' from stack.
} else {
// Operator found.
if (isOperator(char_stack.top())) {
while (getPriority(infix[i]) <= getPriority(char_stack.top())) {
postfix += char_stack.top();
char_stack.pop();
}
char_stack.push(infix[i]);
}
}
}
return postfix;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while (T--) {
string s;
cin >> s;
cout << infixToPostfix(s) << "\n";
}
return 0;
}