CS140 -- Lab 6


Lab Objective

This lab is designed to give you more experience with stacks and dllists.


Lab Materials


mydc

Write mydc which is an arithmetic calculator that operates on integers. The overall structure of mydc is a stacking (reverse Polish) calculator.

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

Program Requirements

  1. You should use your stack library from the previous lab to implement your stack.
  2. It is fine to use scanf to read each input item, since you will be reading items one at a time and figuring out what to do with them.
  3. You will need to add a clear function to your stack library. You should not add a print function to your stack because the value field points to a void * and hence your stack library will not know what to print. You will need to use the same strategy for printing a stack that you used in print_stack in the previous lab.
  4. You will be storing integers on the stack. Your stack library should store void *'s in the value field so you can use the following trick to store integers on the stack. Instead of passing a pointer to an integer to your push routine, simply pass an integer and cast it to a void *. Similarly, when you pop a value or look at the top of the stack, cast the return value to an int. This trick works because casts do not change the actual series of bits that is stored in a memory location so the integer representation of the integer that gets pushed on the stack will be maintained, even though it is being cast to a void *.

Design Document Requirements

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                                       14
Here 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

What to Hand In

Your design document will be due at the end of the lab. You must hand it in to the TA before you leave. You should submit the following files:

  1. stack.c
  2. stack.h
  3. mydc.c