import java.util.*;
class Main{
public static int is_operator(char c)
{
if(c=='^'||c=='/'||c=='*'||c=='+'||c=='-')
return 1;
else
return 0;
}
public static int precedence(char a )
{
if(a=='^')// expoenent-highest priority
return 3;
else if(a=='/'||a=='*')
return 2;
else if(a=='+'||a=='-')//lowest priority
return 1;
else
return 0;
}
public static void infix_to_postfix( String str[], int top,char[] stack)
{
Scanner sc=new Scanner(System.in);
int l=str.length;
for(int i=0;i<l;i++)
{
String result="";
for(int j=0;j<str[i].length();j++)
{
char c=str[i].charAt(j);
if(is_operator(c)==0 && c !='(' && c!=')')
result=result+c;
else if(c=='(')
{
top++;
stack[top]=c;
}
else if(c==')')
{
char x = stack[top];
while(x != '(')
{
result = result+x;
top--;
x = stack[top];
}
top--;
}
else if(is_operator(c)==1)
{
if(precedence(c)>(precedence (stack[top]))) //
{
top++;
stack[top]=c;
}
else
{
while(precedence(c)<=precedence(stack[top]) && precedence(c)!= 0)
{
result=result+stack[top];
top--;
}
top++;
stack[top]=c;
}
}
}
System.out.println(result);
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//*infix = "(" + infix + ")";*/
int top=-1;
boolean b = true;
int n =0;
while(b){
System.out.println("Enter no. of inputs(100 inputs at max): ");
n = sc.nextInt();//1
try{
if(n>100)
throw new ArithmeticException("input index out of bound!");
else b=false;
}
catch(ArithmeticException e){
System.out.println(e);
}}
int i=0;
String[] str = new String[n];
char stack[]=new char[100];
System.out.println("Enter all the expressions(400 characters at max): ");
sc.nextLine();
for(i=0 ; i<n; i++)
{
try{
str[i]= sc.nextLine();
if(str[i].length()>400)
throw new ArithmeticException("input index out of bound!");
else b=false;
}
catch(ArithmeticException e){
System.out.println(e);
}
}
infix_to_postfix(str,top,stack);
}
}