You are on page 1of 1

#include<bits/stdc++.

h>
using namespace std ;
int priority(char c)
{
if(c=='*' || c=='/')
return 1;
else if(c=='+' || c=='-')
return 2;
}
bool isOperator(char c)
{
if(c == '+' || c=='-' || c=='*' || c=='/')
return true ;
else
return false ;
}
string Post_to_Infix(string s)
{
stack<string> st ;
for(int i=0;i<s.size();i++)
{
if(!isOperator(s[i])){
string x(1,s[i]) ;
st.push(x) ;
}
else
{
string s2 = st.top() ;
st.pop() ;
string s1 = st.top() ;
st.pop() ;
string x ;
string ch(1,s[i]) ;
if(priority(s[i])==1 || i==s.size()-1)
x = s1+ch+s2;
else if(priority(s[i])==2)
x = "("+s1+ch+s2+")";
st.push(x) ;
}
}
if(st.size() == 1){
return st.top() ;
}
}
int main()
{
string s ;
cout<<"Enter the Post-Fix Expression : ";
cin>>s ;
string ans = Post_to_Infix(s) ;
cout<<endl<<"The Required In-Fix Expression is :- "<<ans ;
return 0 ;
}

You might also like