#include<bits/stdc++.h>
#include<string>
#include<stdio.h>
#include<stdlib.h>
/*prefix to postfix*/
using namespace std;
string convertPreToPost(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 + b + string(1, pre[i]);
stk.push(t);
}
else
{
stk.push(string(1, pre[i]));
}
}
return stk.top();
}
int main()
{
string pre;
cout<<"
Enter Prefix: ";
cin>>pre;
cout<<"
Postfix: "<<convertPreToPost(pre)<<endl;
}
Output
Enter Prefix: +ab
Postfix: ab+
Add your comment