#include "utmutility.hpp" //Special version of the utility file #include "Stack.hpp" int main() { Stack openings; char symbol; bool is_matched = true; ifstream dataIn; dataIn.open("assignment2.txt"); file_check(dataIn); while(!dataIn.eof()) { dataIn >> symbol; while(is_matched && symbol != '\n') { if(symbol == '{' || symbol == '(' || symbol == '[') openings.push(symbol); if(symbol == '}' || symbol == ')' || symbol == ']') { if(openings.empty()) { cout << "Unmatched closing bracket " << symbol << " deteced." << endl; is_matched = false; } else { char match; openings.top(match); openings.pop(); is_matched = (symbol == '}' && match == '{') || (symbol == ')' && match == '(') || (symbol == ']' && match == '['); if(!is_matched) cout <<"Bad match" << match << symbol << endl; } } } if(!openings.empty()) cout << "Unmatched opening bracket(s) detected." << endl; } pause(); return 0; } #ifndef UTMUTILITY_HPP #define UTMUTILITY_HPP #include // defines cout and cin #include // file input/output #include // useful io manipulator functions setw etc. #include // conversion routines and system() calls #include // math functions like pwr(x,y) #include // char functions #include // string functions #include // numeric limits #include // C library language support #include // date and time functions // all above from current Standard ANSI/ISO C++ using namespace std; // allows us to use the ANSI/ISO functions without // the :: notation // function prototypes void file_check( ifstream &); //From Roux -- accepts filename and stops // program if file cannot be opened. void pause(); //From Roux -- pauses program and waits for a // key to be pressed. void clrscr(); //From Roux -- clears the command window. bool user_says_yes(); //From Kruse & Ryba -- asks for y/n answer. enum Error_code {success, fail, range_error, underflow, overflow, fatal, not_present, duplicate_error, entry_inserted, entry_found, internal_error}; #endif #include "utmutility.hpp" ////////////////////////////////////////////////////////////////// void file_check( ifstream &filename ) /// Stops program if no input file { if (!filename) { cout << "Problem opening input file." << endl; pause(); // pause function must be here exit(1);// Halt the program } }// end of file_check function void pause() ///// Pause until a key is pressed. { system("pause"); // from cstdlib }// end of pause() void clrscr() //// Clear the screen { system("cls"); // from cstdlib } // end of clrscr() bool user_says_yes() // function is from Kruse & Ryba, Appendix C { int c; bool initial_response = true; do { // Loop until appropriate input is received. if (initial_response) cout << " (y/n)?" << flush; else cout << "Respond with either y or n: " << flush; do { // Ignore white space c = cin.get(); } while (c == '\n' || c == ' ' || c == '\t' ); initial_response = false; } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N' ); return (c == 'y' || c == 'Y'); } // end of user_says_yes() #ifndef STACK_HPP #define STACK_HPP #include "utmutility.hpp" const int maxstack = 500; typedef char Stack_entry; class Stack{ public: Stack(); bool empty() const; Error_code pop(); Error_code top(Stack_entry &item) const; Error_code push(const Stack_entry &item); private: int count; Stack_entry entry[maxstack]; }; // class #endif #include "Stack.hpp" Stack :: Stack() /*Pre: None. Post: The stack is initialized to be empty.*/ { count = 0; } bool Stack :: empty() const /*Pre: None. Post: If the Stack is empty, true is returned. Otherwise false is returned.*/ { bool outcome = true; if(count > 0) outcome = false; return outcome; } Error_code Stack :: push(const Stack_entry &item) /*Pre: None. Post: If the Stack is not full, item is added to the top of the Stack. If the Stack is full, an Error_code of overflow is returned and the Stack is left unchanged.*/ { Error_code outcome = success; if(count >= maxstack) outcome = overflow; else entry[count++] = item; return outcome; } Error_code Stack :: pop() /*Pre: None. Post: If the Stack is not empty, the top of the Stack is removed. If the Stack is empty, an Error_code of underflow is returned.*/ { Error_code outcome = success; if(count == 0) outcome = underflow; else --count; return outcome; } Error_code Stack :: top(Stack_entry &item) const /*Pre: None. Post: If the Stack is not empty, the top of the Stack is returned in item. If the Stack is empty an Error_code of underflow is returned.*/ { Error_code outcome = success; if(count == 0) outcome = underflow; else item = entry[count - 1]; return outcome; }