quiz.cc 660 2351 37 3011 6276243046 5433 /******************************************************* Quiz: to enable testing of simple concepts in C++ (c) 1997 Roger Hartley *******************************************************/ #include #include #include #define QUIZ(title, type, name) Quiz(#title, &name, #type, #name) void QuizMain(); // Quiz takes: // title: a string to be displayed as a header // pv: a pointer to the variable whose value is to // be tested ffor // t: a string for the type anme of the variable pv // points to // n: a string for the variable name void Quiz(const string& title, void* pv, const string& t, const string& n) { cout << title << endl << endl; ostrstream res; if (t == "int") res << *(int*)pv << '\0'; else if (t == "float") { res.setf(ios::showpoint); res.precision(2); res << *(float*)pv << '\0'; } else if (t == "char") res << *(char*)pv << '\0'; else if (t == "double") { res.setf(ios::showpoint); res.precision(2); res << *(double*)pv << '\0'; } else if (t == "short") res << *(short*)pv << '\0'; else if (t == "long") res << *(long*)pv << '\0'; else if (t == "char*") res << *(char**)pv << '\0'; string ans; cout << "What is the value of " << n << "? "; cin >> ans; while (!(ans == res.str())) { cout << "No! Try again or press ctrl-c to exit" << endl; cout << "What is the value of " << n << "? "; cin >> ans; } cout << "Congratulations! That's right." << endl; } int main() { QuizMain(); } Òæ,£ .pine-debug1XC.newsrc-opus.elhQt.ps.mo|F .screenrcri‡t.ps.BAK ¼¤  public_html¼ .pine-debug4ÒôÌŽ.lynxrcìÚ .netscape-historyM .XstartupN.epochú O.tcshrc4P.xinitrc<H Tex_Tempt\Š .cshrc.save€w(.netscape-bookmarks.htmlcc&ttt.BAKœU.cr°§ .XdefaultspÀW.uwmrcmØa.netquiz1.cc 660 2351 37 1347 6275701527 5530 //**************************************************************************// // CS177 Spring 1997 // // (c) 1997 Roger Hartley // // // //**************************************************************************// #include "quiz.cc" void QuizMain() { int value1 = 100; int value2 = 200; int value3 = 300; int xxx; if (value1 <= value2) { if (value1 <= value3) xxx = value1; else xxx = value3; } else { if (value2 <= value3) xxx = value2; else xxx = value3; } QUIZ(Nested if-else, int, xxx); } res << *(int*)pv << '\0'; else if (t == "float") { res.setf(ios::showpoint); res.precision(2); res << *(float*)pv << '\0'; } else if (t == "char") res << *(char*)pv << '\0'; else if (t == "double") { res.setf(ios::showpoint); res.prequiz2.cc 660 2351 37 1467 6275702065 5532 //**************************************************************************// // CS177 Spring 1997 // // (c) 1997 Roger Hartley // // // //**************************************************************************// #include "quiz.cc" void QuizMain() { const int ListSize = 5; int ValuesProcessed = 0; float ValueSum = 0; float Numbers[] = { 1.2, 3, 2.4, 5.6, 7.8 }; while (ValuesProcessed < ListSize) { float Value; Value = Numbers[ValuesProcessed]; ValueSum += Value; ++ValuesProcessed; } float Average = ValueSum / ValuesProcessed; QUIZ(While loop, float, Average); } oint); res.precision(2); res << *(float*)pv << '\0'; } else if (t == "char") res << *(char*)pv << '\0'; else if (t == "double") { res.setf(ios::showpoint); res.prequiz3.cc 660 2351 37 2315 6275702733 5526 //**************************************************************************// // CS177 Spring 1997 // // (c) 1997 Roger Hartley // // // //**************************************************************************// #include "quiz.cc" void QuizMain() { int LeftOperand = 127; int RightOperand = 373; char Operator = '+'; int Result; switch (Operator) { case '*': Result = LeftOperand * RightOperand; break; case '/': if (RightOperand!= 0) Result = LeftOperand / RightOperand; else { cout << LeftOperand << " / " << RightOperand << "cannot be computed:" << " denominator is 0." << endl; return 1; } break; case '+': Result = LeftOperand + RightOperand; break; case '-': Result = LeftOperand - RightOperand; break; default: cout << Operator << " is unrecognized operation." << endl; } QUIZ(Switch statement, int, Result); } 0'; string ans; cout << "What is the value of " << n << "? "; cin >> ans; while (!(ans == res.str())) { cout << "No! Try again or press ctrl-c to exit" << endl; cout << "What is the value of " << n << "? "; cin >> ans; } cout << "Congratulations! That's right." << endl; } int main() { QuizMquiz4.cc 660 2351 37 1116 6275703460 5523 //**************************************************************************// // CS177 Spring 1997 // // (c) 1997 Roger Hartley // // // //**************************************************************************// #include "quiz.cc" void QuizMain() { int i = 0; int n = 10; int Sum; for (Sum = 1; Sum < n; ++i) if (i % 2) Sum += i; QUIZ(For loop, int, Sum); } perand * RightOperand; break; case '/': if (RightOperand!= 0) Result = LeftOperand / RightOperand; else { cout << LeftOperand << " / " << RightOperand << "cannot be computed:" << " denominator is 0." << endl; return 1; } break; case '+': Result = LeftOperand + RightOperand; break; case '-':quiz5.cc 660 2351 37 1202 6276243605 5521 //**************************************************************************// // CS177 Spring 1997 // // (c) 1997 Roger Hartley // // // //**************************************************************************// #include "quiz.cc" class X { private: int x; public: X() { x = 10; } int fun(int w, int y) { return w + x * y; } }; void QuizMain() { X obj; int y = 3; int r = obj.fun(5, y + 7); QUIZ(Member access, int, r); } ': if (RightOperand!= 0) Result = LeftOperand / RightOperand; else { cout << LeftOperand << " / " << RightOperand << "cannot be computed:" << " denominator is 0." << endl; return 1; } break; case '+': Result = LeftOperand + RightOperand; break; case '-':Makefile 770 2351 37 65 6274163205 5540 # compiles any program LDLIBS=-lm -lg++ CCC = g++ ***********************// // CS177 Spring 1997 // // (c) 1997 Roger Hartley // // // //**************************************************************************// #include "quiz.cc" class X { private: int x; public: X() { x = 10; } int fun(int w, int y) { retur//**************************************************************************// // CS177 Spring 1997 // // (c) 1997 Roger Hartley // // // //**************************************************************************// #include "quiz.cc" void QuizMain() { const int ListSize = 5; int ValuesProcessed = 0; float ValueSum = 0; float Numbers[] = { 1.2, 3, 2.4, 5.6, 7.8 }; while (ValuesProcessed < ListSize) { float Value; Value = Numbers[ValuesProcessed]; ValueSum += Value; ++ValuesProcessed; } float Average = ValueSum / ValuesProcessed; QUIZ(While loop, float, Average); } oint); res.precision(2); res << *(float*)pv << '\0'; } else if (t == "char") res << *(char*)pv << '\0'; else if (t == "double") { res.setf(ios::showpoint); res.prequiz3.cc 660 2351 37 2315 6275702733 5526 //**************************************************************************// // CS177 Spring 1997 // // (c) 1997 Roger Hartley // // // //**************************************************************************// #include "quiz.cc" void QuizMain() { int LeftOperand = 127; int RightOperand = 373; char Operator = '+'; int Result; switch (Operator) { case '*': Result = LeftOperand * RightOperand; break; case '/': if (RightOperand!= 0) Result = LeftOperand / RightOperand; else { cout << LeftOperand << " / " << RightOperand << "cannot be computed:" << " denominator is 0." << endl; return 1; } break; case '+': Result = LeftOperand + RightOperand; break; case '-': Result = LeftOperand - RightOperand; break; default: cout << Operator << " is unrecognized operation." << endl; } QUIZ(Switch statement, int, Result); } 0'; string ans; cout << "What is the value of " << n << "? "; cin >> ans; while (!(ans == res.str())) { cout << "No! Try again or press ctrl-c to exit" << endl; cout << "What is the value of " << n << "? "; cin >> ans; } cout << "Congratulations! That's right." << endl; } int main() { QuizMquiz4.cc 660 2351 37 1116 6275703460 5523