-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInfixToPrefix.cpp
83 lines (72 loc) · 1.85 KB
/
InfixToPrefix.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
80
81
82
83
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
int precedence(char ch){
if(ch == '^') return 3;
else if(ch == '*' || ch == '/') return 2;
else if(ch == '+' || ch == '-') return 1;
else return -1;
}
bool isOperand(char ch){
return ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) ? true : false;
}
string infixToPostfix(string s){
string res;
stack<char> st;
for(int i=0;s[i];i++){
if(isOperand(s[i])){
res += s[i];
}
else if(s[i] == '('){
// Just push into stack
st.push(s[i]);
}
else if(s[i] == ')'){
// Pop out until u find (
while(!st.empty() && st.top()!='('){
res += st.top();
st.pop();
}
if(!st.empty()){
st.pop() ; // Pop (
}
}
else{
// Operator found check precedence and push into stack
while(!st.empty() && (precedence(s[i]) <= precedence(st.top()))){
res += s[i];
st.pop();
}
// Now operator has correct place to insert
st.push(s[i]);
}
}
while(!st.empty()){
res += st.top() ;
st.pop();
}
return res;
}
string infixToPrefix(string s){
// First reverse the string
reverse(s.begin(),s.end());
// Convert ) to ( and ( to ) , vice versa
for(int i=0;s[i];i++){
if(s[i] == '('){
s[i] = ')';
}
else if(s[i] == ')'){
s[i] = '(' ;
}
}
// Call postfix wallah algorithm
string res = infixToPostfix(s);
return res;
}
int main(){
string s = "(a+b/c)*(a/k-l)";
cout << "Posfix is : " << infixToPostfix(s) << endl;
cout << "Prefix is : " << infixToPrefix(s) << endl;
return 0;
}