302 lines
5.7 KiB
C++
302 lines
5.7 KiB
C++
|
#pragma once
|
|||
|
|
|||
|
#include <iostream>
|
|||
|
|
|||
|
namespace Lenyiin
|
|||
|
{
|
|||
|
class Date
|
|||
|
{
|
|||
|
private:
|
|||
|
friend std::ostream& operator<<(std::ostream& _cout, const Date& d);
|
|||
|
friend std::istream& operator>>(std::istream& _cin, Date& d);
|
|||
|
|
|||
|
// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|||
|
bool isLeapYear(int year) const
|
|||
|
{
|
|||
|
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>·ݵ<C2B7><DDB5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int GetMonthDay(int year, int month)
|
|||
|
{
|
|||
|
static int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
|||
|
int day = monthArray[month];
|
|||
|
|
|||
|
// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (month == 2 && isLeapYear(year))
|
|||
|
{
|
|||
|
day = 29;
|
|||
|
}
|
|||
|
|
|||
|
return day;
|
|||
|
}
|
|||
|
|
|||
|
// <20><>֤<EFBFBD><D6A4><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>Ч<EFBFBD><D0A7>
|
|||
|
bool isValidDate(int year, int month, int day)
|
|||
|
{
|
|||
|
// <20>·ݱ<C2B7><DDB1><EFBFBD><EFBFBD><EFBFBD> 1 <20><> 12 ֮<><D6AE>
|
|||
|
if (month < 1 || month > 12)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD> 1 <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ֮<><D6AE>
|
|||
|
return day >= 1 && day <= GetMonthDay(year, month);
|
|||
|
}
|
|||
|
|
|||
|
public:
|
|||
|
// <20><><EFBFBD>캯<EFBFBD><ECBAAF>
|
|||
|
Date(int year = 2024, int month = 1, int day = 1)
|
|||
|
{
|
|||
|
if (!isValidDate(year, month, day))
|
|||
|
{
|
|||
|
std::cerr << "Invalid date!" << std::endl;
|
|||
|
exit(1); // <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
|
|||
|
}
|
|||
|
|
|||
|
_year = year;
|
|||
|
_month = month;
|
|||
|
_day = day;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Date(const Date& d)
|
|||
|
: _year(d._year), _month(d._month), _day(d._day)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
void Print()
|
|||
|
{
|
|||
|
std::cout << _year << "-" << _month << "-" << _day << std::endl;
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><D8A3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><CDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ȥʹ<C8A5><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>ֵʱ<D6B5><CAB1><EFBFBD>ڿ<EFBFBD><DABF>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ٿ<EFBFBD><D9BF><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>
|
|||
|
// d1.operator==(d2);
|
|||
|
bool operator==(const Date& d) const // bool operator==(Date* this, const Date& d) const
|
|||
|
{
|
|||
|
if (this->_year == d._year && this->_month == d._month && this->_day == d._day)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator<(&d2);
|
|||
|
bool operator<(const Date& d) const // bool operator<(Date* this, const Date& d) const
|
|||
|
{
|
|||
|
if (this->_year < d._year)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
else if (this->_year == d._year && this->_month < d._month)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
else if (this->_year == d._year && this->_month == d._month && this->_day < d._day)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// d1 <= d2
|
|||
|
// d1.operator<=(&d2);
|
|||
|
bool operator<=(const Date& d) const // bool operator<=(Date* this, const Date& d) const
|
|||
|
{
|
|||
|
return (*this < d) || (*this == d); // <20><><EFBFBD>븴<EFBFBD><EBB8B4>
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator>(d2);
|
|||
|
bool operator>(const Date& d) const // bool operator>(Date* this, const Date& d) const
|
|||
|
{
|
|||
|
return d < *this; // <20><><EFBFBD>븴<EFBFBD><EBB8B4>
|
|||
|
}
|
|||
|
|
|||
|
// d1 >= d2
|
|||
|
// d1.operator>=(&d2);
|
|||
|
bool operator>=(const Date& d) const // bool operator>=(Date* this, const Date& d) const
|
|||
|
{
|
|||
|
return !(*this < d); // <20><><EFBFBD>븴<EFBFBD><EBB8B4>
|
|||
|
}
|
|||
|
|
|||
|
// d1 != d2
|
|||
|
// d1.operator!=(&d2);
|
|||
|
bool operator!=(const Date& d) const // bool operator!=(Date* this, const Date& d) const
|
|||
|
{
|
|||
|
return !(*this == d); // <20><><EFBFBD>븴<EFBFBD><EBB8B4>
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator=(d2);
|
|||
|
Date& operator=(const Date& d) // Date& operator=(Date* this, const Date& d)
|
|||
|
{
|
|||
|
if (*this != d)
|
|||
|
{
|
|||
|
_year = d._year;
|
|||
|
_month = d._month;
|
|||
|
_day = d._day;
|
|||
|
}
|
|||
|
return *this;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator+=(int);
|
|||
|
Date& operator+=(int day) // Date& operator+=(Date* this, int day)
|
|||
|
{
|
|||
|
if (day < 0)
|
|||
|
{
|
|||
|
return *this -= -day;
|
|||
|
}
|
|||
|
|
|||
|
_day += day;
|
|||
|
while (_day > GetMonthDay(_year, _month))
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>½<EFBFBD>λ
|
|||
|
_day -= GetMonthDay(_year, _month);
|
|||
|
_month++;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>²<EFBFBD><C2B2>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
|
|||
|
if (_month > 12)
|
|||
|
{
|
|||
|
_year++;
|
|||
|
_month = 1;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return *this;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator+(int);
|
|||
|
Date operator+(int day) // Date operator+(Date* this, int day)
|
|||
|
{
|
|||
|
Date ret(*this); // Date ret(this->_year, this->_month, this->_day);
|
|||
|
return ret += day;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator-=(int);
|
|||
|
Date& operator-=(int day) // Date& operator-=(Date* this, int day)
|
|||
|
{
|
|||
|
if (day < 0)
|
|||
|
{
|
|||
|
return *this += -day;
|
|||
|
}
|
|||
|
|
|||
|
_day -= day;
|
|||
|
while (_day < 1)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2>Ϸ<EFBFBD>, <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ, <20>²<EFBFBD><C2B2>Ϸ<EFBFBD><CFB7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
|
|||
|
_month--;
|
|||
|
if (_month == 0)
|
|||
|
{
|
|||
|
_year--;
|
|||
|
_month = 12;
|
|||
|
}
|
|||
|
_day += GetMonthDay(_year, _month);
|
|||
|
}
|
|||
|
|
|||
|
return *this;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator-(int);
|
|||
|
Date operator-(int day) // Date operator-(Date* this, int day)
|
|||
|
{
|
|||
|
Date ret(*this);
|
|||
|
return ret -= day;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator++(); // ǰ<><C7B0>++
|
|||
|
Date& operator++() // Date& operator++(Date* this)
|
|||
|
{
|
|||
|
*this += 1;
|
|||
|
return *this; // <20><><EFBFBD>ؼ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator++(int); // <20><><EFBFBD><EFBFBD>++
|
|||
|
Date operator++(int) // Date operator++(Date* this, int)
|
|||
|
{
|
|||
|
Date ret(*this);
|
|||
|
*this += 1;
|
|||
|
return ret; // <20><><EFBFBD>ؼ<EFBFBD>֮ǰ<D6AE>Ľ<EFBFBD><C4BD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator--(); // ǰ<><C7B0>--
|
|||
|
Date& operator--() // Date& operator--(Date* this)
|
|||
|
{
|
|||
|
*this -= 1;
|
|||
|
return *this;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator--(int); // <20><><EFBFBD><EFBFBD>--
|
|||
|
Date operator--(int) // Date operator--(Date* this, int)
|
|||
|
{
|
|||
|
Date ret(*this);
|
|||
|
*this -= 1;
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator-(d2);
|
|||
|
int operator-(const Date& d) // int operator-(Date* this, const Date& d)
|
|||
|
{
|
|||
|
Date max = *this;
|
|||
|
Date min = d;
|
|||
|
int flag = 1;
|
|||
|
|
|||
|
if (*this < d)
|
|||
|
{
|
|||
|
max = d;
|
|||
|
min = *this;
|
|||
|
flag = -1;
|
|||
|
}
|
|||
|
|
|||
|
int count = 0;
|
|||
|
while (min != max)
|
|||
|
{
|
|||
|
++min;
|
|||
|
count++;
|
|||
|
}
|
|||
|
|
|||
|
return count * flag;
|
|||
|
}
|
|||
|
|
|||
|
// d1.operator[](int);
|
|||
|
int& operator[](int index) // int& operator[](Date* this, int index)
|
|||
|
{
|
|||
|
if (index == 0)
|
|||
|
{
|
|||
|
return _year;
|
|||
|
}
|
|||
|
else if (index == 1)
|
|||
|
{
|
|||
|
return _month;
|
|||
|
}
|
|||
|
else if (index == 2)
|
|||
|
{
|
|||
|
return _day;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
~Date()
|
|||
|
{
|
|||
|
std::cout << "~Date()" << std::endl;
|
|||
|
}
|
|||
|
|
|||
|
private:
|
|||
|
int _year;
|
|||
|
int _month;
|
|||
|
int _day;
|
|||
|
};
|
|||
|
|
|||
|
std::ostream& operator<<(std::ostream& _cout, const Date& d)
|
|||
|
{
|
|||
|
_cout << d._year << "-" << d._month << "-" << d._day;
|
|||
|
return _cout;
|
|||
|
}
|
|||
|
|
|||
|
std::istream& operator>>(std::istream& _cin, Date& d)
|
|||
|
{
|
|||
|
_cin >> d._year >> d._month >> d._day;
|
|||
|
return _cin;
|
|||
|
}
|
|||
|
}
|