import ch03.stacks.*; import java.util.Scanner; public class PostfixEval { public static double postfixEval(String expr) { UnboundedStackInterface stack = new LinkedStack(); Scanner getInput = new Scanner(expr); double operand1, operand2; while (getInput.hasNext()) { // could be a value or an op if (getInput.hasNextDouble()) // its a value; push it stack.push(getInput.nextDouble()); else { // its an op; pop, pop, eval, push operand2 = stack.top(); stack.pop(); operand1 = stack.top(); stack.pop(); switch (getInput.next()) { // what is the operator ? case "+": stack.push(operand1 + operand2); break; // java 1.7 case "-": stack.push(operand1 - operand2); break; case "*": stack.push(operand1 * operand2); break; case "/": stack.push(operand1 / operand2); break; } } } return stack.top(); // auto convert Double to double } }