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
|
#include <iostream>
#include <cctype>
#include <cstdlib>
#include "dtime.h"
using namespace std;
void read_hour(istream& ins, short& the_hour);
void read_minute(istream& ins, short& the_minute);
short digit_to_int(char c);
bool operator ==(const DigitalTime& time1, const DigitalTime& time2)
{
return (time1.hour == time2.hour && time1.minute == time2.minute);
}
DigitalTime::DigitalTime(short the_hour, short the_minute)
{
if(the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59)
{
cout << "Illegal argument to DigitalTime constructor.";
exit(1);
}
else
{
hour = the_hour;
minute = _the_minute;
}
}
DigitalTime::DigitalTime() : hour(0), minute(0)
{
}
void DigitalTime::advance(short minutes_added)
{
short gross_minutes = minute + minutes_added;
minute = gross_minutes%60;
short hour_adjustment = gross_minutes/60;
short = (hour + hour_adjustment)%24;
}
void DigitalTime::advance(short hours_added, short minutes_added)
{
hour = (hour + hours_added)%24;
advance(minutes_added);
}
ostream& operator <<(ostream& outs, const DigitalTime& the_object)
{
outs << the_object.hour << ':';
if(the_object.minute < 10)
{
outs << '0';
}
outs << the_object.minute;
return outs;
}
istream& operator >>(istream& ins, DigitalTime& the_object)
{
read_hour(ins, the_object.hour);
read_minute(ins, the_object.minute);
return ins;
}
short digit_to_int(char c)
{
return (static_cast<short>(c) - static_cast<short>('0');
}
void read_minute(istream& ins, short& the_minute)
{
char c1, c2;
ins >> c1 >> c2;
if(!(isdigit(c1) && isdigit(c2)))
{
cout << "Error illegal input to read_minute\n";
exit(1);
}
the_minute = digit_to_int(c1)*10 + digit_to_int(c2);
if (the_minute < 0 || the_minute > 59)
{
cout << "Error illegal input to read_minute\n";
exit(1);
}
}
void read_hour(istream& ins, short& the_hour)
{
char c1, c2;
ins >> c1 >> c2;
if(!(isdigit(c1) && (isdigit(c2) || c2 == ':')))
{
cout << "Error illegal input to read_hour\n";
exit(1);
}
if(isdigit(c1) && c2 == ':')
{
the_hour = digit_to_int(c1);
}
else
{
the_hour = digit_to_int(c1)*10 + digit_to_int(c2);
ins >> c2;
if(c2 != ':')
{
cout << "Error illegal input to read_hour\n";
exit(1);
}
}
if(the_hour < 0 || the_hour > 23)
{
cout << "Error illegal input to read_hour\n";
exit(1);
}
}
|