Solved programming problem

Convert Postfix Expression to Infix Expression in C++

Source code

#include<bits/stdc++.h>

#include<string>

#include<stdio.h>

#include<stdlib.h>

/*postfix to infix*/

using namespace std;

string convertPostToIn(string post)

{

    stack<string> stk;

    for(int i=0; i<post.size(); i++)

    {

        if(post[i]=='+' || post[i]=='-' ||post[i]=='*' ||post[i]=='/' || post[i]=='^')

        {

           

            string a = stk.top();

            stk.pop();

            string b = stk.top();

            stk.pop();

            string t = "(" + b +   string(1, post[i]) + a +")";

            stk.push(t);

        }

        else

        {

            stk.push(string(1, post[i]));

        }

    }

    return stk.top();

}

int main()

{

    string post;

    cout<<"
Enter Postfix: ";

    cin>>post;

    cout<<"
Infix: "<<convertPostToIn(post)<<endl;

}

Output

Enter Postfix: ab+

Infix: (a+b)

 

Join the discussion

Add your comment