1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream>
using std::cout;
using std::endl;
// struct definition
struct tod
{
int hour; // the hour, 0-23
int minute; // the minute, 0-59
int second; // the second, 0-59
char descr[32]; // the description of the time of day
};
// function prototypes
void coutTod(const tod*);
void coutTod(int, const tod[]); // n = number of items in the array
long diffTod(const tod*, const tod*);
int isAfter(const tod*, const tod*); // true if 1st is after 2nd
int isBefore(const tod*, const tod*); // true if 1st is before 2nd
int main()
{
// declare and initalize the time to noon
tod theTime[] = {{12, 0, 0, "noon "},
{0, 0, 0, "midnight "},
{11,30, 0, "lunch time "},
{18, 45, 0, "supper time "},
{23, 59, 59, "bed time "}};
// output user information
cout << endl;
// call function to display elements of struct
// output the time of day and numeric value
coutTod(5, theTime);
cout << endl;
// output the difference of two times of day
cout << "From " << theTime[1].descr << "to " << theTime[4].descr <<
"the difference is " << diffTod(theTime[1], theTime[4]) << " seconds" << endl;
cout << "From " << theTime[2].descr << "to " << theTime[3].descr <<
"the difference is " << diffTod(theTime[2], theTime[3]) << " seconds" << endl;
cout << "From " << theTime[0].descr << "to " << theTime[1].descr <<
"the difference is " << diffTod(theTime[0], theTime[1]) << " seconds" << endl;
cout << endl;
// comparing noon and lunch time
if (isAfter(theTime[0], theTime[2]) == 0)
cout << theTime[0].descr << "is not after " << theTime[2].descr << endl;
else
cout << theTime[0].descr << "is after " << theTime[2].descr << endl;
// comparing midnight and bedtime
if (isBefore(theTime[1], theTime[4]) == 0)
cout << theTime[1].descr << "is before " << theTime[4].descr << endl;
else
cout << theTime[1].descr << "is not before " << theTime[4].descr << endl;
// comparing supper time and bedtime
if (isBefore(theTime[3], theTime[4]) == 0)
cout << theTime[3].descr << "is before " << theTime[4].descr << endl;
else
cout << theTime[3].descr << "is not before " << theTime[4].descr << endl;
return 0;
}
// calls the function to display the struct
void coutTod (int n, const tod t[])
{
for (int i = 0; i < n; i++)
coutTod(&t[i]);
}
// formats the output of the struct
void coutTod(const tod* t)
{
cout << t->descr << t->hour << ":";
if (t-> minute < 10)
cout << '0';
cout << t->minute << ':';
if (t->second < 10)
cout << '0';
cout << t->second << endl;
}
// computes the difference in seconds of two times of day
long diffTod(const tod* t1, const tod* t2)
{
long time_dif = ((long)(t1->hour - t2->hour) * 3600L)
+ ((long)(t1->minute - t2->minute) * 60L)
+ (long)(t1->second - t2->second);
return time_dif;
}
// computes whether the time of day is after the first time
int isAfter(const tod* time1, const tod* time2)
{
if (diffTod(time1, time2) < 0)
return 0; // if first time is not after
else
return 1; // if first time is after
}
// computes whether first time of day is before the second
int isBefore(const tod* time1, const tod* time2)
{
if (diffTod(time1, time2) < 0)
return 0; // if first time is before the second
else
return 1; // if first time is before the second
}
|