Lab 4 - Dynamic Array Stack

Due: 5:00pm on Wednesday

The purpose of this assignment is to look at the dynamic array implementation of a stack that is given in the book and see how it can be used to convert an infix expression to a postfix expression. We will be making a minor modification to the code to support the full() check.

Download the book's code for Figure 7.15 from the author's website. Note that there are three files to download: DStack.h, DStack.cpp and Fig7-15.cpp. Due to the encoding on the author's website, you will have to save the code to disk rather than view it in the web browser. Use the following three commands for wget to retrieve the code directly to your Sleipnir account:

wget http://cs.calvin.edu/activities/books/c++/ds/2e/SourcePrograms/Chap07/Figure7.15/DStack.h
wget http://cs.calvin.edu/activities/books/c++/ds/2e/SourcePrograms/Chap07/Figure7.15/DStack.cpp
wget http://cs.calvin.edu/activities/books/c++/ds/2e/SourcePrograms/Chap07/Figure7.15/Fig7-15.cpp
The author also has the files encoded in MAC encoding. The means when you open the file in vi/vim, it will look like all the file is on one line with a ^M character at the end of each line. To use vi to edit the files, issue the following vi last-line command once you have opened the file in vi:
:e ++ff=mac
Figure 7.15 (pages 380 - 383) presents code that uses a character stack to convert an infix expression into a postfix expression. Unfortunately, the DStack.h code provided on the author's website is an integer stack. So the first thing you will need to do is edit DStack.h and change the following line from:
typedef int StackElement;
to
typedef char StackElement;
Note that if you don't make this change, the code will still compile, but all of your characters will be converted to their ASCII integer equivalent before being pushed on the stack. This will work, but is not clear. So change the StackElement to char before compiling.

This code uses seperate compilation. You must compile it with the following:

g++ -g -c DStack.cpp
g++ -g -c Fig7-15.cpp
g++ -g -o lab4 DStack.o Fig7-15.o
There are some interest coding styles to notice about the author's code. First, in DStack.cpp, he uses new(nothrow) to disable the portion of new that would throw bad_alloc. This is not recommended, but is a way to convert old code that does not use exception handling. Second, the author uses for(;;) in Fig7-15.cpp to perform infinite loops instead of while(true) or while(1). Using for(;;) is a bit of an older style of code. Remember that infinite loops should always have at least one if statement which breaks out of the loop. Third, the author makes extensive use of the assert statement instead of checking something with an if statement. The assert statement will exit the program if the given Boolean expression is false. For example, assert(!opStack.empty()) will exit the program when the stack is empty.

Lab Assignment

Modify DStack.h and DStack.cpp to add the full() function to the Stack class. full() should return true when myTop is equal to myCapacity - 1. Modify Fig7-15.cpp to check if the stack is full before using the push() operation. All calls to opStack.push(token) should be replaced with:
assert(!opStack.full());
opStack.push(token);
Compile and run your modified code. Test your program by giving it infix expressions. You can find several examples of infix expressions on pages 374 - 383 of the book. You can trigger a full stack error (which will cause the program to exit) by giving several lines of open parentheses, since it always pushes an open parenthesis. Unless you modify the maximum capacity by changing the constructor that takes an integer to have a smaller default value, it will take more than 128 open parentheses to trigger a full stack.

You can also trigger the existing empty stack check by giving an invalid infix expression such as one with more close parentheses than open parentheses.

Email me your modified code.