/** * File: ReversePolishCalculatorMess * Authors: Wayne Snyder and Leo Reyzin (snyder@cs.bu.edu, reyzin@cs.bu.edu) * Date: 9/20/2011 * Purpose: This is a prototype of a program to evaluate postfix ("reverse Polish") expressions; the * assignment is to take this clunky, static code, and turn it into a reasonable object-oriented * program. * Platform: This was developed on Mac OS X 10.6.5 in Dr. Java * */ import java.util.Scanner; public class ReversePolishCalculatorMess { public static void main(String[] args) { double V [] = new double[26]; double S [] = new double[100]; int next = 0; Scanner sc = new Scanner(System.in); while(sc.hasNext()) { if (sc.hasNextDouble()) S[next++] = sc.nextDouble(); else { String cmd = sc.next(); if (cmd.equals("set")) { char var = sc.next().charAt(0); V[var-'a'] = sc.nextDouble(); } else { char cmdChar = cmd.charAt(0); if (cmdChar>='a' && cmdChar<='z') S[next++] = V[cmdChar-'a']; else { double op2 = S[--next]; double op1 = S[--next]; switch (cmdChar) { case '+': S[next++] = op1 + op2; break; case '-': S[next++] = op1 - op2; break; case '/': S[next++] = op1 / op2; break; case '*': S[next++] = op1 * op2; break; } } } } System.out.print("Stack: "); for (int i=0; i