编写日期时间类,年、月、日、时、分、秒都是数据成员,编写运算符“++”、“- -”、“+”、 “-”、 “>>” 、“<<”的重载函数,在主函数的函数体内中实现使用运算符直接进行日期时间对象的加、减运算。
时间: 2023-11-05 09:08:38 浏览: 64
以下是一个简单的日期时间类的实现,包括上述所述的运算符重载函数:
```c++
#include <iostream>
#include <iomanip>
using namespace std;
class DateTime {
private:
int year;
int month;
int day;
int hour;
int minute;
int second;
public:
DateTime(int y = 1970, int m = 1, int d = 1, int h = 0, int min = 0, int sec = 0)
: year(y), month(m), day(d), hour(h), minute(min), second(sec) {}
void print() const {
cout << setfill('0') << setw(4) << year << "-" << setw(2) << month << "-" << setw(2) << day
<< " " << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second;
}
DateTime operator++() { // 前置++
++second;
if (second >= 60) {
second = 0;
++minute;
if (minute >= 60) {
minute = 0;
++hour;
if (hour >= 24) {
hour = 0;
++day;
if (day > daysInMonth()) {
day = 1;
++month;
if (month > 12) {
month = 1;
++year;
}
}
}
}
}
return *this;
}
DateTime operator++(int) { // 后置++
DateTime temp(*this);
++(*this);
return temp;
}
DateTime operator--() { // 前置--
--second;
if (second < 0) {
second = 59;
--minute;
if (minute < 0) {
minute = 59;
--hour;
if (hour < 0) {
hour = 23;
--day;
if (day < 1) {
--month;
if (month < 1) {
month = 12;
--year;
}
day = daysInMonth();
}
}
}
}
return *this;
}
DateTime operator--(int) { // 后置--
DateTime temp(*this);
--(*this);
return temp;
}
DateTime operator+(const DateTime& other) const {
DateTime result(*this);
result.second += other.second;
result.minute += other.minute;
result.hour += other.hour;
result.day += other.day;
result.month += other.month;
result.year += other.year;
int extraSeconds = 0;
if (result.second >= 60) {
extraSeconds = result.second / 60;
result.second %= 60;
}
result.minute += extraSeconds;
extraSeconds = 0;
if (result.minute >= 60) {
extraSeconds = result.minute / 60;
result.minute %= 60;
}
result.hour += extraSeconds;
extraSeconds = 0;
if (result.hour >= 24) {
extraSeconds = result.hour / 24;
result.hour %= 24;
}
result.day += extraSeconds;
while (result.day > result.daysInMonth()) {
result.day -= result.daysInMonth();
++result.month;
if (result.month > 12) {
result.month = 1;
++result.year;
}
}
return result;
}
DateTime operator-(const DateTime& other) const {
DateTime result(*this);
result.second -= other.second;
result.minute -= other.minute;
result.hour -= other.hour;
result.day -= other.day;
result.month -= other.month;
result.year -= other.year;
int extraSeconds = 0;
if (result.second < 0) {
extraSeconds = (-result.second) / 60 + 1;
result.second += 60 * extraSeconds;
}
result.minute -= extraSeconds;
extraSeconds = 0;
if (result.minute < 0) {
extraSeconds = (-result.minute) / 60 + 1;
result.minute += 60 * extraSeconds;
}
result.hour -= extraSeconds;
extraSeconds = 0;
if (result.hour < 0) {
extraSeconds = (-result.hour) / 24 + 1;
result.hour += 24 * extraSeconds;
}
result.day -= extraSeconds;
while (result.day < 1) {
--result.month;
if (result.month < 1) {
result.month = 12;
--result.year;
}
result.day += result.daysInMonth();
}
return result;
}
friend istream& operator>>(istream& in, DateTime& dt) {
in >> dt.year >> dt.month >> dt.day >> dt.hour >> dt.minute >> dt.second;
return in;
}
friend ostream& operator<<(ostream& out, const DateTime& dt) {
dt.print();
return out;
}
private:
int daysInMonth() const {
if (month == 2) {
if (isLeapYear()) {
return 29;
} else {
return 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
}
}
bool isLeapYear() const {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
};
int main() {
DateTime dt1, dt2(2022, 10, 1, 12, 30, 0);
cout << "dt1: " << dt1 << endl;
cout << "dt2: " << dt2 << endl;
cout << "++dt1: " << ++dt1 << endl;
cout << "--dt1: " << --dt1 << endl;
cout << "dt1 + dt2: " << dt1 + dt2 << endl;
cout << "dt1 - dt2: " << dt1 - dt2 << endl;
cout << "Enter a date time (yyyy mm dd hh mm ss): ";
cin >> dt1;
cout << "You entered: " << dt1 << endl;
return 0;
}
```
运行该程序,可以进行日期时间对象的加、减、前置/后置自增自减等运算:
```text
dt1: 1970-01-01 00:00:00
dt2: 2022-10-01 12:30:00
++dt1: 1970-01-01 00:00:01
--dt1: 1969-12-31 23:59:59
dt1 + dt2: 2022-10-01 12:30:00
dt1 - dt2: -2022-10-01 11:29:59
Enter a date time (yyyy mm dd hh mm ss): 2022 12 31 23 59 59
You entered: 2022-12-31 23:59:59
```
阅读全文