Results 1 to 4 of 4
Thread: InfixToPostfix
- 03-22-2012, 06:33 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
InfixToPostfix
how to put the precedence class to this coding.
Infix2Postfix.java
Java Code:import java.util.Scanner; import java.util.Stack; public class Infix2Postfix { private Stack<String> stack; private String infixExp; private String postfixExp = ""; public Infix2Postfix() {} public void convertToPostfix(String exp){ String str = ""; infixExp = exp; stack = new Stack<String>(); for (int i=0;i<infixExp.length();i++){ str = infixExp.substring(i,i+1); if(str.matches("[a-zA-Z]|\\d")) postfixExp += str; else if (isOperator(str)){ if (stack.isEmpty()){ stack.push(str); } else{ String stackTop = stack.peek(); while (precedence(stackTop,str).equals(stackTop)&& !(stack.isEmpty())){ postfixExp += stack.pop(); if (!(stack.isEmpty())) stackTop = stack.peek(); } stack.push(str); } } } System.out.println("The postfix form of the expression you entered is: " + postfixExp); } private boolean isOperator(String ch){ String operators = "*/%+-"; if (operators.indexOf(ch) != -1) return true; else return false; } private String precedence(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 void getInfix(String infixExp) { this.infixExp = infixExp; } public String showInfix() { return infixExp; } public String showPostfix() { return postfixExp; } }
TestProgInfix2Postfix.java
Java Code:import java.util.Scanner; import java.util.Stack; public class TestProgInfix2Postfix { public static void main(String[] args) { int count = 0; while(count < 5) { System.out.println("Enter an expression in the Infix form:"); Scanner scanner = new Scanner(System.in); String expression = scanner.nextLine(); Infix2Postfix x = new Infix2Postfix(); x.convertToPostfix(expression); x.getInfix(expression); System.out.println(); System.out.println("Trying to view infix: "+x.showInfix()); System.out.println("Trying to view postfix: "+x.showPostfix()); System.out.println(); count++; } } }
- 03-22-2012, 08:30 PM #2
Re: InfixToPostfix
how to put the precedence class to this coding.
What is the "precedence class"?If you don't understand my response, don't ignore it, ask a question.
- 03-23-2012, 08:35 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: InfixToPostfix
precedence: precedence between two operators. If the first operator is of higher or equal precedence than the second operator, its returns the value true, otherwise its return value false.
- 03-23-2012, 09:02 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: InfixToPostfix
The essential part of a stack based algorithm for infix -> posfix expressions is the priority/precedence of the operator on the stack top, compared to the priority/precedence of the operator to be entered on the stack. There's a nice explanation in Sahni and Horowitz "Data Structures" but I'm sure there's an answer on Google as well.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Bookmarks