FAQ | Newsboard | Telnet | Email TA | Lectures | Assignments | Man pages | Help |
---|
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.
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:
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
Follow these instructions for turning in assignment one.