-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.C++
145 lines (128 loc) · 4.05 KB
/
04.C++
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
#include <iostream> // Library for input and output operations
using namespace std; // To avoid using std:: before cout, cin, etc.
class Time
{
private:
int hour = 0; // Stores the hour value (0-23)
int minute = 0; // Stores the minute value (0-59)
int second = 0; // Stores the second value (0-59)
public:
// Constructor: Initializes a Time object with given values
Time(int h, int m, int s)
{
setHour(h); // Validates and sets the hour
setMinute(m); // Validates and sets the minute
setSecond(s); // Validates and sets the second
}
// Getter method for hour
int getHour()
{
return hour;
}
// Getter method for minute
int getMinute()
{
return minute;
}
// Getter method for second
int getSecond()
{
return second;
}
// Setter method for hour with validation
void setHour(int h)
{
if (h >= 0 && h <= 23) // Ensure the hour is in the correct range
{
hour = h;
}
else
{
hour = 0; // Default value if input is invalid
}
}
// Setter method for minute with validation
void setMinute(int m)
{
if (m >= 0 && m <= 59) // Ensure the minute is in the correct range
{
minute = m;
}
else
{
minute = 0; // Default value if input is invalid
}
}
// Setter method for second with validation
void setSecond(int s)
{
if (s >= 0 && s <= 59) // Ensure the second is in the correct range
{
second = s;
}
else
{
second = 0; // Default value if input is invalid
}
}
// Method to set time by calling individual setter methods
void setTime(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
// Method to return a new Time object representing the next second
Time nextSecond()
{
Time temp(hour, minute, second); // Create a copy of the current time
temp.second++; // Increment the second
if (temp.second >= 60) // If seconds reach 60, reset to 0 and increase minute
{
temp.second = 0;
temp.minute++;
if (temp.minute >= 60) // If minutes reach 60, reset to 0 and increase hour
{
temp.minute = 0;
temp.hour++;
if (temp.hour >= 24) // If hours reach 24, reset to 0 (new day starts)
{
temp.hour = 0;
}
}
}
return temp; // Return the updated Time object
}
// Method to return a new Time object representing the previous second
Time prevSecond()
{
Time temp(hour, minute, second); // Create a copy of the current time
temp.second--; // Decrement the second
if (temp.second < 0) // If seconds go below 0, set to 59 and decrease minute
{
temp.second = 59;
temp.minute--;
if (temp.minute < 0) // If minutes go below 0, set to 59 and decrease hour
{
temp.minute = 59;
temp.hour--;
if (temp.hour < 0) // If hours go below 0, set to 23 (previous day)
{
temp.hour = 23;
}
}
}
return temp; // Return the updated Time object
}
};
int main() {
// Creating a Time object with the maximum valid time (23:59:59)
Time T1(23, 59, 59);
// Getting the next second after 23:59:59, which should reset to 00:00:00
Time T2 = T1.nextSecond();
cout << "Next second: " << T2.getHour() << ":" << T2.getMinute() << ":" << T2.getSecond() << endl;
// Getting the previous second before 23:59:59, which should be 23:59:58
Time T3 = T1.prevSecond();
cout << "Prev second: " << T3.getHour() << ":" << T3.getMinute() << ":" << T3.getSecond() << endl;
return 0; // End of program
}