Answer to Question #277012 in C++ for sohaib

Question #277012

postfix to infix conversion in c++ prograqmme



1
Expert's answer
2021-12-08T19:03:52-0500
#include<stack>
#include <iostream>
using namespace std;
 
bool checking_operand(char c)
{
   return (c >= 'a' && c <= 'z') ||
          (c >= 'A' && c <= 'Z');
}
 


string ConvertPostToInfix(string text)
{
    stack<string> s1;
 
    for (int i=0; text[i]!='\0'; i++)
    {
        
        if (checking_operand(text[i]))
        {
           string op(1, text[i]);
           s1.push(op);
        }
 
        
        else
        {
            string op1 = s1.top();
            s1.pop();
            string op2 = s1.top();
            s1.pop();
            s1.push("(" + op2 + text[i] +
                   op1 + ")");
        }
    }
 
  
    return s1.top();
}
 


int main()
{
    string st= "fb*c+";
    cout << ConvertPostToInfix(st);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog