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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Incomplete version
// Starting code for student tutorial
#include <iostream>
using namespace std;
struct Mytime
{
int hours;
int mins;
};
int timecmp( Mytime, Mytime );
// compares two Mytime values
// returns 0 if times are same
// returns >0 if first is later than second
// returns <0 if first is before second
int setTime( Mytime& t, int h, int m );
// if valid time sets hh and mm to h and m
// and returns 0
// if invalid returns integer > 0 as error code
// error code +1 = underflow hours
// error code +2 = overflow hours
// error code +4 = underflow mins
// error code +8 = overflow mins
void display1Time( Mytime t );
// displays in form hh:mm
void elapsed( Mytime t1, Mytime t2, Mytime& duration );
// determines the time duration between t1 and t2
// t1 assumed to be earlier than t2
int main()
{
Mytime now;
Mytime then;
Mytime howlong;
int h,m;
do // validate input
{
cout << "Enter start hh mm : ";
cin >> h >> m ;
} while (setTime(now,h,m));
do
{
cout << "Enter finish hh mm : ";
cin >> h >> m ;
} while (setTime(then,h,m));
elapsed(now,then,howlong);
cout << "Time elapsed from ";
display1Time(now);
cout << " until ";
display1Time(then);
cout << " is ";
display1Time(howlong);
cout << endl;
return ( 0 );
}
int timecmp(Mytime t1,Mytime t2)
{
if (t1.hours == t2.hours)
{
if (t1.mins == t2.mins)
{
return 0;
}
else // mins not same
{
if (t1.mins > t2.mins)
{
return 1; // greater than zero
}
else
{
return -1;
}
}
}
else // hours not same
{
if (t1.hours > t2.hours)
{
return 1;
}
else
{
return -1;
}
}
}
int setTime(Mytime& t, int h, int m )
{
if (h < 0)
{
return 1; // error code +1 = underflow hours
}
else if (h > 23)
{
return 2; // error code +2 = overflow hours
}
if (m < 0)
{
return 4; // error code +4 = underflow mins
}
else if (m > 60)
{
return 8; // error code +8 = overflow mins
}
t.hours = h;
t.mins = m;
return 0;
}
void display1Time(Mytime t)
{
if (h > 10)
{
cout << h << endl;
}
else
{
cout << "0" << h << endl;
}
if (m > 10)
{
cout << m << endl;
}
else
{
cout << "0" << m << endl;
}
cout << h << ":" << m << endl;
}
void elapsed(Mytime t1, Mytime t2, Mytime& duration)
{
setTime(duration,0,0);
}
| |