int date::getmonthday(int year, int month)
{int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )){//是闰年并且是2月份return 29;}return arr[month];
}
date::date(int year, int month, int day)
{//判断日期是否合法if ( month>0 && month < 13 && ( day>0&&day<=getmonthday(year,month) ) ){_year = year;_month = month;_day = day;}else{cout << "日期不合法" << endl;}
}
bool date::operator==(const date& d)
{return (_year == d._year) && (_month == d._month) && (_day == d._day);
}
bool date::operator!=(const date & d)
{return !(*this == d);//复用d1==d2的相反逻辑
}
如果我们再次自己写就会有很多情况考虑,所以直接复用 d1==d2的取反
bool date::operator<(const date& d)
{if (_year < d._year)//年小为小{return true;}else if (_year == d._year && _month < d._month)//年相等 月小为小{return true;}else if (_year == d._year && _month == d._month && _day < d._day)// 年月相等 天小为小{return true;}else{return false;}
}
年小的为小,
年相等,月小为小
年 月相等,天小为小
其他情况都为false
bool date::operator<=(const date& d)
{return (* this < d) || (*this == d);//复用 d1
d1<=d2是由 d1==d2 和d1
bool date::operator>(const date& d)
{return !(*this < d) && (*this != d);//复用 d1
d1>d2的逻辑取反是 d1<=d2 ,由于d1
bool date::operator>=(const date& d)
{return !(* this < d);//复用d1
复用d1
date& date::operator+=(int day)
{if (day < 0)//+= -等价于 -={*this -= -day;//复用-=return *this;}//由于是+=改变本身,所以返回*thisint getday = _day + day;//判断当前加上的天数是否大于当前月份的天数while (getday > getmonthday(_year, _month)){getday -= getmonthday(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}//最后注意剩余的getday就为当前月份的天数_day = getday;//除了作用域 *this 还在所以可以使用引用返回return *this;
}
当day为负时,+= - 等价于-= ,所以调用-=的复用即可(-=实现在后面)
同样由于+=是对于本身操作,除了作用域还存在,所以使用引用返回
date date::operator+(int day)
{//注释的为第一种方法//由于不改变日期本身,所以用一个临时变量代替//date ret = *this;//int getday = ret._day + day;判断当前加上的天数是否大于当前月份的天数//while (getday > getmonthday(ret._year, ret._month))//{// getday -= getmonthday(ret._year, ret._month);// ret._month++;// if (ret._month == 13)// {// ret._year++;// ret._month = 1;// }//}最后注意剩余的getday就为当前月份的天数//ret._day = getday;除了作用域 ret不在了,所以使用传值返回//return ret;//方法二date tmp = *this;tmp += day;return tmp;//复用 日期+=天数的功能
}
这里用了两种方法实现,但是可以发现第一种过于繁琐,只需复用上述+=,返回临时变量tmp即可
同样由于临时变量出了作用域就不存在了,所以使用传值返回
date& date::operator-=(int day)
{if (day < 0){*this += -day;//复用+=return *this;}_day -= day;//当day 小于 当前月份的天数 直接return//当 day大于等于 当前月份的天数 进入循环while (_day <= 0){//返回到上一个月份_month--;//当月份为1时 --为0if (_month == 0){_year--;_month = 12;}_day += getmonthday(_year, _month);}return *this;
}
这里不太好想,如果当前日期月份的天数_day大于day,则直接return,若小于则会使_day为负进入循环,同时向上一个月借天数直到 _day<=0
若day为负,-=-等价于+= ,直接复用+=即可
date date::operator-(int day)
{date ret = *this;ret -= day;return ret;
}
复用上面的-=即可,返回临时变量ret
date& date::operator++()//++d
{*this += 1;//复用+=return *this;
}
date date::operator++(int)//d++
{date ret = *this;*this += 1;//复用+=return ret;
}
后置++因为同样是operator++,为了区分所以加上参数int(用于占位没有实际作用),构成函数重载
因为返回临时变量ret,所以用传值返回
date& date::operator--()//--d
{*this -= 1;//复用-=return *this;
}
date date::operator--(int)//d--
{date ret = *this;*this -= 1;//复用-=return ret;
}
后置- -因为同样是operator- -,为了区分所以加上参数int(用于占位没有实际作用),构成函数重载
因为返回临时变量ret,所以用传值返回
date date::operator--(int)//d--
{date ret = *this;*this -= 1;//复用-=return ret;
}int date::operator-(date& d){date max = *this;date min = d;if (*this < d)//复用<{max = d;min = *this;}int n = 0;while (min != max)//复用!={n++;if (min._day < getmonthday(min._year, min._month)){min._day++;}else{min._month++;min._day = 1;}if (min._month == 13){min._year++;min._month = 1;}}return n;}
主要使用max和min两个临时变量,使min._day不断++,最终使max==min 结束返回计数n值
#include
using namespace std;
class date
{
public://函数声明int getmonthday(int year, int month);//查询当前月份的天数date(int year=1, int month=1, int day=1);//构造void print();//输出bool operator==(const date& d);//d1==d2bool operator!=(const date& d);//d1!=d2bool operator<(const date& d);//d1(const date& d);//d1>d2bool operator>=(const date& d);//d1>=d2date operator+(int day);//日期+天数date& operator+=(int day);//日期+=天数date operator-(int day);//日期-天数date& operator-=(int day);//日期-=天数date& operator++();//++d//int 参数仅是为了占位,构成函数重载 区分前置date operator++(int);//d++date& operator--(); //--ddate operator--(int);//d--int operator-( date& d);//日期-日期 返回天数
private:int _year;int _month;int _day;
};
#include"date.h"
//函数实现
int date::getmonthday(int year, int month)
{int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )){//是闰年并且是2月份return 29;}return arr[month];
}
date::date(int year, int month, int day)
{//判断日期是否合法if ( month>0 && month < 13 && ( day>0&&day<=getmonthday(year,month) ) ){_year = year;_month = month;_day = day;}else{cout << "日期不合法" << endl;}}
void date::print()
{cout << _year << "-" << _month << "-" << _day << endl;
}
bool date::operator==(const date& d)
{return (_year == d._year) && (_month == d._month) && (_day == d._day);
}
bool date::operator!=(const date & d)
{return !(*this == d);//复用d1==d2的相反逻辑
}
bool date::operator<(const date& d)
{if (_year < d._year)//年小为小{return true;}else if (_year == d._year && _month < d._month)//年相等 月小为小{return true;}else if (_year == d._year && _month == d._month && _day < d._day)// 年月相等 天小为小{return true;}else{return false;}
}
bool date::operator<=(const date& d)
{return (* this < d) || (*this == d);//复用 d1(const date& d)
{return !(*this < d) && (*this != d);//复用 d1=(const date& d)
{return !(* this < d);//复用d1if (day < 0)//+= -等价于 -={*this -= -day;//复用-=return *this;}//由于是+=改变本身,所以返回*thisint getday = _day + day;//判断当前加上的天数是否大于当前月份的天数while (getday > getmonthday(_year, _month)){getday -= getmonthday(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}//最后注意剩余的getday就为当前月份的天数_day = getday;//除了作用域 *this 还在所以可以使用引用返回return *this;
}
date date::operator+(int day)
{由于不改变日期本身,所以用一个临时变量代替//date ret = *this;//int getday = ret._day + day;判断当前加上的天数是否大于当前月份的天数//while (getday > getmonthday(ret._year, ret._month))//{// getday -= getmonthday(ret._year, ret._month);// ret._month++;// if (ret._month == 13)// {// ret._year++;// ret._month = 1;// }//}最后注意剩余的getday就为当前月份的天数//ret._day = getday;除了作用域 ret不在了,所以使用传值返回//return ret;date tmp = *this;tmp += day;return tmp;//复用 日期+=天数的功能
}date& date::operator-=(int day)
{if (day < 0){*this += -day;//复用+=return *this; }_day -= day;//当day 小于 当前月份的天数 直接return//当 day大于等于 当前月份的天数 进入循环while (_day <= 0){//返回到上一个月份_month--;//当月份为1时 --为0if (_month == 0){_year--;_month = 12;}_day += getmonthday(_year, _month);}return *this;
}
date date::operator-(int day)
{date ret = *this;ret -= day;return ret;
}
date& date::operator++()//++d
{*this += 1;return *this;
}
date date::operator++(int)//d++
{date ret = *this;*this += 1;return ret;
}date& date::operator--()//--d
{*this -= 1;//复用-=return *this;
}date date::operator--(int)//d--
{date ret = *this;*this -= 1;//复用-=return ret;
}int date::operator-(date& d){date max = *this;date min = d;if (*this < d){max = d;min = *this;}int n = 0;while (min != max){n++;if (min._day < getmonthday(min._year, min._month)){min._day++;}else{min._month++;min._day = 1;}if (min._month == 13){min._year++;min._month = 1;}}return n;}
#include"date.h"
int main()
{date d1(2023, 2, 9);date d2(2023, 2, 1);cout << (d1==d2) << endl;cout << (d1 < d2) << endl;cout << (d1 - d2) << endl;return 0;
}
上一篇:MySQL事务管理
下一篇:力扣64.最小路径和