Home Tutorial Java Core Java Convert Infix to Postfix

 
 

Share on Google+Share on Google+
Java Convert Infix to Postfix
Posted on: December 22, 2012 at 12:00 AM
Advertisement
In this tutorial, you will learn how to convert expression from infix to postfix.

Java Convert Infix to Postfix

In this tutorial, you will learn how to convert expression from infix to postfix. The given code accepts the expression from the user and display its postfix expression by using Stacks.

Example:

import java.util.Scanner;
import java.util.Stack;

public class InfixToPostfix {
private String infix;
private String postfix = "";

public void convertString(String a){
String str = "";
infix = a;
Stack<String> stack = new Stack<String>();

for(int i = 0; i < infix.length(); i++){
str = infix.substring(i,i+1);
if(str.matches("[a-zA-Z]|\\d"))
postfix += str;
else if (isOperator(str)){
if (stack.isEmpty()){
stack.push(str);
}
else{
String stackTop = stack.peek();
while (getPrecedence(stackTop,str).equals(stackTop)&& !(stack.isEmpty())){
postfix += stack.pop();
if (!(stack.isEmpty()))
stackTop = stack.peek();
}
stack.push(str);
}
}
}
while(!(stack.isEmpty()))
postfix += stack.pop();
System.out.println("Postfix of the expression is: " + postfix);
}

private boolean isOperator(String ch){
String operators = "*/%+-";
if (operators.indexOf(ch) != -1)
return true;
else
return false;
}

private String getPrecedence(String op1, String op2){
String multiplicativeOps = "*/%";
String additiveOps = "+-";
if ((multiplicativeOps.indexOf(op1) != -1) && (additiveOps.indexOf(op2) != -1))
return op1;
else if ((multiplicativeOps.indexOf(op2) != -1) && (additiveOps.indexOf(op1) != -1))
return op2;
else if((multiplicativeOps.indexOf(op1) != -1) && (multiplicativeOps.indexOf(op2) != -1))
return op1;
else
return op1;
}
public static void main(String[] args) {

System.out.println("Enter an expression in the Infix form:");
Scanner scanner = new Scanner(System.in);

String expression = scanner.nextLine();
new InfixToPostfix().convertString(expression);
} 
}
Advertisement

Related Tags for Java Convert Infix to Postfix:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 22, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com