题解 | #日期差值#
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
#include<iostream>
using namespace std;
class Date {
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
int Getday(Date&t);//获取当前的所有天数
void prin() {
cout << _year << "-";
if (_month < 10) {
cout << "0" << _month << "-";
} else {
cout << _month << "-";
}
if (_day < 10) {
cout << "0" << _day << endl;
} else {
cout << _day << endl;
}
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
: _year(year),
_month(month),
_day(day) {
;
}
// 拷贝构造函数
// d2(d1)
/*Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}*/
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day);
// 日期-天数
Date operator-(int day);
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
bool operator>(const Date& d);
// ==运算符重载
bool operator==(const Date& d);
// >=运算符重载
bool operator >= (const Date& d);
// <运算符重载
bool operator < (const Date& d);
// <=运算符重载
bool operator <= (const Date& d);
// !=运算符重载
bool operator != (const Date& d);
// 日期-日期 返回天数
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
int month[13] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day2[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//日期+=天数
Date& Date::operator+=(int day) {
_day += day;
while (_day > GetMonthDay(_year, _month)) {
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13) {
_month = 1;
_year++;
}
}
return *this;
}
// 日期+天数
Date Date::operator+(int day) {
Date tmp(*this);
tmp += day;
return tmp;
}
// 日期-天数
Date Date::operator-(int day) {
Date tmp(*this);
tmp -= day;
return tmp;
}
// 日期-=天数
Date& Date:: operator-=(int day) {
_day -= day;
if (_day < 0) {
_day += GetMonthDay(_year, _month);
_month - 1;
if (_month == 0) {
_month = 12;
_year = _year - 1;
}
}
return *this;
}
// 前置++
Date& Date:: operator++() {
*this += 1;
return *this;
}
// 后置++
Date Date::operator++(int) {
Date tmp = *this;
*this += 1;
return tmp;
}
// 后置--
Date Date::operator--(int) {
Date tmp = *this;
*this -= 1;
return tmp;
}
// 前置--
Date& Date::operator--() {
*this -= 1;
return *this;
}
Date& Date::operator=(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
int Date::GetMonthDay(int year, int month) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return day2[month];
} else {
return day[month];
}
}
bool Date::operator<(const Date& x) {
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;
}
return false;
}
bool Date::operator==(const Date& x) {
return _year == x._year
&& _month == x._month
&& _day == x._day;
}
// 复用
// d1 <= d2
bool Date::operator<=(const Date& x) {
return *this < x || *this == x;
}
bool Date::operator>(const Date& x) {
return !(*this <= x);
}
bool Date::operator>=(const Date& x) {
return !(*this < x);
}
bool Date::operator!=(const Date& x) {
return !(*this == x);
}
int Date::Getday(Date &t)
{
int p=0;
int s=t._month;
while(--s)
{
p+=t.GetMonthDay(t._year, s);
}
p+=t._day;
int year__=t._year-1;
while (year__--) {
if ((year__ % 4 == 0 && year__ % 100 != 0) || year__ % 400 == 0) {
p =p+ 366;
} else {
p =p+ 365;
}
}
return p;
}
int Date::operator-(const Date& d) {
int year = _year;
int month = _month;
int day1 = _day;
int t = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
while (month) {
t += day2[month];
month--;
}
} else {
while (month) {
t += day[month];
month;
}
}
while (year--) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
t += 366;
} else {
t += 365;
}
}
t += day1;
int t2 = 0;
year = d._year;
day1 = d._day;
month = d._month;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
while (month) {
t2 += day2[month];
month--;
}
} else {
while (month) {
t2 += day[month];
month--;
}
}
while (year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
t2 += 366;
} else {
t2 += 365;
}
year--;
}
t2 += d._day;
return abs(t - t2);
}
int main() {
int a;
int b;
cin>>a>>b;
int year1=a/10000;
int month1=(a/100)%100;
int day1=a%100;
int year2=b/10000;
int month2=(b/100)%100;
int day2=b%100;
Date A1(year1,month1,day1);
Date B1(year2,month2,day2);
int p1=A1.Getday(B1);
int p2=B1.Getday(A1);
int pw=abs(p1-p2);
cout<<pw+1;
return 0;
}