Answer to Question #246001 in C++ for roe

Question #246001

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 * /


1
Expert's answer
2021-10-04T00:50:43-0400

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



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
APPROVED BY CLIENTS