CS 113: Introduction to Computer Science II with Introduction to C

Steve Homer---Spring 1999

Homework 4---due Thursday, April 1 before 10:00 PM

Reading: Standish, Chapter 6, pages 205-240; Chapter 7, pages 253-290


The files you must submit for this homework are exactly: h4stack.h, h4stack.c, h4calc.c, h4makefile, and h4.scr.

A Stack Implementation

You will implement a Stack Data Structure that holds doubles using a linked list. It must have the following interface:
typedef double stackElementT;

typedef struct stackNodeTag {
  /* You fill this in. */
} stackNodeT;

typedef struct {
  /* Top of stack, i.e., pointer to 1st node in list. */
  stackNodeT *top;
  /* Add any other fields you need. */
} stackT;

void StackInitialize(stackT *);
void StackDestroy(stackT *);
void StackPush(stackT *, stackElementT);
stackElementT StackPop(stackT *);
int StackIsEmpty(stackT *);

Your stack must be implemented using a linked list (an array implementation of a stack will be covered in lab). A linked list for this assignment looks conceptually something like:

 |
 v
---------     ---------     ---------
| 1.2 | -+--> | 0.3 | -+--> | 4.5 |0|
---------     ---------     ---------
where each node contains some data and a pointer to the next node in the list (except for the last node whose pointer is NULL). Since a node should only be created when some data needs to be stored in the list, each node will be dynamically-allocated. Conversely, when a piece of data is removed, its node should be freed.

Also, you need to keep track of the beginning of the linked list with a pointer.

The type stackNodeT should hold the data needed for a linked list node. The type stackT should hold the field(s) you need to keep track of a linked list.

Stack Operations

Keep in mind when implementing your stack that things in a stack go on and come off of the same place, i.e., the top. This will determine how you add or remove nodes from the list.

Roughly, each stack function will operate as follows:

All functions take the stack (i.e., something of type stackT) by reference.


Only generic stack code should go in this stack module. Code specific to the calculator (below) should go in the main program file, h4calc.c.

You must submit files h4stack.h, containing the interface, and h4stack.c, containing the implementation. Don't forget to wrap your header.

A simple test program, /cs/course/cs113/current/hw4/h4stacktest.c, is available for you to test your stack module. We suggest that you compile and run your stack module with this code to help ensure your code works properly.

A Calculator

Using your implementation of a stack, implement a simple postfix calculator program. In this case, postfix means that an operation is entered after its operands. You must submit a file named h4calc.c that contains this main program.

In general terms, this stack calculator works as follows:

  1. Gets a piece of data (operand or operator).
  2. If the data is an operand, pushes it into the stack.
  3. If the data is an operator, pops as many elements from the stack as needed, performs the operation, and pushes the result on the stack.

Your calculator should provide exactly the following operations: +, -, /, x (for multiplication), neg (i.e., the string "neg" represents negation), and ^ (raise to a power). All operations require 2 operands, except negation, which takes just one.

Operands are given to the calculator as either positive or negative numbers (i.e., you can put a + or - in front of them). Thus, examining the first character is not enough to distinguish a number from an operator. Also note that numbers may start with a decimal point (e.g., .50). As the interface shows above, you will use the type double for these numbers.

When the calculation is completed successfully, the program should print out the final result. The result should be printed with the format ``%g'' which displays floating point numbers in either regular (e.g., 3.14159) or scientific notation (7.8679e+10).

Error Handling

For all errors, you should print an appropriate error message, showing the point in the expression where the error occurred (see Example Runs below) and then terminate the program. The errors you must handle are:

Note that the stack code prints something generic under error conditions (like popping from an empty stack); however, your program should prevent those errors from ever being reached. I.e., it should detect potential stack errors (by using the given stack functions) and print out its own, specific error messages.

Example Runs

Your program will not read in data (or prompt for it); instead, it will receive an expression to calculate via the commandline (see lab on Program Arguments). Once this expression has been evaluated (or an error occurs) the program terminates.

Here are some example runs (make sure each operand and operator is separated by a space):

% h4calc 45.6 2.0 / 10.97 + neg 9 0.5 ^ x .69 -
The result is -102

% h4calc -6.7 4 x + 5.7 3 - 11.2 x neg
Error with "-6.7 4 x +": not enough operands for "+"

% h4calc 76.0 45.6 89 - 5.0 x 217 + / 42 neg -
Error with "76.0 45.6 89 - 5.0 x 217 + /": attempt to divide by zero.

Because you are probably more familiar with infix notation, where operators go in between operands, note that the first test run calculates:

(neg((45.6 / 2.0) + 10.97) x (9 ^ .5)) - .69

Note that commandline arguments are strings, so you must determine whether each argument is an operator or a number. If it is an operator, perform the operation. If it represents a real number operand, you can easily convert it to a double using a C library function like atof() (see the Manual Page for atof on how to use it and what header needs to be included for its prototype).

You may assume that input will be entered correctly, in the sense that only numbers and operators will be entered on the commandline.

A Make File

Write a make file for this program that will generate the executable ``h4calc'' for the calculator program above. This will be a smaller portion of your grade for this homework, but still must be done correctly to get full credit.

You should name your make file ``h4makefile'' and submit it as part of this homework. Since the utility make normally expects the makefile to be named ``Makefile'' or ``makefile'', you will have to run the make utility with:

make -f h4makefile
Use the make file from Homework 3 as a guide for this one.

Test Runs

Your script file (h4.scr) must contain: each source code file (use "cat" to display h4calc.c, h4stack.h, h4stack.c and h4makefile, in that order), compilation (using your make file), and test runs, in that order.

You script must show 6 test runs. Start out with the 3 listed in the Example Runs section and then add 3 more of your own that demonstrate each of the operations, plus all the error handling done by the main program.