-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfixToPostfixUsingCPP.cpp
53 lines (52 loc) · 1.09 KB
/
InfixToPostfixUsingCPP.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
#include<bits/stdc++.h>
using namespace std;
bool Operator(char a){
if(a == '+' || a == '-' || a == '*' || a == '/')
return true;
else
return false;
}
int precedance(char a){
if(a == '*' || a == '/')
return 3;
else if(a == '+' || a == '-')
return 2;
else
return 0;
}
string InfixToPostfix(string infix){
stack<char> s;
s.push('$');
string postfix;
int i = 0;
while(infix[i] != '\0'){
if(!Operator(infix[i])){
postfix+=infix[i];
i++;
}
else{
if(precedance(infix[i]) > precedance(s.top())){
s.push(infix[i]);
i++;
}
else{
char ch = s.top();
s.pop();
postfix+=ch;
}
}
}
while(s.top()!='$'){
char c = s.top();
s.pop();
postfix+=c;
}
return postfix;
}
int main(){
string infix = "a-b+t/d";
// cout<<"Enter the string:";
// cin>>infix;
string postfix = InfixToPostfix(infix);
cout<<postfix<<"\n";
}