Source of tstruct.cpp


  1: // Filenme: TSTRUCT.CPP
  2: // Purpose: Illustrates a simple struct for representing time.

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

  7: struct Time
  8: {
  9:     int hours;
 10:     int minutes;
 11:     int seconds;
 12: }; // <-- Note the (necessary) semi-colon!

 14: void SetTime(/* out */ Time& t,
 15:              /* in */  int newHours,
 16:              /* in */  int newMinutes,
 17:              /* in */  int newSeconds);
 18: void IncrementTime(/* inout */ Time& t);
 19: void DisplayTime(/* in */ const Time& t);


 22: int main()
 23: {
 24:     cout << endl
 25:          << "This program uses a struct to "
 26:          << "represent time in military form. "   << endl
 27:          << "You should study the source code "
 28:          << "at the same time as the output. "    << endl
 29:          << endl;

 31:     Time t1 = {11, 59, 50};  // t1 initialized at time of declaration

 33:     Time t2;  // t2 has its fields assigned after declaration
 34:     t2.hours = 10;
 35:     t2.minutes = 40;
 36:     t2.seconds = 45;

 38:     Time t3;  // t3 is initialized by a call to SetTime
 39:     SetTime(t3, 12, 0, 0);

 41:     DisplayTime(t1);  cout << endl;
 42:     DisplayTime(t2);  cout << endl;
 43:     DisplayTime(t3);  cout << endl;
 44:     cout << endl;

 46:     for (int i = 1; i <= 11; i++)
 47:     {
 48:         DisplayTime(t1);  cout << endl;
 49:         IncrementTime(t1);
 50:     }

 52:     cout << endl;

 54:     return 0;
 55: }


 58: //******************************************************************
 59: void SetTime(/* out */ Time& t,
 60:              /* in */  int newHours,
 61:              /* in */  int newMinutes,
 62:              /* in */  int newSeconds)
 63: // Pre:  0 <= newHours   <= 23
 64: //       0 <= newMinutes <= 59
 65: //       0 <= newSeconds <= 59
 66: // Post: Time t is set according to the incoming parameters.
 67: // Note that this function must be called for t prior to a
 68: // call to any other function with t as parameter.
 69: {
 70:     t.hours   = newHours;
 71:     t.minutes = newMinutes;
 72:     t.seconds = newSeconds;
 73: }


 76: //******************************************************************
 77: void IncrementTime(/* inout */ Time& t)
 78: // Pre:  t has been initialized.
 79: // Post: t has been advanced by one second, with
 80: //       23:59:59 wrapping around to 00:00:00.
 81: {
 82:     t.seconds++;
 83:     if (t.seconds > 59)
 84:     {
 85:         t.seconds = 0;
 86:         t.minutes++;
 87:         if (t.minutes > 59)
 88:         {
 89:             t.minutes = 0;
 90:             t.hours++;
 91:             if (t.hours > 23)
 92:                 t.hours = 0;
 93:         }
 94:     }
 95: }



 99: //******************************************************************
100: void DisplayTime(/* in */ const Time& t)
101: // Pre:  t has been initialized.
102: // Post: t has been output in the form HH:MM:SS.
103: {
104:     if (t.hours < 10)
105:         cout << '0';
106:     cout << t.hours << ':';
107:     if (t.minutes < 10)
108:         cout << '0';
109:     cout << t.minutes << ':';
110:     if (t.seconds < 10)
111:         cout << '0';
112:     cout << t.seconds;
113: }