#include<iostream>
// Date类用于表示日期并支持一系列日期操作
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
int month_day[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (month == 2)
return month_day[month - 1] + 1; // 如果是闰年且月份为2月,则天数为对应的数组值加1
}
else
{
return month_day[month - 1]; // 非闰年则直接返回对应的月份天数
}
}
// 构造函数:默认构造函数,可指定年、月、日;如果不指定,默认初始化为1900年1月1日
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数:通过另一个Date对象进行拷贝构造
Date(const Date& d)
{
this->_day = d._day;
this->_month = d._month;
this->_year = d._year;
}
// 赋值运算符重载:将当前对象的值赋值为另一个Date对象的值
Date& operator=(const Date& d)
{
if (this == &d)
return *this;
this->_day = d._day;
this->_month = d._month;
this->_year = d._year;
return *this;
}
// 析构函数:类对象销毁时调用,这里没有特殊逻辑,所以为空函数体
~Date() { }
// 日期+=天数
Date& operator+=(int day)
{
// 迭代处理直到天数减为0
while (day > 0)
{
int monthDays = GetMonthDay(this->_year, this->_month); // 获取当前月份的天数
int remainingDays = monthDays - this->_day; // 当月剩余天数
if (day <= remainingDays)
{
this->_day += day; // 如果加上day后仍在本月内,直接累加天数
break;
}
else
{
day -= remainingDays; // 否则,减去本月剩余天数,继续处理下个月
this->_day = 0;
++this->_month;
if (this->_month > 12)
{
++this->_year; // 如果月份超过12,则进入下一年
this->_month = 1;
}
}
}
return *this;
}
// 日期+天数:返回新的Date对象,原对象不变
Date operator+(int day)
{
Date carrent = *this;
carrent += day; // 调用+=运算符重载实现
return carrent;
}
// 日期-天数:返回新的Date对象,原对象不变
Date operator-(int day)
{
Date carrent = *this;
// 迭代处理直到天数减为0
while (day > 0)
{
if (day < carrent._day)
{
carrent._day -= day; // 如果减去day后仍在本月内,直接减去天数
break;
}
else
{
day -= carrent._day; // 否则,减去本月天数,继续处理上个月
--carrent._month;
if (carrent._month <= 0)
{
--carrent._year; // 如果月份小于等于0,则进入上一年的12月
carrent._month = 12;
}
carrent._day = GetMonthDay(carrent._year, carrent._month);
}
}
return carrent;
}
// 日期-=天数:与+天数运算类似,原对象发生改变
Date& operator-=(int day)
{
// 迭代处理直到天数减为0
while (day > 0)
{
if (day < this->_day)
{
this->_day -= day; // 如果减去day后仍在本月内,直接减去天数
break;
}
else
{
day -= this->_day; // 否则,减去本月天数,继续处理上个月
--this->_month;
if (this->_month <= 0)
{
--this->_year; // 如果月份小于等于0,则进入上一年的12月
this->_month = 12;
}
this->_day = GetMonthDay(this->_year, this->_month);
}
}
return *this;
}
// 前置++:日期加1
Date& operator++()
{
*this += 1; // 调用+=运算符重载实现
return *this;
}
// 后置++:日期加1,但返回的是未加1之前的日期
Date operator++(int)
{
Date carrent = *this;
*this += 1; // 调用+=运算符重载实现
return carrent;
}
// 后置--:日期减1,但返回的是未减1之前的日期
Date operator--(int)
{
Date carrent = *this;
*this -= 1; // 调用-=运算符重载实现
return carrent;
}
// 前置--:日期减1
Date& operator--()
{
*this -= 1; // 调用-=运算符重载实现
return *this;
}
// >运算符重载:比较日期的大小
bool operator>(const Date& d)
{
if (this == &d)
return false;
if (this->_year > d._year)
return true;
if (this->_year < d._year)
return false;
if (this->_month > d._month)
return true;
if (this->_month < d._month)
return false;
if (this->_day > d._day)
return true;
return false;
}
// ==运算符重载:判断日期是否相等
bool operator==(const Date& d)
{
if (this == &d)
return true;
if (this->_year == d._year && this->_month == d._month && this->_year == d._year)
return true;
return false;
}
// >=运算符重载:判断日期是否大于等于
bool operator >= (const Date& d)
{
if (this == &d)
return true;
if (this->_year > d._year)
return true;
if (this->_year < d._year)
return false;
if (this->_month > d._month)
return true;
if (this->_month < d._month)
return false;
if (this->_day >= d._day)
return true;
return false;
}
// <运算符重载:判断日期是否小于
bool operator < (const Date& d)
{
if (this == &d)
return false;
if (this->_year > d._year)
return false;
if (this->_year < d._year)
return true;
if (this->_month > d._month)
return false;
if (this->_month < d._month)
return true;
if (this->_day > d._day)
return false;
if (this->_day < d._day)
return true;
return false;
}
// <=运算符重载:判断日期是否小于等于
bool operator <= (const Date& d)
{
if (this == &d)
return true;
if (this->_year > d._year)
return false;
if (this->_year < d._year)
return true;
if (this->_month > d._month)
return false;
if (this->_month < d._month)
return true;
if (this->_day <= d._day)
return true;
return false;
}
// !=运算符重载:判断日期是否不等
bool operator != (const Date& d)
{
if (this == &d)
return false;
if (this->_year != d._year && this->_month != d._month && this->_year != d._year)
return true;
return false;
}
// 日期-日期:计算两个日期之间的天数差
int operator-(const Date& d)
{
// 计算年份差、月份差、日期差
int yearDiff = _year - d._year;
int monthDiff = _month - d._month;
int dayDiff = _day - d._day;
// 总天数为年份差乘以365加上月份差乘以30再加上日期差
int totalDays = yearDiff * 365 + monthDiff * 30 + dayDiff;
return totalDays;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
};
批注和详细介绍:
1. 这段C++代码定义了一个`Date`类,用于表示日期,并支持一系列日期操作。
2. `Date`类有三个私有成员变量 `_year`、`_month` 和 `_day` 分别用于存储年份、月份和日期。
3. `GetMonthDay`是一个私有成员函数,用于根据年份和月份获取某月的天数。注意,在这个函数中考虑了闰年的情况。
4. 构造函数:`Date(int year = 1900, int month = 1, int day = 1)`,提供了默认参数,所以可以根据需要使用构造函数进行日期对象的初始化。
5. 拷贝构造函数:`Date(const Date& d)`,用于通过另一个`Date`对象进行拷贝构造,即将一个已存在的对象复制生成一个新对象。
6. 赋值运算符重载:`Date& operator=(const Date& d)`,用于将当前对象的值赋值为另一个`Date`对象的值,使得两个对象的值相等。
7. 析构函数:`~Date() {}`,这里是一个空的析构函数,因为该类没有动态分配内存,所以不需要进行特殊的资源释放操作。
8. 运算符重载:
- `+=`:日期+=天数,使日期增加指定的天数。
- `+`:日期+天数,返回一个新的`Date`对象,表示日期增加指定的天数,原日期对象不变。
- `-=`:日期-=天数,使日期减去指定的天数。
- `-`:日期-天数,返回一个新的`Date`对象,表示日期减去指定的天数,原日期对象不变。
- `++`:前置自增运算符,日期加1。
- `++(int)`:后置自增运算符,日期加1,但返回的是未加1之前的日期。
- `--(int)`:后置自减运算符,日期减1,但返回的是未减1之前的日期。
- `--`:前置自减运算符,日期减1。
- 比较运算符重载:`>、>=、<、<=、==、!=`,用于比较日期对象之间的大小关系。
- `-(const Date& d)`:重载减法运算符,用于计算两个日期对象之间的天数差。
注意事项:
- 在实际应用中,日期类应该包含更多的功能和鲁棒性处理,例如考虑到闰年规则的更多情况,错误输入的处理,以及更多的日期运算等。此代码仅仅是一个简单的示例,可能不足以应对所有实际情况。