CS 112
Homework 6
Due: Wednesday 3/10/2004 by 10:00 p.m.
(Notice earlier submission time)

Before you start working on the homework

Look at the applet for arithmetic expressions, to see how the stack expands when an expression is being evaluated. To see what expression is currently being evaluated, open the Java Console (in Netscape, go to Communicator/Tools/Java Console). When you click on New in the applet, a new expression is chosen to be evaluated. These expressions are exactly those on which you will be testing your program for this homework.

To Do

Implement in C++ a class ArithExpr for a very restricted form of arithmetic expressions in postfix form. The class should have the following structure.

  1. As private members of the class, an array of characters (where the arithmetic expression is stored as a sequence of characters) and its length. Your expressions will be built up from atoms which are "numbers" made of only one decimal digit (0 through 9) and the arithmetic operators +, -, * and /
  2. A constructor, initializing the expression to the empty string (i.e. its length is 0).
  3. A member function for initializing the array by reading the expression from the standard input.
  4. A member function for printing the expression to the standard output.
  5. A member function for evaluating the arithmetic expression, using the algorithm described in class. This should make use of one of the Stack classes that you created and tested in Hw 5.

    Careful: the expression is a sequence of characters, but the values you push on the stack, some resulting from evaluating a subexpression of an arithmetic expression, are integers. You might have to do some type casting here (converting a character representing a digit into the appropriate integer value). We will discuss this in class next Tuesday, if there are confusions about this part. Meanwhile, you can implement this part with a simple switch statement, testing what character you read (e.g. the character '1') and pushing on the stack the corresponding integer (the integer 1, in this case).
  6. Design a test driver for your class. Test your program on a few arithmetic expressions with at least 5 operators.
  7. To simplify the interface, I will provide the test data. The data file is available here. Notice that each arithmetic expression ends with a dot, and the file ends with a dot on one line (i.e. when you read in an expression and its length is 0, you know it is the end).
Submit in Hw6 all the files: sources, Makefile and a typescript. See below for naming conventions. Test your program on all the arithmetic expressions illustrated by the applet shown in class (look in the Java console).
Naming conventions and files to submit (in Hw6):

Ileana Streinu