-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfix-To-Postfix.cpp
More file actions
55 lines (48 loc) · 1.1 KB
/
Infix-To-Postfix.cpp
File metadata and controls
55 lines (48 loc) · 1.1 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
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
bool isOperant(char x) {
if(x>='A'&&x<='Z'){
return true;
}
return false;
}
bool isHighPrio (char a,char b) {
if((a == '+' || a == '-') && (b=='/' || b=='*')) {
return true;
} else {
return false;
}
}
void inFixToPostFix(string expr){
stack<char> S;
int a = expr.length();
vector<char> postFix(a);
int index=0;
for(int i=0;expr[i]!='\0';i++) {
if(isOperant(expr[i])) {
postFix[index++] = (expr[i]);
} else if(!S.empty()){
if(isHighPrio(S.top(),expr[i])) {
S.push(expr[i]);
} else {
while(!S.empty()) {
postFix[index++]=(S.top());
S.pop();
}
S.push(expr[i]);
}
} else {
S.push(expr[i]);
}
}
while(!S.empty()) {
postFix[index++]=(S.top());
S.pop();
}
puts(&postFix[0]);
}
int main() {
inFixToPostFix("A+B*C-D*E");
}