// FILE: 00ab4ANSI.cpp (ANSI screen codes must be installed) // PURPOSE: 2000 AB Exam: Question 4 //----------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp, apstack.h, apstack.cpp // 2. COMMAND LINE: g++ (or gxx) 00ab4ANSI.cpp //----------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Print out 2000 AB Exam Question 4 and implement your solutions // to part a and part b as specified //----------------------------------------------------------------------- // These commands mimic Borland/Inprise conio.h screen control functions // 1. In Linux, the commands work as is // 2. In Windows systems, DEVICE=C:\WINDOWS\COMMAND\ANSI.SYS must be added // to config.sys file. NOTE: **never** change your config.sys file unless // you **really**, ***really*** know what you're doing. //----------------------------------------------------------------------- #define ESC "\x1b[" #define gotoxy(x, y) cout << ESC << y << ";" << x << "H" #define clrscr() cout << ESC "2J" #define clreol() cout << ESC "K" #define up(rows) cout << ESC << rows << "A" #define down(rows) cout << ESC << rows << "B" #define right(cols) cout << ESC << cols << "C" #define left(cols) cout << ESC << cols << "D" //----------------------------------------------------------------------- #include #include #include #include "apstack.h" struct AdviceNode { string QorA; AdviceNode *yes; AdviceNode *no; AdviceNode(const string &s, AdviceNode *yes2 = NULL, AdviceNode *no2 = NULL); }; bool IsQuestionNode(AdviceNode *); //================================================== // Part a. Complete GiveAdvice(), as started below. //================================================== void GiveAdvice(AdviceNode *T) { char ch; cout << "Want to see a romantic movie?: "; cin >> ch; cout << "\tGiveAdvice() not installed...\n"; } //================================================= // Part b. Complete TracePath(), as started below. //================================================= bool TracePath(AdviceNode *T, const string &movie, apstack &pathStack) { cout << "\tTracePath() not installed...\n"; } void testGiveAdvice(AdviceNode *); void testTracePath(AdviceNode *); void buildTree(AdviceNode *&); void printTree(AdviceNode *T, int level, int mid); int main() { string title("2000 AB Question 4"); clrscr(); gotoxy(40 - title.length()/2, 1); cout << title << endl << endl; AdviceNode *tree; buildTree(tree); testGiveAdvice(tree); testTracePath(tree); return 0; } //==================================================== AdviceNode::AdviceNode(const string &s, AdviceNode *yes2, AdviceNode *no2) : QorA(s), yes(yes2), no(no2) {} void buildTree(AdviceNode *&T) { AdviceNode *Y1 = new AdviceNode("Casablanca"); AdviceNode *Y2 = new AdviceNode("Titanic"); Y1 = new AdviceNode("A classic movie?", Y1, Y2); AdviceNode *N1 = new AdviceNode("Jaws"); AdviceNode *N2 = new AdviceNode("Citizen Kane"); N1 = new AdviceNode("Suspense?", N1, N2); N2 = new AdviceNode("Star Wars"); N1 = new AdviceNode("Science Fiction?", N2, N1); T = new AdviceNode("Do you want to see a romantic movie?", Y1, N1); } bool IsQuestionNode(AdviceNode *T) { if ( T == NULL ) return false; else return ( T->yes != NULL && T->no != NULL); } void printTree(AdviceNode *T, int level, int mid) { const int ROW = 2; if ( T == NULL ) return; gotoxy(mid - T->QorA.length()/2, ROW + 2*level); cout << T->QorA; if ( IsQuestionNode(T) ) { int midLeft = mid - 40/int(pow(2, level)); gotoxy(mid - 1, ROW + 2*level + 1); cout << "/"; int midRight = mid + 40/int(pow(2, level)); gotoxy(mid + 1, ROW + 2*level + 1); cout << "\\"; printTree(T->yes, level + 1, midLeft); printTree(T->no, level + 1, midRight); } } void testGiveAdvice(AdviceNode *T) { printTree(T, 1, 40); cout << "\n\n\nPart a. Testing GiveAdvice(): Answer (Y or N)\n"; GiveAdvice(T); cin.ignore(10, '\n'); cout << "\n\nPress to continue..."; cin.ignore(10, '\n'); } void testTracePath(AdviceNode *T) { apstack S; const string TOP("top -> "), PAD(" "); clrscr(); cout << "\n\nPart b. Testing TracePath(T, \"Jaws\", S):\n"; TracePath(T, "Jaws", S); if ( S.isEmpty() ) cout << TOP << "... stack is empty\n"; else { string item; S.pop(item); cout << TOP << item << endl; while ( !S.isEmpty() ) { S.pop(item); cout << PAD << item << endl; } }//** else cout << "\nTesting TracePath(T, \"Titanic\", S):\n"; TracePath(T, "Titanic", S); if ( S.isEmpty() ) cout << TOP << "... stack is empty\n"; else { string item; S.pop(item); cout << TOP << item << endl; while ( !S.isEmpty() ) { S.pop(item); cout << PAD << item << endl; } }//** else cout << "\nTesting TracePath(T, \"Animal House\", S):\n"; TracePath(T, "Animal House", S); if ( S.isEmpty() ) cout << TOP << "... stack is empty\n"; else { string item; S.pop(item); cout << TOP << item << endl; while ( !S.isEmpty() ) { S.pop(item); cout << PAD << item << endl; } }//** else cout << endl; }