In this lab, you will be modifying the code in
Figure 7.15
of the book. This code uses a stack to convert an infix expression to a
postfix expression. Instead of using the character stack DStack.h that the
book uses, use arraystack.cpp which contains
the template Stack that uses static arrays, as discussed in class Monday.
So you will need to modify the DStack.h include to instead use arraystack.cpp.
You will also have to change the Stack opStack
declaration to
use a character stack from our template class.
Note that this template stack class uses exception handling to indicate the
"full stack" and "empty stack" errors. You will need to modify each call to
push()
, pop()
and top()
in Figure
7.15 to be in a try/catch block. For example, on a push you would have:
try { opStack.push(token); } catch(FullStack) { cout << "Error: Stack is full. Cannot convert expression.\n"; return BLANK; }On pop and top, you would check for the EmptyStack error. Once you have made these modifications, you can remove the
assert(!opStack.empty())
line for the closing paren pop as
that is how the book checks for the EmptyStack error. Leave the
if(opStack.empty()) break
line in the "Pop remaining operators"
for loop as that is how the loop exits when it has finished popping the
remaining operators off the stack. It is not an error check, it is a stopping
condition.
To test your program, you will need to give it an infix expression and it will print the postfix form of the same expression. The book has several examples on pages 374 - 383 (section 7.5). To exit the program, give type # and hit enter instead of an expression. You can trigger the FullStack error by trying an expression with many open parens. You can trigger an EmptyStack error by giving an invalid infix expression (mismatched parens for example).
Email me your modified Figure 7.15 code. You should not have to change arraystack.cpp to complete this lab.