Given a text file, your program will determine if all the parentheses, curly braces, and square brackets match, and are nested appropriately. Your program should work for mathematical formulas and most computer programs.
Your program should read in the characters from the file, but ignore all characters except for the following: { } ( ) [ ]
The general algorithm is to use a stack to store the opening unmatched brackets. When a closing bracket is encountered, check it against the one on top of the stack (pop it off)--make sure it matches. When you are finished there should be no unmatched brackets left on the stack.
Your program should first implement a char stack. CharStack.h is provided on the website. You must supply the CharStack.cpp file that includes the implementations of the functions in the class declaration. Note that the stack elements will be stored in a string, and no variable named top is necessary. You can complete this assignment using these string functions: at(int), size(), append() or +=, and substr(int, int). Note: Do NOT use these functions: push_back, pop_back, or back.
Your Driver program must prompt the user to enter a filename. It should then try to open the file and then check it make sure the brackets all match appropriately. If they all match, the program should output a message saying so. If not, the program should output an appropriate error message.
There are three types of errors that can happen (and they can happen with any kind of bracket):
missing } : if you reach the end of the file, and there is an opening { that was never matched, like: int main () { x[size]=10;
expected } but found ) : this is a wrong closing bracket, like: {x[i]=10;)... unmatched } : this occurs if there is a closing bracket but not an opening bracket
(not even one of the wrong kind), like: int main () { x[i]=10; } }...
Powered by