Infix to Postfix Conversion

Aim: 
To write a java program that converts infix expression into postfix expression.

Description: 
This Program translates an infix expression to a postfix expression. In this program we take “infix” string as an input and displays “postfix” string. In the process, we use a stack as a temporary storage. So, instead of writing a separate program for stack operations, the Infix_Postfix class uses java.util.Stack class.

Program:
import java.io.*;
import java.util.*;
class Infix_Postfix
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader( new InputStreamReader (
System.in));
System.out.println("Enter Infix Expression : ");
String s= br.readLine();
Stack<Character> st=new Stack<Character>();
String output="";
int i=0,len=s.length();
char x;
st.push('@');
while(len!=0)
{ char c=s.charAt(i);
if(c=='(')
{ st.push(c);
}
else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^'||c=='$')
{ check(st,c,output);
st.push(c);
}
else if(c==')')
{ while((x=(Character)st.pop())!='(')
{ output=output+x;
}
}
else
{ output+=s.charAt(i);
}
i=i+1;
len--;
}
while((x=(Character)st.pop())!='@')
{ output+=x;
}
System.out.println("postfix Expression is : "+ output);
}
static void check(Stack st,char c,String output)
{ while(priority(c)<=priority((Character)st.peek()))
output=output+st.pop();
}
static int priority(char ch)
{ if(ch=='+'||ch=='-')
return(1);
else if(ch=='*'||ch=='/')
return(2);
else if(ch=='$'||ch=='^')
return(3);
else
return(0);
}
}