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 constant will be declared by a token of the form ':=< constant_name>' in the input stream. Constant names will be of the same form as variable names in prior assignments.
    • Constant names must start with an alphabetic character, either upper or lower case, and may contain alphanumeric characters starting with the second character.
  • The operation of the declaration of a constant is similar to that of a variable, but not identical. Variables may have their values changed, constants cannot. Therefore when a constant declaration is read, the constant name will be extracted and the list of variable and constant names checked to see if this name has been previously defined. There are three possible cases.
    • The name is not listed as previously defined. Therefore, the top of the stack will be popped and the value assigned to the constant, and the constant added to the list of named constants and variables.
    • The name was previously defined as a variable. Therefore, report that there is a name conflict with a previously defined variable as an error, then exit the program.
    • The name was previously defined as a constant. Therefore, report that the name was previously defined, and that constants cannot have their values changed, then exit the program.
    • Note that the error messages for the last two cases must reflect the difference in the problem encountered. Likewise, attempts to declare or assign a new value to a variable name that conflicts with a constant name must be handled as an error. You are therefore to recognize and report this error as a variable / constant name conflict, then exit the program.
  • The linked list of variables will now be a list of variables and constants. As such, the nodes in this linked list must now incorporate one additional element, which is a Boolean value indicating that the name is defined as a constant if true, and a variable if false. Definition of variables and constants must therefore set this field correctly when creating a node to be placed in the linked list of variables and constants.

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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.