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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#include <iostream>
#include <string>
using namespace std;
class Clock
{
private:
int mHours; //(range 0-23)
int mMinutes; //(range 0-59)
int mSeconds; //(range 0-59)
public:
Clock();
Clock(int);
Clock(int, int);
Clock(int, int, int);
int getHours()
{return mHours;}
int getMinutes()
{return mMinutes;}
int getSeconds()
{return mSeconds;}
void setHours(int hourValue) {
if ( hourValue >= 0 && hourValue <23) {
mHours = hourValue;
} else if(hourValue<0) {
mHours = 0;
} else if(hourValue>12) {
mHours = 12;
}
}
void setMinutes(int minuteValue) {
if ( minuteValue > 0 && minuteValue < 60) {
mMinutes = minuteValue;
} else if ( minuteValue < 0) {
mMinutes = 0;
} else if ( minuteValue > 59) {
mMinutes = 59;
}
}
void setSeconds(int secondValue) {
if ( secondValue > 0 && secondValue < 60) {
mSeconds = secondValue;
} else if ( secondValue < 0) {
mSeconds = 0;
} else if ( secondValue > 59) {
mSeconds = 59;
}
}
bool isMorning();
bool isAfternoon();
bool isEvening();
void tick();
friend ostream &operator<<(ostream &, Clock&);
};
ostream &operator<<(ostream &out, Clock &time){
if(time.mHours >12)
{ time.mHours = time.mHours - 12;
string display = "PM";
}else{
string display = "AM";
}
out << time.mHours;
out << ":"<< time.mMinutes <<":"
<< time.mSeconds << " "<<display << endl;
return out;
}
Clock::Clock() {
setHours(12);
setMinutes(0);
setSeconds(0);
}
Clock::Clock(int hourValue)
{setHours(hourValue);}
Clock::Clock(int hourValue, int minuteValue)
{setHours(hourValue);
setMinutes(minuteValue);}
Clock::Clock(int hourValue, int minuteValue, int secondValue)
{setHours(hourValue);
setMinutes(minuteValue);
setSeconds(secondValue);}
bool Clock::isMorning() {
if ( mHours < 12) {
return true;
}else{
return false;
}
}
bool Clock::isAfternoon() {
if ( mHours >= 12 && mHours < 18) {
return true;
}else{
return false;
}
}
bool Clock::isEvening() {
if ( mHours < 18) {
return false;
}else{
return true;
}
}
void Clock::tick() {
setSeconds((getSeconds() + 1 ) % 60);
if ( getSeconds()==0)
{
setMinutes((getMinutes() + 1) % 60);
if (getMinutes() == 0)
{
setHours((getHours() % 12) +1);
}
}
}
/*if (mSeconds < 60)
mSeconds = mSeconds + 1;
else
mSeconds = 0;
if (mMinutes < 60 && mSeconds == 0)
mMinutes = mMinutes + 1;
else
mMinutes = 0;
if (mHours < 24 && mMinutes == 0 && mSeconds == 0)
mHours = mHours + 1;
else
mHours = 0;*/
void printTimeMessage(Clock a) {
cout << "The current time is "<< a << endl;
}
int main() {
int hour;
int minute;
int second;
Clock a(hour,minute,second);
cout << "What is the hour?" << endl;
cin >> hour;
cout << "What is the minute?" << endl;
cin >> minute;
cout << "What is the second?" << endl;
cin >> second;
printTimeMessage(Clock(hour,minute,second));
hour= hour +2;
minute = minute + 30;
second = second + 15;
cout << "Back to the Future!";
printTimeMessage(Clock(hour,minute,second));
a.tick();
printTimeMessage(Clock(hour,minute,second));
return 0;
}
|