Results 1 to 1 of 1
-
How to parse postfix arithmetic expressions
Java Code:import java.io.IOException; public class ParsePost { private Stack theStack; private String input; public ParsePost(String s) { input = s; } public int doParse() { theStack = new Stack(20); char ch; int j; int num1, num2, interAns; for (j = 0; j < input.length(); j++) { ch = input.charAt(j); theStack.displayStack("" + ch + " "); if (ch >= '0' && ch <= '9') // if a number push it theStack.push((int) (ch - '0')); else // it's an operator { num2 = theStack.pop(); num1 = theStack.pop(); switch (ch) { case '+': interAns = num1 + num2; break; case '-': interAns = num1 - num2; break; case '*': interAns = num1 * num2; break; case '/': interAns = num1 / num2; break; default: interAns = 0; } theStack.push(interAns); } } interAns = theStack.pop(); return interAns; } public static void main(String[] args) throws IOException { String input = "1-2+3*4+5/6-7+8*9"; int output; ParsePost aParser = new ParsePost(input); output = aParser.doParse(); System.out.println("Evaluates to " + output); } class Stack { private int maxSize; private int[] stackArray; private int top; public Stack(int size) { maxSize = size; stackArray = new int[maxSize]; top = -1; } public void push(int j) { stackArray[++top] = j; } public int pop() { return stackArray[top]; } public int peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize - 1); } public int size() { return top + 1; } public int peekN(int n) { return stackArray[n]; } public void displayStack(String s) { System.out.print(s); System.out.print("Stack (bottom>top): "); for (int j = 0; j < size(); j++) { System.out.print(peekN(j)); System.out.print(' '); } System.out.println(""); } } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
How to convert infix arithmetic expressions to postfix
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:36 PM -
JSP Expressions
By Java Tip in forum Java TipReplies: 0Last Post: 12-26-2007, 10:13 AM -
JSP Expressions
By Java Tip in forum Java TipReplies: 0Last Post: 12-02-2007, 09:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks