Problem: Write a program, named p7.cpp that behaves in the manner describe in programming assignment 6, but which incorporates the changes listed below. As this is a C++ program, implementations of new features should use C++ conventions, libraries and I/O, except as specifically noted below.

  • Add support for declaring variables and using the variables declared in performing subsequent calculations.
    • The syntax for declaring a variable is "=< variable name>". Note that this use of the '=' symbol is to perform an assignment of the value at the top of the stack to the name given in the assignment operation, not the operator used to terminate operation of the program and return results.
    • A valid variable name is composed of alphanumeric characters, with the first character an alphabetic character. Both upper- and lower-case alphabetic characters are permissible, and variable names are case-sensitive.
    • All operators defined in prior assignments are to be treated as reserved words, and thus cannot be used as variable names.
    • When an assignment is read from the stdin input stream, the name is to be checked for prior declaration. If previously declared, pop the value from the top of the stack and assign that value to the variable. If it has not been previously declared, add the name to the list of declared variables, pop the value from the top of the stack and assign that value to the newly declared variable.
    • Attempts to declare a variable when the stack is empty results in a stack underflow, which must already be treated as an error condition as per prior assignments.
    • When a declared variable name is encountered in the input stream, push the value of the variable onto the stack.
    • Handling of the name of the variable may, as the sole exception, be handled using the cstring library functions.
  • You are to implement retention of the list of declared variables as a circular doubly linked list of structures. These structures must contain space for a string to contain the name of the variable, a double allocation to hold the assigned value, and forward and backward links to other structures of the same type.
    • A circular doubly linked list is a linked list that has the head and the tail linked to a header node that is not itself part of the linked list. The advantage of this added node is that if you are searching for a particular item in the linked list, you can place that items identifier in the header node, then search the list without checking for the end of the list, since the value will always be found in the header, which is the "next" node for the tail of the list. Failure can be tested for by checking whether the node the item was found in was the header node. Being doubly linked, the list can be traversed from either end, and you can change direction of traversal at need.

Naming: Your submitted file is to be named p p7.cpp. This will be the last pure C program of the semester.

Output: Output requirements are unchanged from programming assignment 6, in the sense that what is displayed is not changed, even though the method of output is required to follow C++ conventions using C++ objects and libraries. Thus, the following output requirements are unchanged.

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.
< variable> : push the value of the variable onto the stack. Variable names must be alphanumeric, both upper-a and lower-case permitted, and must start with an alphabetic character.

The above describes the majority of the behavior of the program you are to write. To reiterate and expand important points:

  • If the first command-line argument is "--help" subsequent command-line arguments are ignored, the usage message is displayed and the program terminates.
  • If the first command-line argument is "-rpn" subsequent command line arguments are ignored and the program proceeds with reading from standard input.
  • Invalid/unrecognized command-line arguments produce a suitable error message, display the usage information above, and terminate the program.
  • If an unrecognized token is read from standard input, a suitable error message for the type of error is to be displayed with the usage text and the program terminated.
  • A malformed peek() operand is an error, and should be reported as such, and in the same manner as other error messages.
  • If all inputs are recognized and no errors arise, print the following to stdout.
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.