This lab is designed to give you more experience with stacks and dllists.
Input comes from standard input. The following input constructs are recognized:
number Push a number onto the stack. + - / * % The top two values on the stack are: added (+), subtracted (-), multiplied (*), divided (/), or remaindered (%). The two entries are popped off the stack and the result is pushed in their place. In the case of subtraction, division, and remaindering, the topmose value is the rightmost value in the expression. For example, if 3 and 7 are the top two numbers on the stack with 3 being the topmost element, then the subtract command will produce 4 (7-3), the divide command will produce 2 (7/3), and the remainder command will produce 1 (7%3). d Duplicate the top value on the stack. p Print the top value on the stack. The top value remains unchanged. f Print all values on the stack. q Exit the program. c Clear the stack (i.e. pop off all values)Here are some example input sequences:
INPUT: 5 6 + p OUTPUT: 11 INPUT: 3 4 5 - * p 2 - p OUTPUT: -3 -5 INPUT: c p f OUTPUT: main stack: empty main stack: empty INPUT: + OUTPUT: not enough operands INPUT: + 1 2 + p OUTPUT: not enough operands 3 INPUT: c 1 2 3 4 f * + p OUTPUT: main stack: 1 2 3 4 14
The design for this program is relatively simple since the only major data structure you need to use is a stack and the stack will be storing integers. To give you some insight into how the stack should be manipulated as you process the input, you should trace the following sets of input and for each token, either show what the stack looks like after the token is processed, if the token is a number or operator, or else show what the output will look like if the token is a print operator. Here is an example that both traces an input sequence and shows how you should format your trace:
INPUT: 2 3 + c 1 2 3 4 f * + p Token Stack Output 2 2 3 2 3 + 5 c empty 1 1 2 1 2 3 1 2 3 4 1 2 3 4 f main stack: 1 2 3 4 * 1 2 12 + 1 14 p 14Here are the input sequences you should trace (if an error occurs, print an error message and continue):
INPUT: 8 3 % 8 2 / + p d - INPUT: 6 d f * p c 3 5 - 4 5 f INPUT: 8 5 + * p 8 * % 6 d d * - f c 4 5 % p