/****************************************************************************** * Compilation: javac EvaluatePostfix.java * Execution: java EvaluatePostfix < file.txt * Dependencies: Stack.java StdIn.java * * Evaluates postfix expresions using a stack. * * Windows users: replace with so signify end of file. * * % java EvaluatePostfix * 3 4 5 + * * * 27 * * % java EvaluatePostfix * 1 2 3 4 5 * + 6 * * + * * 277 * * % java EvaluatePostfix * 7 16 16 16 * * * 5 16 16 * * 3 16 * 1 + + + * * 30001 * * % java EvaluatePostfix * 7 16 * 5 + 16 * 3 + 16 * 1 + * * 30001 * * Known bugs * ---------- * - No error checking - assumes input is legal postfix expression. * - All token must be separated by whitespace, e.g., 1 5+ is illegal. * ******************************************************************************/ public class EvaluatePostfix { public static void main(String[] args) { Stack stack = new Stack(); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals("+")) stack.push(stack.pop() + stack.pop()); else if (s.equals("*")) stack.push(stack.pop() * stack.pop()); else stack.push(Integer.parseInt(s)); } StdOut.println(stack.pop()); } }