题解 | #日期差值#
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Date {
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator >> (istream& in, Date& d);
public://d2(d1) d2.Date(d1)
int GetMouthDay(int year, int mouth) {
static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[mouth];
if (mouth == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
day += 1;
}
return day;
}
Date()
{}
Date(int year, int mouth, int day) {
this->_year = year;
this->_mouth = mouth;
this->_day = day;
}
void Print() {
cout << _year << this->_mouth << this->_day;
}
//d1>d2--->!(d1<=d2)
bool operator>(const Date& d) {
return !(*this <= d);
}
bool operator>=(const Date& d) {
return !(*this < d);
}
bool operator==(const Date& d) {
if (this->_year == d._day && this->_mouth == d._mouth && this->_day == d._day) {
return true;
}
return false;
}
bool operator!=(const Date& d) {
return !(*this == d);
}
//d1<d2
//d1.operator<(&d1,d2)
inline bool operator<(const Date& d) {
if (this->_year < d._year) {
return true;
} else if (this->_year == d._year && _mouth < d._mouth) {
return true;
} else if (_year == d._year && _mouth == d._mouth && _day < d._day) {
return true;
}
return false;
}
bool operator <= (const Date& d) {
return *this < d || *this == d;
}
//d1+=10
Date& operator += (int day) {
this->_day += day;
while (_day > GetMouthDay(this->_year, this->_mouth)) {
_day -= GetMouthDay(this->_year, this->_mouth);
_mouth++;
if (_mouth == 13) {
++_year;
_mouth = 1;
}
}
return *this;
}
Date operator+(int day) {
Date ret(*this);
ret += day;//ret.operator+=(day);
return ret;
}
//d1+=10----d1.operator(&d1,10)
Date& operator-=(int day) {
this->_day -= day;
while (this->_day <= 0) {
this->_mouth--;
if (_mouth == 0) {
--_year;
_mouth = 12;
}
this->_day += GetMouthDay(_year, _mouth);
}
return *this;
}
Date operator-(int day) {
Date ret = *this;
ret._day -= day;
while (ret._day <= 0) {
ret._mouth--;
if (ret._mouth == 0) {
--ret._year;
ret._mouth = 12;
}
ret._day += GetMouthDay(ret._year, ret._mouth);
}
return ret;
}
//++d1--->d1.operator++(&d1)
Date& operator++() {
*this += 1;
return *this;//返回加之后的值
}
//d1++--->d1.operator++(&d1,0)
Date operator++(int) {
Date tmp(*this);
*this += 1;
return tmp;
}
//d1-d2 利用n返回d1和d2所相差的天数
int operator-(const Date& d) {
int flag = 1;
Date max = *this;//拷贝构造---浅拷贝---编译器可以自动生成
Date min = d;
if (*this < d) {
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max && min < max) {
++min;
++n;
}
return n + 1;
}
void Scanf() {
scanf("%04d%02d%02d", &this->_year, &this->_mouth, &this->_day);
}
////cour->ostream
////cin->istream
private:
int _year;
int _mouth;
int _day;
};
ostream& operator<<(ostream& out, const Date& d) {
out << d._year << d._mouth << d._day << endl;
return out;
}
istream& operator >> (istream& in, Date& d) {
in >> d._year >> d._mouth >> d._day;
return in;
}
int main() {
/*Date d1(0, 1, 1);
Date d2(0, 1, 1);*/
/*Date d1(2022,2,10);
Date d2(2022, 2, 1);*/
//cin >> d1 >> d2;
/*while (cin>>d1>>d2)
{
cout << d1 << d2;
}*/
Date d1, d2;//&& d2.Scanf()
/*while (cin>>d1>>d2) {
cout << d1 - d2;
}*/
d1.Scanf();
d2.Scanf();
/* cout << d1<<endl;
cout << d2<<endl;*/
cout << d1 - d2;
}
// 64 位输出请用 printf("%lld")
查看10道真题和解析
美的集团公司福利 747人发布
