-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfixpostfix.cpp
More file actions
62 lines (56 loc) · 1.41 KB
/
Copy pathinfixpostfix.cpp
File metadata and controls
62 lines (56 loc) · 1.41 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
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<stack>
#include<locale>
using namespace std;
int preced(char ch) {
if(ch == '+' || ch == '-') {
return 1;
}else if(ch == '*' || ch == '/') {
return 2;
}else if(ch == '^') {
return 3;
}else {
return 0;
}
}
string inToPost(string infix ) {
stack<char> stk;
stk.push('#');
string postfix = "";
string::iterator it;
for(it = infix.begin(); it!=infix.end(); it++) {
if(isalnum(char(*it)))
postfix += *it;
else if(*it == '(')
stk.push('(');
else if(*it == '^')
stk.push('^');
else if(*it == ')') {
while(stk.top() != '#' && stk.top() != '(') {
postfix += stk.top();
stk.pop();
}
stk.pop();
}else {
if(preced(*it) > preced(stk.top()))
stk.push(*it);
else {
while(stk.top() != '#' && preced(*it) <= preced(stk.top())) {
postfix += stk.top();
stk.pop();
}
stk.push(*it);
}
}
}
while(stk.top() != '#') {
postfix += stk.top();
stk.pop();
}
return postfix;
}
int main() {
string infix;
cin >> infix;
cout << "Postfix Form Is: " << inToPost(infix) << endl;
}