Source of testfrac.cpp


  1: // Filename: TESTFRAC.CPP
  2: // Purpose:  Tests some features of the Fraction class.

  4: #include <iostream>
  5: #include <iomanip>
  6: using namespace std;


  9: #include "FRACTION.H"
 10: #include "MENU.H"
 11: #include "TXITEMS.H"
 12: #include "PAUSE.H"


 15: void DisplayFractionTests(const TextItems&);


 18: int main()
 19: {
 20:     Menu m("Main Menu");
 21:     m.AddOption("Quit");
 22:     m.AddOption("Get information");
 23:     m.AddOption("Display specific fraction tests");

 25:     TextItems testfracText("testfrac.dat");
 26:     int menuChoice;

 28:     do
 29:     {
 30:         m.Display();
 31:         menuChoice = m.Choice();

 33:         switch (menuChoice)
 34:         {
 35:             case -1:
 36:                 cout << "\nProgram now terminating.\n\n";
 37:                 break;

 39:             case  1:
 40:                 cout << endl;
 41:                 break;

 43:             case 2:
 44:                 testfracText.DisplayItem("Program Description");
 45:                 break;

 47:             case 3:
 48:                 DisplayFractionTests(testfracText);
 49:                 break;
 50:         }

 52:     } while (menuChoice != 1  &&  menuChoice != -1);
 53:     cout << endl;

 55:     return 0;
 56: }



 60: void DisplayFractionTests(/* in */ const TextItems& testfracText)
 61: // Pre:  "testfracText" has been initialized
 62: // Post: A series of tests has been performed on the Fraction
 63: //       class, one of them with the aid of some input from the
 64: //       user, and the results of the tests have been displayed.
 65: //       Two of the tests are optional and may or may not be
 66: //       performed, at the discretion of the user.  Either test,
 67: //       if successful, will lead to immediate termination of
 68: //       the program, so two separate runs of the program are
 69: //       necessary to complete both of these tests, which are
 70: //       the last two performed.
 71: {
 72:     testfracText.DisplayItem("Test Info: Part 1");

 74:     char response;
 75:     cout << "Would you like to check your constructor? (Y/N): ";
 76:     cin >> response;  cin.ignore(80, '\n');  cout << endl;
 77:     if (response == 'Y'  ||  response == 'y')
 78:     {
 79:         Fraction fTest1(1,0);
 80:         cout << "Fraction value is: " << fTest1 << endl;
 81:         Pause(0);
 82:     }

 84:     cout << "Would you like to check your Set function? (Y/N): ";
 85:     cin >> response;  cin.ignore(80, '\n');  cout << endl;
 86:     if (response == 'Y'  ||  response == 'y')
 87:     {
 88:         Fraction fTest2;
 89:         fTest2.Set(1,0);
 90:         cout << "Fraction value is: " << fTest2 << endl;
 91:         cout << fTest2 << endl;
 92:         Pause(0);
 93:     }


 96:     testfracText.DisplayItem("Test Info: Part 2");
 97:     cout << endl;

 99:     cout << "And here are the corresponding values "
100:          << "according to *your* Fraction class: "   << endl;

102:     Fraction f11(3,4), f12(-3,-4), f13(15,20), f14(-9,-12);
103:     cout << f11 << "  " << f12 << "  " << f13 << "  " << f14 << endl;

105:     Fraction f21(3,1), f22(3);
106:     cout << f21 << "  " << f22 << endl;

108:     Fraction f31(-3,1), f32(-3);
109:     cout << f31 << "  " << f32 << endl;

111:     Fraction f4;
112:     cout << f4 << endl;

114:     Fraction f51, f52, f53, f54;
115:     f51.Set(3,4);  f52.Set(-3,-4);  f53.Set(15,20);  f54.Set(-9,-12);
116:     cout << f51 << "  " << f52 << "  " << f53 << "  " << f54 << endl;

118:     Fraction f61, f62;
119:     f61.Set(3,1);  f62.Set(3);
120:     cout << f61 << "  " << f62 << endl;

122:     Fraction f71(-3,1), f72(-3);
123:     cout << f71 << "  " << f72 << endl;

125:     Fraction f8;
126:     f8.Set();  // Note that Set called with no parameters *requires*
127:                // the parentheses, *unlike* the constructor with no
128:                // parameters.  Compare the declaration of f4 above.
129:     cout << f8 << endl;
130:     Pause(0);


133:     testfracText.DisplayItem("Test Info: Part 3");
134:     Fraction g11, g12, g13, g14, g21, g31;
135:     cin >> g11 >> g12 >> g13 >> g14 >> g21 >> g31;
136:     cin.ignore(80, '\n');  cout << endl;


139:     testfracText.DisplayItem("Test Info: Part 4");
140:     cout << "And here are the actual values as read in "
141:          << "and then displayed by your program: "    << endl;
142:     cout << g11 << "  " << g12 << "  " << g13 << "  " << g14 << "  "
143:          << g21 << "  " << g31 << "  " << endl;
144:     Pause(0);
145: }