-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.cpp
132 lines (126 loc) · 3.26 KB
/
Time.cpp
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
#include "Time.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
Time::Time(int hour, int minute) {
setHour(hour);
setMinute(minute);
}
void Time::setHour(int hour) {
this -> hour = (hour >= 0 && hour <= 24) ? hour : 0;
}
void Time::setMinute(int minute) {
this -> minute = (minute < 60 && minute >= 0) ? minute : 0;
}
int Time::getHour() {
return hour;
}
int Time::getMinute() {
return minute;
}
Time Time::operator++() {
* this = * this + 1;
return ( * this);
}
void Time::operator += (int minute) {
* this = * this + minute;
}
Time Time::operator - (Time t) {
Time result;
result.hour = hour - t.hour;
result.minute = minute - t.minute;
if (minute - t.minute < 0) {
result.minute += 60;
result.hour--;
}
return result;
}
Time Time::operator + (int minute) {
Time result = * this;
if (minute >= 0) {
while (minute + result.minute >= 60) {
if (result.hour >= 24) {
result.hour -= 24;
result.hour++;
} else result.hour++;
minute -= 60;
}
result.minute += minute;
return result;
} else return Time(0, 0);
}
bool Time::operator != (Time t) {
return (t.hour != hour && t.minute != minute);
}
bool Time::operator == (Time t) {
return (t.hour == hour && t.minute == minute);
}
bool Time::operator < (Time t) {
return (hour < t.hour || (hour == t.hour && minute < t.minute));
}
bool Time::operator > (Time t) {
return (hour > t.hour || (hour == t.hour && minute > t.minute));
}
bool Time::operator <= (Time t) {
return (hour < t.hour || (hour == t.hour && minute <= t.minute));
}
bool Time::operator >= (Time t) {
return (hour > t.hour || (hour == t.hour && minute >= t.minute));
}
ostream & operator << (ostream & s, Time t) {
if (t.hour > 12) {
if (t.hour - 12 < 10) {
if (t.minute < 10)
cout << "0" << t.hour - 12 << ":" << "0" << t.minute << " PM";
else
cout << "0" << t.hour - 12 << ":" << t.minute << " PM";
} else {
if (t.minute < 10)
cout << t.hour - 12 << ":" << "0" << t.minute << " PM";
else
cout << t.hour - 12 << ":" << t.minute << " PM";
}
} else {
if (t.hour < 10) {
if (t.minute < 10)
cout << "0" << t.hour << ":" << "0" << t.minute << " PM";
else
cout << "0" << t.hour << ":" << t.minute << " PM";
} else {
if (t.minute < 10)
cout << t.hour << ":" << "0" << t.minute << " PM";
else
cout << t.hour << ":" << t.minute << " PM";
}
}
return s;
}
ofstream & operator << (ofstream & f, Time t) {
if (t.hour > 12) {
if (t.hour - 12 < 10) {
if (t.minute < 10)
f << "0" << t.hour - 12 << ":" << "0" << t.minute << " PM";
else
f << "0" << t.hour - 12 << ":" << t.minute << " PM";
} else {
if (t.minute < 10)
f << t.hour - 12 << ":" << "0" << t.minute << " PM";
else
f << t.hour - 12 << ":" << t.minute << " PM";
}
} else {
if (t.hour < 10) {
if (t.minute < 10)
f << "0" << t.hour << ":" << "0" << t.minute << " PM";
else
f << "0" << t.hour << ":" << t.minute << " PM";
} else {
if (t.minute < 10)
f << t.hour << ":" << "0" << t.minute << " PM";
else
f << t.hour << ":" << t.minute << " PM";
}
}
return f;
}