编写日期时间类,年、月、日、时、分、秒都是数据成员,编写运算符“++”、“- -”、“+”、 “-”、 “>>” 、“<<”的重载函数,在主函数的函数体内中实现使用运算符直接进行日期时间对象的加、减运算。
时间: 2023-11-05 11:13:19 浏览: 33
以下是一个可能的实现:
```cpp
#include <iostream>
class DateTime {
public:
DateTime(int year, int month, int day, int hour, int minute, int second):
year(year), month(month), day(day), hour(hour), minute(minute), second(second) {}
// prefix increment operator
DateTime& operator++() {
++second;
normalize();
return *this;
}
// prefix decrement operator
DateTime& operator--() {
--second;
normalize();
return *this;
}
// addition operator
DateTime operator+(const DateTime& other) const {
DateTime result(*this);
result.year += other.year;
result.month += other.month;
result.day += other.day;
result.hour += other.hour;
result.minute += other.minute;
result.second += other.second;
result.normalize();
return result;
}
// subtraction operator
DateTime operator-(const DateTime& other) const {
DateTime result(*this);
result.year -= other.year;
result.month -= other.month;
result.day -= other.day;
result.hour -= other.hour;
result.minute -= other.minute;
result.second -= other.second;
result.normalize();
return result;
}
// input operator
friend std::istream& operator>>(std::istream& in, DateTime& dateTime) {
in >> dateTime.year >> dateTime.month >> dateTime.day >> dateTime.hour >> dateTime.minute >> dateTime.second;
dateTime.normalize();
return in;
}
// output operator
friend std::ostream& operator<<(std::ostream& out, const DateTime& dateTime) {
out << dateTime.year << "-" << dateTime.month << "-" << dateTime.day << " "
<< dateTime.hour << ":" << dateTime.minute << ":" << dateTime.second;
return out;
}
private:
int year;
int month;
int day;
int hour;
int minute;
int second;
// normalize values to valid range
void normalize() {
while (second < 0) {
--minute;
second += 60;
}
while (second >= 60) {
++minute;
second -= 60;
}
while (minute < 0) {
--hour;
minute += 60;
}
while (minute >= 60) {
++hour;
minute -= 60;
}
while (hour < 0) {
--day;
hour += 24;
}
while (hour >= 24) {
++day;
hour -= 24;
}
while (day < 1) {
--month;
if (month < 1) {
--year;
month = 12;
}
day += daysInMonth(year, month);
}
while (day > daysInMonth(year, month)) {
day -= daysInMonth(year, month);
++month;
if (month > 12) {
++year;
month = 1;
}
}
}
// number of days in given month of given year
int daysInMonth(int year, int month) const {
switch (month) {
case 2:
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
}
};
int main() {
DateTime now(2022, 3, 4, 10, 10, 10);
std::cout << "Now: " << now << std::endl;
DateTime later = now + DateTime(1, 2, 3, 4, 5, 6);
std::cout << "Later: " << later << std::endl;
DateTime earlier = now - DateTime(1, 2, 3, 4, 5, 6);
std::cout << "Earlier: " << earlier << std::endl;
++now;
std::cout << "Now after increment: " << now << std::endl;
--now;
std::cout << "Now after decrement: " << now << std::endl;
std::cout << "Enter a date and time in the format YYYY MM DD hh mm ss: ";
std::cin >> now;
std::cout << "You entered: " << now << std::endl;
return 0;
}
```
这个实现中,我们使用了一个 `normalize()` 函数来确保日期时间对象的数据成员都在合法范围内。例如,如果秒数小于 0,我们会将分钟数减 1,并将秒数加上 60,以确保秒数在 0 到 59 之间。如果日期不在当前月份的合法天数范围内,我们会将月份和年份相应地调整,直到日期在合法范围内。
在 `main()` 函数中,我们首先创建一个 `DateTime` 对象 `now`,并将其输出。然后,我们使用加法运算符创建一个新的 `DateTime` 对象 `later`,并输出它。接着,我们使用减法运算符创建一个新的 `DateTime` 对象 `earlier`,并输出它。然后,我们分别使用 ++ 运算符和 -- 运算符对 `now` 进行增加和减少,输出结果。最后,我们使用输入运算符 >> 读入一个 `DateTime` 对象,并将其输出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)