CS202 Computer Science II
FAQ Newsboard  Telnet Email TA Lectures Assignments Man pages Help

Assignment 1
Due September 16, 1998
Turnin instructions

Last modified Sep 4, 1998

You will learn about data abstraction, modules, and separate compilation in this assignment.

Design and implement a program that reads characters from standard input, ignores all characters except parentheses, and ensures that every open parenthesis is matched by a corresponding closed parenthesis. You will use a stack to ensure this correspondence. Put another way, the program only deals with "(" and ")" (ignoring other characters that it may read) and ensures that each open parenthesis, "(", has a matching closing parenthesis, ")" and it uses a stack to keep track of matching opening and closing parentheses.

A stack is an abstract data structure. Its distinguishing property is that the last item you put in is the first item you can take out. We will talk a little bit about stacks in class. For this assignment you don't need to implement a stack, you just need to use one.

The program itself is fairly simple. Every time you see an open paren "(", push it on the stack, every time you see a close paren ")" , pop the top off the stack. If you can't pop from the stack, then you have mismatched parens. If you've seen all the input and the stack is empty, everything matched.

Did you know that your compiler uses such an algorithm to make sure that all parens and curly braces match ? Easy isn't it ?!

Here are the functions that allow you to manipulate a stack.

  1. void InitStack(void) which "creates" and initializes the stack. You may assume that there exists only one stack in your universe. You must call (invoke) this function once and only once and it shold be called (invoked) before you use any other stack manipulation function.

  2. int Empty(void) which returns true if the stack is empty and false otherwise.

  3. int Full(void) which returns true if the stack is full and false otherwise.

  4. void Push(char ch) which pushes ch on the stack. The user must check if the stack is full before trying to push items on the stack

  5. char Pop(void) which returns the item popped off the stack. The user must check if the stack is empty before trying to push items on the stack

  6. void StackPrint(void) which prints the current contents of the stack from the top of the stack to the bottom. It should indicate which is the item at the top of the stack. Look at the sample output.

Please include the following line at the beginning of your program file.

#include "stack.h"
Save the file stack.h to your assignment 1's directory. You will also need one of the object files in the same directory. Save one of these to a file named stack.o in the same directory. Save files by clicking the right mouse button and using the "save link as" option in netscape. (This is especially useful for binary files not designed to be displayed in your browser).

stack.o contains my implementation of the stack manipulation functions above.
The first few lines of your program will therefore look like this

#include <iostream.h>
#include "stack.h"

int main(void)
{

...

}
Here are some constraints on your program:
  1. Print the contents of the stack using StackPrint() after every operation on the stack.

  2. You may assume that your stack can never contain more than 100 items at a time.

  3. Your program stops when it sees an EOF, end-of-file.


Compiling and Running

I have also supplied a makefile to help manage program files. It assumes you will put your code in a file named as1.cpp and ensure that stack.o, stack.h, and makefile are in the same directory as as1.cpp.

Now you just need to type make at the unix prompt. The make command will compile, and link your source files and make an executable named paren.

Here's an example of compiling and running paren:

dws010:20 as1> make
g++ -c -Wall as1.cpp
g++ -o paren -Wall stack.o as1.o
dws010:21 as1> paren
((((())                         <------- my input
top-> (  <-bot
top-> ( (  <-bot
top-> ( ( (  <-bot
top-> ( ( ( (  <-bot
top-> ( ( ( ( (  <-bot
top-> ( ( ( (  <-bot
top-> ( ( (  <-bot
)))                             <------- my input
top-> ( (  <-bot
top-> (  <-bot
top->  <-bot
Parens Matched

Turning it in

Follow these instructions for turning in assignment one.