#include "stdstuff.h" #include "LStack.h" void analyze (String2002 str) { ListStack stack; int i; for (i = 0; i < str.length(); i++) { switch (str[i]) { case '(': stack.push('('); break; case '{': stack.push('{'); break; case '[': stack.push('['); break; case ')': if (stack.isEmpty() || (stack.pop()!= '(') ) { cout << "Mismatched round bracket at position " << (i + 1) << endl; return; } break; case '}': if (stack.isEmpty() || (stack.pop()!= '{') ) { cout << "Mismatched squiggly bracket at position " << (i + 1) << endl; return; } break; case ']': if (stack.isEmpty() || (stack.pop() != '[') ) { cout << "Mismatched square bracket at position " << (i + 1) << endl; return; } break; } } if (!stack.isEmpty()) { cout << "The String2002 object contains unmatched opening brackets.\n"; } else { cout << "The String2002 object is fine.\n"; } } void realMain () { String2002 str; for (;;) { cout << "Please enter an expression (STOP to terminate): "; getline (cin, str, '\n'); if (str.isEqualCaseInsensitive("stop")) { return; } analyze (str); } } int main () { try { realMain (); } catch (exception &e) { // catches all uncaught exceptions cout << "\nException <" << e.what() << "> occurred.\n"; } pause(); return 0; }