Problem: Write a program, named p5.cpp that behaves in the manner describe in programming assignment 4, but which incorporates the changes listed below. As this is the first C++ program, the required changes involve transitioning from C-style I/O to C++ I/O mechanisms.
These changes do not require any change to the linked lists used to implement the stack and the free node list.
Naming: Your submitted file is to be named p5.c This will be the last pure C program of the semester.
Output: Output requirements are unchanged from programming assignment 4, 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
The above describes the majority of the behavior of the program you are to write. To reiterate and expand important points:
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.
An additional requirement is that your stack must be implemented using a singly-linked list, a struct for the entries in the linked list and the malloc() function. When the program starts execution, there will be no memory allocated for the stack, only a pointer (initialized NULL) for the stack. A second empty linked list based stack, a "free" list, with required pointer, will be provided to hold unused stack/linked-list nodes that result from popping the stack.
Push operations will involve checking the free list for any previously allocated but free nodes. If available, assign the value of the operand to be pushed to the data portion of the node, remove it from the free node list, and push it onto the stack linked-list. If no free nodes are available, allocate a new node, assign the operand to be pushed to the data member of the node, and push it onto the stack. Pop operations will remove a node from the stack linked list, push it onto the free list, and return the data value of the node to the caller.