Algorithm
Infix to Posfix
By Neilor Tonin, URI Brazil
The teacher asked you to write a program that convert an infix expression to a postfix expression. Like you know, the terms in and pos are according with the operators position. The program will have to handle only with the binary operators +, -, *, /, ^. parenthesis, letters and numbers. An example would be an expression like:
(A*B+2*C^3)/2*A. The program must convert this expression (infix) to the posfix expression: AB*2C3^*+2/A*
All expressions of the test cases are expressions with valid sintax.
Input
The first line of input is an integer N (N < 1000), that indicates the total number of test cases. Each case is a valid expression in the infix format.
Output
For each test case, print the expression converted to posfix expression.
Input Sample | Output Sample |
3 |
A2* |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
map op;
void sieve()
{
op['+']=1;
op['-']=1;
op['/']=2;
op['*']=2;
op['^']=3;
}
void infixToPostfix(string str)
{
stack q;
for(int i=0;i>t;
cin.ignore();
string str;
while(t--)
{
cin>>str;
infixToPostfix(str);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output