I have seen several posters asking for help on programs converting an Infix expression to a Postfix one. I have written a program to convert a simple Infix expression to a Postfix one so that people here can be directed towards some sample code from which they get the idea to writing one. I have also provided comments describing what the code part is supposed to do in that block, the idea here is to discourage blind copying and to encourage the understanding of the program. Once the idea behind such a program is undestood, even a beginner can write one for himself.
There can be many ways in which a particular solution can be implemented, this impementation is based on this text.
Lastly I want to mention why I call this program one that converts a simple Infix expression. Thats because I haven't written it to take into consideration complex expressions with paranthesis in them, also I haven't written it for an exhaustive list of operators (the operators I have taken into account are mentioned in the source code). Programmers can take an hint from this simple program and add their own additions to it.
Infix to Postfix
package alok.examples;
import java.util.Scanner;
import java.util.Stack;
/*
* This is a code snippet to convert a simple Infix Expression to a Postfix one.
* NOTE : It would not work for expressions containing paranthesis. It only takes into
* account operators for the following operations:
* 1. Multiplication
* 2. Division
* 3. Modulus
* 4. Addition
* 5. Subtraction
*
* The precedence of the operations is shown below. Lower level operations have a higher
* precedence, so here, operations in Level 1 have higher precedence than those mentioned
* in Level 2.
* Operations mentioned in the same level have equal precedence.
*
* Level 1. Multiplication, Division, Modulus
* Level 2. Addition, Subtraction
*/
public class Infix2Postfix {
private Stack<String> stack;
private String infixExp;
private String postfixExp = "";
public Infix2Postfix(String exp){
String str = "";
infixExp = exp;
stack = new Stack<String>();
for (int i=0;i<infixExp.length();i++){
/*
* If the character is a letter or a digit we append it to the postfix
* expression directly.
*/
str = infixExp.substring(i,i+1);
if(str.matches("[a-zA-Z]|\\d"))
postfixExp += str;
else if (isOperator(str)){
/*
* If the stack is empty we directly push the current char into it.
*/
if (stack.isEmpty()){
stack.push(str);
}
else{
/*
* If the current character is an operator, we need to check the stack
* status then, if the stack top contains an operator with lower
* precedence, we push the current character in the stack else we pop
* the character from the stack and add it to the postfix string. This
* continues till we either find an operator with lower precedence in the
* stack or we find the stack to be empty.
*/
String stackTop = stack.peek();
while (getPrecedence(stackTop,str).equals(stackTop)&& !(stack.isEmpty())){
postfixExp += stack.pop();
if (!(stack.isEmpty()))
stackTop = stack.peek();
}
stack.push(str);
}
}
}
// In the end just append all the operators from the stack to the postfix expression.
while(!(stack.isEmpty()))
postfixExp += stack.pop();
// Print out the postfix expression
System.out.println("The postfix form of the expression you entered is: " + postfixExp);
}
/*
* Returns true if 'ch' is an operator, false o/w
*/
private boolean isOperator(String ch){
String operators = "*/%+-";
if (operators.indexOf(ch) != -1)
return true;
else
return false;
}
/*
* Returns the operator with higher precedence among 'op1' & 'op2', if they have equal
* precedence, the first operator in the argument list (op1) is returned.
*/
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 Infix2Postfix(expression);
}
}
apc00008 0 Newbie Poster
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
Anonymous123 0 Newbie Poster
verruckt24 438 Posting Shark
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.