## Algorithm

Problem Name: beecrowd | 1077

# Infix to Posfix

By Neilor Tonin, URI Brazil

Timelimit: 1

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 A*2 (A*2+c-d)/2 (2*4/a^b)/(2*c) A2* A2*c+d-2/ 24*ab^/2c*/

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````
#include <iostream>
using namespace std;
map < char,int> op;
void sieve()
{
op['+']=1;
op['-']=1;
op['/']=2;
op['*']=2;
op['^']=3;
}
void infixToPostfix(string str)
{
stack < char> q;

for(int i=0;i < str.length();i++)
{
if(isalnum(str[i])) cout<<str[i];

else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||
str[i]=='^')
{
if(!q.empty()){
while(op[str[i]]<=op[q.top()])
{
cout<<q.top();
q.pop();
if(q.empty()) break;
}
}
q.push(str[i]);
}
else if(str[i]=='('){
q.push('(');
//cout<<"first";
}

else if(str[i]==')')
{
while(q.top()!='(')
{
cout<<q.top();
q.pop();
}
q.pop();
}
}
while(!q.empty())
{
cout<<q.top();
q.pop();
}
cout<<endl;
}
int main()
{
sieve();
int t;
cin>>t;
cin.ignore();
string str;
while(t--)
{
cin>>str;
infixToPostfix(str>;
}
return 0;
}
``````
Copy The Code &

Input

cmd
3 A*2 (A*2+c-d)/2 (2*4/a^b)/(2*c)

Output

cmd
A2* A2*c+d-2/ 24*ab^/2c*/