题解 | #日期差值#
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
#include<iostream>
using namespace std;
class Date {
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month) {
int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//闰年29天
if (arr[month] == 28
&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
return 29;
}
return arr[month];
}
//获取一年的天数
int GetYearDay(int year, int month, int day) {
int SumDay = 0;
for (int i = 1; i <= month - 1; i++) {
SumDay += GetMonthDay(year, i);
}
SumDay += day;
return SumDay;
}
// 全缺省的构造函数(初始化)
Date(int year = 1900, int month = 1, int day = 1) {
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d) {
//cout << "Date(Date& d)" << endl;
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
int print() {
cout << _year << "年" << _month << "月" << _day << "日" << endl;
return 0;
}
// 析构函数
~Date() {
_year = 0;
_month = 0;
_day = 0;
}
// 日期+=天数
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;
};
// 日期+=天数
Date& Date:: operator+=(int day) {
if (day < 0) {
return *this -= (-day);
}
_day += day;
while (_day > GetMonthDay(_year, _month)) {
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13) {
++_year;
_month = 1;
}
}
return *this;
}
// 日期+天数
//出了作用域,tmp就不在了所以不能用引用返回
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) {
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& 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;
}
int Date:: operator-(const Date& d) {
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d) {
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max) {
min++;
n++;
}
return n * flag;
}
// >运算符重载
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 _year == d._year &&
_month == d._month &&
_day == d._day;
}
bool Date:: operator<=(const Date& d) {
//不用改代码复用就行
//this是左值的地址*this就是左值
return *this < d || *this == d;
}
//>=
bool Date:: operator>(const Date& d) {
//不用改代码复用就行
//this是左值的地址*this就是左值
return !(*this <= d);
}
// <=运算符重载
bool Date:: operator >= (const Date& d) {
return !(*this < d);
}
// !=运算符重载
bool Date:: operator != (const Date& d) {
return !(*this == d);
}
//// 日期-日期 返回天数
//int operator-(const Date& d);
void testDate1() {
Date d1(2023, 11, 10);
Date d2(2000, 1, 1);
cout << d1 - d2 << endl;
}
int main() {
int a, b;
cin >> a >> b;
Date d1(a / 10000, (a / 100) % 100, a % 100);
Date d2(b / 10000, (b / 100) % 100, b % 100);
int ret = d1 - d2;
if ((ret < 0)) {
if ((d1 + 1) == d2) {
cout << ((-ret) + 1);
} else
cout << (-ret) + 1;
} else {
cout << (d2-d1);
}
}
字节跳动公司福利 1297人发布
