Convert the given postfix expression into equivalent infix expression.Show all steps ,no marks will be given for direct answer.
a b + c -d e * /
Source code
#include <bits/stdc++.h>
using namespace std;
bool is_operand(char c)
{
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z');
}
string convertToInfix(string expression)
{
stack<string> stk;
for (int i=0; expression[i]!='\0'; i++)
{
if (is_operand(expression[i]))
{
string operation(1, expression[i]);
stk.push(operation);
}
else
{
string opr1 = stk.top();
stk.pop();
string opr2 = stk.top();
stk.pop();
stk.push("(" + opr2 + expression[i] +
opr1 + ")");
}
}
return stk.top();
}
int main()
{
string expression = "ab+c-de*/";
cout << convertToInfix(expression);
return 0;
}
Output
Comments
Leave a comment