CS 112
Homework 6
Due: Wednesday 3/14/2001 by 12:00 a.m.
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.
- 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 /
- A constructor, initializing the expression to the empty
string (i.e. its length is 0).
- A member function for initializing the array by reading the expression
from the standard input.
- A member function for printing the expression to the standard output.
- 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).
- Design a test driver for your class. Test your program on a few arithmetic
expressions with at least 5 operators.
- 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):
- Stack of integers: IntStack.cpp, IntStack.h
- Arithmetic expression: ArithExp.cpp, ArithExp.h
- Test driver: testAE.cpp
- Makefile
- Data file: data
- typescript
Special arrangement for this homework only
Because the midterm exam is a day after this homework is due, and
because we skipped a class due to the snow storm, I am slightly
relaxing the deadline for the submission of the final form
of this homework (the final form is the one on which you will
receive a grade). What is due by March 14 is a program that does
almost all that you are required to do, but perhaps is not fully
debugged. You can finish debugging it during the Spring break,
and submit the final, fully debugged code, on Wednesday, March
28, by midnight. However, if you do not submit anything by March 14, or if
what you submit by the March 14th deadline is far from what I am
requesting, then there will be a penalty on your grade for Hw6.
Ileana Streinu