CS 211 - Lab 7

Fall 2012

Due: Monday, October 15th, 2012 at 11:59 pm

Stack Calculator

For this lab, you are to write a program in the C programming language that will perform the operations of a stack calculator. A stack calculator uses a "stack" (which we will implement using an array) to store the operands of a mathematical expression. When an operation is performed, the last two values on the stack are removed and used as the operands, then the result of the operation is placed on the stack.

A stack typically has the following operations:

  • push - a value is placed in the stack. With our implementation, the value is placed in the "lowest" open/empty position on the array. You will be required to a push() function in your code.
  • pop - the value that was most recently pushed onto the stack is removed from the stack. You will be required to write a pop() function in your code. With our implement, the value that is in the "highest" filled position on the array is "popped" by the function. This position on the array is now considered to be open/empty.
  • top - the value that was most recently pushed onto the stack is returned. You will be required to write a top() function in your code. Thenumber of values on the stack and the positions of these values are not modified by the top operation.
  • isEmpty - determines if there are no values on the stack. Before either a pop or top operation is performed, the stack cannot be empty. You will be required to write an isEmpty() function in your code. This function is to return a true value if there are no values on the stack or return a false value if there is at least one value on the stack.
  • isFull - detemines if the array storing the stack has no open/empty positions. Before a push operation is performed, the stack cannot be full. You will be required to write an isFull() function in your code.
A stack may also have the following additional operation:
  • top & pop -this operation combines the top and pop operations mentioned above into a single operation. Often people actually implement "top & pop" but call it "pop".
For this program, we will implement our stack using an integer array of size 10. The value of 10 must be specified in a #define statement so that it can easily be changed if needed.

The input and output of the program will be given through standard input and standard output. Your program must be able to accept the following input. You may assume that each command is given on its own input line in the program (you don't have to look for multiple commands on a single line of input). You may also assume there are no white space characters before the command on any input line.

  • q : quit the program.
  • integer value : when an integer value (either positive or negative) is given on the command line, this value is to be pushed onto the stack. If the stack is currently full, given an error message and ignore the input value (i.e. don't quit your program and don't modify your stack).
  • c : when the user enters the character 'c' as input, your program is to clear the stack. All values are to be emptied from the stack.
  • = : when the user enters the character '=' as input, your program is to display to the user to top value on the stack.
  • arithmetic operation : when the user enters any of the following arithmetic operations, your program is to take the top two values off the stack, perform the operation with those values, then push the result onto the stack. If the stack does not contain 2 values, then an error message should be given to the user and the input ignored (i.e. don't quit the program and make sure the stack is put back to the same state as it was prior to the entering of the input). Note that the order of the operands is very important. The first value taken off the stack is the right-hand-side operand, while the second value taken off the stack is the left-hand-side operand. The arithemtic operations are:
    • + : addition
    • - : subtraction
    • * : multiplication
    • / : division - integer division only
    • % : modulus - remainder from an integer division
    • ^ : exponentiation (x raised to the power of y)
  • any other input is an error and should be ignored after an error message is given to the user.
For an array implementation of a stack, in addition to the array itself another variable is needed to keep track of the current position of the top of the stack. The "top of the stack" is the position of the value on the stack that was most recently pushed. Often the implementation of a stack of integers is defined as follows:
int stack[STACKSIZE];

int tosPosition; /* Top Of Stack Position */

tosPosition = -1; /* initialized to -1 to indicated the stack is empty */

As values are pushed, the Top of Stack variable is incremented. As values are popped, the Top of Stack variable is decremented.

Command Line Argument: Debug Mode

Your program is to be able to take one optional command line argument, the -d flag. When this flag is given, your program is to run in "debug" mode. When in this mode, your program is to display the values currently in the stack in proper order. When the flag is not given, this debugging information should not be displayed. One simple way to set up a "debugging" mode is to use a boolean variable which is set to true when debugging mode is turned on but false otherwise. Then usig a simple if statement controls whether information should be output.
if ( debugMode == TRUE )

printf (" Debugging Information \n");

Use of #defined Values

As is shown below in the Suggested Program Template, you should use #define statements to determine the size of the stack. This way, if you need to change the stack size, this can easily be done. In fact, using the #defined values will be one of the grading criteria items for this lab.

Program Submission

Your are to submit the programs for this lab via the Assignments Page in Blackboard.

To help the TA, name your file with your net-id and the assignment name, like:

  • ptroy1LabX.c

Submit this file via the Assignment Link for the Lab in Blackboard.

  1. In the CS 211 Web Pages in Blackboard, go to the Assignments Page
  2. Click on the link for the correct lab. This will open a web page with the title: "Upload Assignment: Lab X", where X is the number of the lab
  3. Scroll down and click on the button "Browse for Local File"
  4. Select the file that you created that contains the program. Then click OK.
  5. Repeat steps 3 and 4 for your second program.
  6. Click the submit button on the "Upload Assignment: Lab X" page.
  7. You should see the Submission History page that shows what you submitted. Verify you actually submitted the correct information.

Suggested Program Template Layout

#define STACKSIZE 10

/* stack function signatures */

void push (int stack[], int *tos, int val);

void pop (int *tos);

int top (int stack[], int tos);

int topAndPop (int stack[], int *tos);

int isFull (int tos); /* note returns a boolean value */

int isEmpty (int tos); /* note returns a boolean value */

-- Main.troy - 2012-10-10

Topic revision: r2 - 2012-10-10 - 16:52:03 - Main.troy
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
Helping Women Faculty Advance
Funded by NSF