//////////////////////////////////////////////////////////////////////////////// // Author: Julie Petrusa (991339) // // Program 3, Due date: March 6, 2001 // // The class StackMachine gets a postfix expression from the user (containing // // single letter operands and the operators +, -, *, // // and /) and prints a sequence of instructions to // // evaluate the expression // // Fields: input // // Methods: -instructs, which inputs a postfix expression and outputs a // // sequence of instructions to evaluate the expression // // Known bugs: none // //////////////////////////////////////////////////////////////////////////////// import java.io.*; // needed in order to do io class StackMachine { //--------------------------------------------------------------------------- // fields String input; // holds input string //--------------------------------------------------------------------------- // constructor public StackMachine(String in) { input = in; // get the input string } //--------------------------------------------------------------------------- /* method instructs -inputs a postfix expression and outputs a sequence of instructions to evaluate the expression -precondition: -the postfix expression is valid -postcondition: -if postfix expression valid, a sequence of instructions to evaluate the expression is displayed -if postfix expression not valid, throws an exception -return: -void */ public void instructs() { int stackSize = input.length(); // get maximum stack size TheStringStack newStack = new TheStringStack(stackSize); // stack System.out.println(""); int i = 1; // counter for TEMPn for(int j=0; j Enter a postfix expression"); System.out.println(" 2 --> Exit the program"); System.out.print("Answer: "); // prompt user for answer System.out.flush(); int answer = getInt(); // read in the user's answer switch (answer) // do what the user wants { case 1: System.out.println(""); // prompt user for expression System.out.print("Enter a postfix expression: "); System.out.flush(); String in = getString(); // read in the expression StackMachine newSM = new StackMachine(in); // create SM newSM.instructs(); // display instructions Bonus newBonus = new Bonus(newSM); // create Bonus newBonus.result(); // calculate and display result break; case 2: value = false; // to end do-while loop break; default: // invalid answer, display error message System.out.println(""); System.out.println("Invalid answer!"); break; } // end switch } while (value != false); System.in.read(); // to view console } // end try catch (IOException e) { System.out.println("IOException"); } // end catch } // end main //--------------------------------------------------------------------------- /* method getString -reads in a string -precondition: -the user has typed in a valid string -postcondition: -if valid string, string is read in -if not valid string, throws IOException -return: -static */ public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //--------------------------------------------------------------------------- /* method getInt -reads in an integer -precondition: -the user has typed in a valid integer -postcondition: -if valid integer, integer is read in -if not valid integer, throws IOException -return: -static */ public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); } //--------------------------------------------------------------------------- } // end class PostfixExpressions