支持 日期的加减天数,以及日期减日期得到相差的天数。主要包含一下内容:
1.基本成员变量:
private:
int _year;
int _month;
int _day;
2.构造函数 与拷贝构造的实现
//构造函数
Date::Date(const int year ,const int month ,const int day ) {
if (!IsLegalDate(year, month, day)) {
cout << "非法日期!" << std::endl;
exit(1);
}//检查
_year = year;
_month = month;
_day = day;
}
//拷贝构造
Date::Date(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
3.日期类的基本功能
//判断日期是否合法
bool Date::IsLegalDate(const int year, const int month, const int day)const {
if (year <= 0 || (month <= 0 && month > 12) || (day <= 0 || day > GetMonthDay(year, month)))
return false;
return true;
}
//返回月份天数
int Date::GetMonthDay(const int year, const int month)const {
static int MonthDayArray[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
return MonthDayArray[month - 1] + (month == 2 && IsLeapYear(year) ? 1 : 0);
}
//打印年月日
void print() const{
cout << _year << '.' << _month << '.' << _day << std::endl;
}
//是否是闰年
bool IsLeapYear(const int year) const{
return (!(year % 4) && year % 100) || !(year % 400);
}
3.运算符重载
包含一些基础算法。
//+= 天数
Date& Date:: operator+=(const int day) {
_day += day;//加天数
for (int monthday = GetMonthDay(_year, _month); _day > monthday; monthday = GetMonthDay(_year, _month)) {//调整
_day -= monthday;
_month++;
if (_month > 12) {
_month = 1;
_year++;
}
}
return *this;
}
//+ 天数
Date Date::operator+(const int day) const {
Date tmp(*this);
return tmp += day;
}
//-=天数
Date& Date::operator-=(const int day) {
_day -= day;//减天数
while (_day <= 0) {//调整
_month--;
if (_month <= 0) {
_month = 12;
_year--;
if (_year <= 0) {//检查
cout << "非法日期!" << std::endl;
exit(1);
}
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
//- 天数
Date Date::operator-(const int day)const
{
Date tmp(*this);
return tmp-=day;
//前置++
Date& operator++() {
return *this += 1;
}
//后置++
//后置++其实是前置++的重载函数,参数只用于区分,并不使用
Date operator++(int) {
Date tmp(*this);
*this += 1;
return tmp ;
}
//前置--
Date& operator--() {
return *this -= 1;
}
//后置--
//参数用于区分重载
Date operator--(int) {
Date tmp(*this);
*this -= 1;
return tmp;
}
//日期类-日期类返回间隔的天数(与日期减天数构成函数重载)
int Date::operator-(Date const& d)const {
int dev = 0;
if (_year > d._year)//计算年差
for (int y = d._year; y != _year; y++)
dev += IsLeapYear(y) ? 366 : 365;
else
for (int y = _year; y != d._year; y++)
dev -= IsLeapYear(y) ? 366 : 365;
//月差
if (_month > d._month)
for (int m = d._month; m != _month; m++)
dev += GetMonthDay(d._year, m);
else
for (int m = _month; m != d._month; m++)
dev -= GetMonthDay(d._year, m);
//日差
dev += _day - d._day;
return dev;
}
//赋值运算符 根据原赋值运算符的规则,返回引用
Date& Date::operator=(Date const& d) {
if (this != &d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//关系运算符 年月日依次比较
bool Date::operator<(const Date& d) const{
return _year < d._year ? true : (_year == d._year ? (_month < d._month ? true : (_month == d._month ? (_day < d._day) : false)) : false);
}
bool Date::operator==(const Date& d) const{
return d._year == _year && d._month == _month && d._day == _day;
}
bool Date::operator>(const Date& d) const{
return _year > d._year ? true : (_year == d._year ? (_month > d._month ? true : (_month == d._month ? (_day > d._day) : false)) : false);
}
bool operator<=(const Date& d)const {
return *this == d || *this < d;
}
bool operator>=(const Date& d)const {
return *this == d || *this > d;
}
bool operator!=(const Date& d)const {
return !(*this == d);
}
//取反重载为 将日期的真实性颠倒
bool operator!()const {
return !IsLegalDate(_year, _month, _day);
}