Problem: Write a program, named p p9.cpp , that behaves in the manner describe in programming assignment 8, but which incorporates the changes listed below. First, update the rpn8.h file to define the rpn9 class, and save the file as rpn9.h. Once this is done, you will add the ability to define constants as an additional feature of the rpn9 class objects. This will involve the following changes.
A second change is required for this assignment, and that is to implement the operand stack with an instance of the C++ stack container class. Note that though this will simplify several aspects of your program, it will complicate one aspect. When the dump operator is read from stdin , you must still print the contents of the stack to stdout . This is not a directly supported operation of the C++ stack container. While it is possible to use the stack container directly to replace the prior stack implementations, obviating the need for the header file that will contain your definition of your stack (to be named stack9.h ) you must still create that file and provide all the functionality for your operand stack in that file.
A suggestion of one way to deal with the dump operator is to create a second stack container , pop values from your operand stack object, output them and place them on this second stack container , repeating until done, then iteratively pop the operands from the second stack , pushing them onto the operand stack , discarding the second stack object when done. You may, however, implement this as you see fit, so long as you use a standard C++ stack container for your operand stack.
Naming: Your submitted files are to be named p9.cpp , rpn9.h , and stack9.h.
Output: Output requirements are basically those of assignment 8, with the changes discussed above.
Your program's normal output must be to stdout and of one of the formats following, assuming argc and argv are the usual parameters for main() and where < program_name> is argv[0] .
If argv[1] is "--help ", display the following.
Usage: < program_name>
"< program_name> --help"
display this usage material.
"< program_name> -rpn"
The program accepts input from standard input as a sequence
of numbers and operators. The numbers (operands, as
integers or floating point numbers) read are push ed on a
stack until needed. When an operator is read, the required
operands are popped from the stack and used to perform the
calculation, with the result placed o n the stack. Valid
operators are +, -, * and /, are interpret ed as addition,
subtraction, multiplication and division, respectively, as
described below. An additional operator is =, which
indicates that the value at the top of the stack is popped
from th e stack and displayed along with the number of
values remaining on the stack, whereupon the program
terminates.
Stack underflows generate an error message and halt the
program, as do a stack overflows. Unrecognized input
tokens produce error messages and result in program
termination, as do unrecognized command line arguments.
The size of the stack is 10.
Stack operations are performed so as to produce results
identically as indicated here.
+ : push(pop() + pop());
- : temp = pop(); push(pop() – – temp;
* : push(pop() * pop());
/ : temp = pop(); push(pop() / temp;
' : push(1.0 / pop());
~ : push(-pop());
** : temp = pop(); push(pow(pop(),temp));
swap : t1 = pop(); t2 = pop(); push(t1); push (t2);
copy : temp = pop(); push(temp); push(temp);
= : pop stack top and display it as the result with the number of stack element s remaining.
dump : display the contents of the calculator stack.
peek : display the top-of-stack value.
peek("< text>") : display the top-of-stack value following the specified text.
=< variable> : pop the value from the top of the stack and Assign that value to the name specified.
:=< constant> : pop the value from the top of the stack and Assign that value to the name specified if not Previously defined.
< variable> : push the value of the variable onto the stack.
< constant> : push the value of the constant onto the stack.
Variable and constant n n ames must be alphanumeric, both upper- and lower-case permitted, and must start with an alphabetic character.
Upon completing evaluation of an expression read from stdin (using cin ), output to stdout (using cout ) is the same as for previous assignments:
Result = < result popped from top of stack>.
< number of operands on the stack> values remain on the stack.
For all error conditions, print the required output to stderr.
Remember that any item delimited by angle brackets ("< >") is to be replaced with the appropriate information, without the angle brackets.