#include<bits/stdc++.h>
#include<string>
#include<stdio.h>
#include<stdlib.h>
/*prefix to infix*/
using namespace std;
string convertPreToIn(string pre)
{
stack<string> stk;
for(int i=pre.size()-1; i>=0; i--)
{
if(pre[i]=='+' || pre[i]=='-' ||pre[i]=='*' ||pre[i]=='/' || pre[i]=='^')
{
string a = stk.top();
stk.pop();
string b = stk.top();
stk.pop();
string t = "(" + a + string(1, pre[i]) + b +")";
stk.push(t);
}
else
{
stk.push(string(1, pre[i]));
}
}
return stk.top();
}
int main()
{
string pre;
cout<<"
Enter Prefix: ";
cin>>pre;
cout<<"
Infix: "<<convertPreToIn(pre)<<endl;
}
Output
Enter Prefix: +ab
Infix: (a+b)
Add your comment