Sample Programming Assignment
Chapter 7 Assignment
Implement a program postfix.cpp which reads a file of postfix arithmetic
expressions as input, evaluates all of the expressions, and writes the
resulting answers to the standard output. The filename should be specified on
the command line as a parameter to the program.
The following function will be most helpful,
void evaluate_line(istream& input, bool& okay, double& answer)
// Precondition: is_more(input) is true.
// Postcondition: One line of the input file has been read (up to
// and including the newline character). The function has tried to
// interpret this line as a postfix arithmetic
// expression made up of nonnegative double numbers and the
// four operations + - * and /. If everything went okay, then
// the parameter answer is set to the value of this expression,
// and okay is set to true. If there were problems, then okay
// is set to false.
The implementation of this function should follow the pseudo code found in
figure 7.12 in Chapter 7 of the book. You should use the stack class provided
in stack2.h and stack2.template (which in turn uses the linked list toolkit
found in link2.h and link2.template).
You may find it useful to develop the following functions.
void validate_argc(int argc)
// Postconditions: If argc is not correct, issue an error message to cerr
// and exit the program.
void open_for_read(const char filename, ifstream& f)
// Postconditions: Open the stream f as the filename provided. If the open
// fails, issue an error memssage and exit the program.
void evaluate_stack_top(char operation, Stack<double> & numbers, bool & okay)
// Postconditions: 2 numbers are popped off of the stack, combinded using the
// operation provided and the result is pushed onto the stack. If any error
// is encountered, e.g., numbers.size() < 2, or operation is not '+', '-', '*'
// or '/', or division by 0.0 occurs, then okay is set to false otherwise okay
// is set to true.
bool is_more(istream & source)
// Precondition: input is an open istream.
// Postcondition: The return value is true if input still has more data to
// read; otherwise the return value is false.