-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtime.cpp
More file actions
106 lines (87 loc) · 1.97 KB
/
Copy pathtime.cpp
File metadata and controls
106 lines (87 loc) · 1.97 KB
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
// time.h
#include <iomanip>
#include <iostream>
#include <stdexcept>
using namespace std;
#ifndef TIME_H
# define TIME_H
class Time {
public:
explicit Time(int = 0, int = 0, int = 0);
void setTime(int, int, int);
void setHour(int);
void setMinute(int);
void setSecond(int);
unsigned int getHour() const;
unsigned int getMinute() const;
unsigned int getSecond() const;
void printStandard() const;
void tick(int, int, int) const;
private:
unsigned int hour;
unsigned int minute;
unsigned int second;
};
#endif
// include "Time.h"
Time::Time(int hour, int minute, int second) {
setTime(hour, minute, second);
// tick(hour, minute, second);
}
void Time::setTime(int h, int m, int s) {
setHour(h);
setMinute(m);
setSecond(s);
}
void Time::setHour(int h) {
if (h >= 0 && h < 24)
hour = h;
else
throw std::invalid_argument("hour must be 0-23");
}
void Time::setMinute(int m) {
if (m >= 0 && m <= 59) // 这里之前的范围没有包括59
minute = m;
else
throw std::invalid_argument("minute must be 0-59");
}
void Time::setSecond(int s) {
if (s >= 0 && s <= 59) // 这里之前的范围没有包括59
second = s;
else
throw std::invalid_argument("second must be 0-59");
}
unsigned int Time::getHour() const { return hour; }
unsigned int Time::getMinute() const { return minute; }
unsigned int Time::getSecond() const { return second; }
void Time::printStandard() const {
cout << ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12) << ":"
<< setfill('0') << setw(2) << getMinute() << ":" << setw(2)
<< getSecond();
}
void Time::tick(int h, int m, int s) const {
if (s != 59)
s += 1;
else {
s = 0;
if (m != 59)
m += 1;
else {
m = 0;
h += 1;
}
}
}
using namespace std;
int main() {
Time t(12, 54, 59);
t.printStandard();
cout << "\n";
try {
Time t2(99, 99, 99);
} catch (invalid_argument &e) { cerr << e.what(); }
}
/*
12:54:59
our must be 0-23
*/