题解 | #日期差值#
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
#include <iostream> using namespace std; #include <assert.h> class Date { public: 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; assert(false); } } bool operator<(const Date& x) const { if (_year < x._year) { return true; } else if (_year == x._year && _month < x._month) { return true; } else if (_year == x._year && _month == x._month && _day < x._day) { return true; } else return false; } bool operator==(const Date& x) const { return _year == x._year && _month == x._month && _day == x._day; } //复用上面写过的<和==实现<= bool operator<=(const Date& x) const { return *this < x || *this == x; } //复用<=实现> bool operator>(const Date& x) const { return !(*this <= x); } //复用<实现>= bool operator>=(const Date& x) const { return !(*this < x); } //复用==实现!= bool operator!=(const Date& x) const { return !(*this == x); } int GetMonthDay(int year, int month)//获得某年某月的具体天数 { static int daysArr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//因为查询月份对应的天数这个函数经常调用,每次调用都创建数组太麻烦,直接把这个数组放到静态区,就不用每次调函数都创建数组了 //用下标映射月份 0月,1, 2 ........ //if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && month == 2)//这样写每次都要判断闰年,条件有点多,先判断月份效率高a if ((month == 2) && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))//判断是否为2月,是2月则继续判断是否为闰年,闰年2月是29天 { return 29; } return daysArr[month]; } Date& operator+=(int day)//+=是会改变原对象的值为新值 { if (day < 0)//如果是+=一个负数,就要转换成-=一个正数 { return *this -= -day; } _day += day;//this->_day-=day while (_day > GetMonthDay(_year, _month))//this->_day > GetMonthDay(this->_year, this->_month) { _day -= GetMonthDay(_year, _month); _month++; if (_month > 12) { _year++; _month = 1; } } return *this; } //复用+= Date operator+(int day) const//+不会改变原对象的值,+的结果是存在一个临时对象中,传值返回临时对象的值做结果 { Date tmp = *this;//创建一个中间对象,这个对象用*this指向的对象初始化 tmp += day;//tmp的值+=day,不影响*this指向的对象 return tmp;//传值返回tmp(过程中会调用拷贝构造函数完成传值返回) } //前置++ Date& operator++() { *this += 1; return *this; } //后置++ Date operator++(int)//增加这个int参数不是为了接收具体的值,仅仅是为了占位,便于区分,跟前置++构成重载 { Date tmp = *this; *this += 1; return tmp; } //-= Date& operator-=(int day)//-=是会改变原对象的值为新值 { if (day < 0)//如果是-=一个负数,就要转换成+=一个正数 { return *this += -day; } _day -= day; while (_day <= 0) { _month--;//向上个月借位 if (_month == 0) { _year--;//向上一年借位 _month = 12; } _day += GetMonthDay(_year, --_month); } return *this; } //- Date operator-(int day) const//-不会改变原对象的值,+的结果是存在一个临时对象中,传值返回临时对象的值做结果 { Date tmp = *this; tmp -= day;//_day-=day也行 return tmp; } //前置--,返回值是-1后的值 Date& operator--() { *this -= 1; return *this; } //后置--,返回值是-1之前的值 Date operator--(int) { Date tmp = *this; *this-=1; return tmp; } //自定义类型优先用前置++或-- int operator-(const Date& d) const//计算2个对象相差几天 { Date bigdate = *this; Date smalldate = d; if (*this < d) { bigdate = d; smalldate = *this; } int day = 0; while (bigdate != smalldate) { ++smalldate; day++; } return day; } private: int _year; int _month; int _day; }; int main() { string s1; cin>>s1; string year=s1.substr(0,4); int y1=stoi(year); string month=s1.substr(4,2); int m1=stoi(month); string day=s1.substr(6,2); int a1=stoi(day); string s2; cin>>s2; string year2=s2.substr(0,4); int y2=stoi(year2); string month2=s2.substr(4,2); int m2=stoi(month2); string day2=s2.substr(6,2); int a2=stoi(day2); Date d1(y1,m1,a1); Date d2(y2,m2,a2); cout<<(d1-d2)+1<<endl; } // 64 位输出请用 printf("%lld")