Skip to content

Instantly share code, notes, and snippets.

@ManiruzzamanAkash
Created February 16, 2017 18:52
Show Gist options
  • Save ManiruzzamanAkash/154c2ce3a71a2e15107717de9bf1db34 to your computer and use it in GitHub Desktop.
Save ManiruzzamanAkash/154c2ce3a71a2e15107717de9bf1db34 to your computer and use it in GitHub Desktop.
Infix to Postfix notation code in java language
package infixtopostfix;
import java.util.Stack;
import java.util.Scanner;
public class InfixToPostfix {
static int getPrecedence(char checkChar)
{
if(checkChar=='+'||checkChar=='-')
return 1;
if(checkChar=='*'||checkChar=='/')
return 2;
if(checkChar=='('||checkChar==')')
return 0;
return -1;
}
public static void main(String[] args) {
Stack<Character> stack=new Stack();
Scanner scanner =new Scanner(System.in);
String result="";
String inputStr=scanner.nextLine();
char[] inputCharArray=inputStr.toCharArray();
for(char chrac:inputCharArray)
System.out.println(chrac);
for(int i=0;i<inputCharArray.length;i++)
{
char checkChar=inputCharArray[i];
if(checkChar!='+'&&checkChar!='-'&&checkChar!='/'&&checkChar!='*'&&checkChar!='('&&checkChar!=')')
{
result=result+checkChar;
}
else
{
if(checkChar!='('&&checkChar!=')')
{
if(stack.isEmpty())
{
stack.push(checkChar);
}
else
{
while(getPrecedence(stack.peek())>=getPrecedence(checkChar))
{
result=result+stack.pop();
if(stack.isEmpty())
break;
}
stack.push(checkChar);
}
}
else
{
if(checkChar=='(')
stack.push(checkChar);
else
{
while(stack.peek()!='(')
{
result=result+stack.pop();
}
stack.pop();
}
}
}
}
while(!stack.isEmpty())
result=result+stack.pop();
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment