Date.h
#include <iostream>
using namespace std;
class Date
{
public:
void Print();
int GetMonthDay(int year, int month);
Date(int year = 1900, int month = 1, int day = 1);
Date(const Date& d);
Date& operator=(const Date& d);
~Date();
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.cpp
#include "Date.h"
void Date::Print()
{
cout << _year << " " << _month << " " << _day << endl;
}
int Date::GetMonthDay(int year, int month)
{
int Month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (year % 400 == 0 || year % 100 != 0 && year % 4 == 0)
Month[2] = 29;
return Month[month];
}
Date::Date(int year, int month, int day)
{
if (year >= 1 &&
month >= 1 && month <= 12 &&
day >= 1 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else {
cout << "错误日期" << endl;
}
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date::~Date()
{
_year = 1;
_month = 1;
_day = 1;
}
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
Date& Date::operator+=(int day)
{
if (day < 0)
*this -= ( - day);
else {
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_month += 1;
if (_month > 12)
{
_month = 1;
_year += 1;
}
_day -= GetMonthDay(_year, _month);
}
}
return *this;
}
Date Date::operator+(int day)
{
Date ans(*this);
ans += day;
return ans;
}
Date& Date::operator-=(int day)
{
if (day < 0)
*this += (-day);
else {
_day -= day;
while (_day < 0)
{
_month--;
if (_month == 0)
{
_year -= 1;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
}
return *this;
}
Date Date::operator-(int day)
{
Date ans(*this);
ans -= day;
return ans;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date ans(*this);
*this += 1;
return ans;
}
Date Date::operator--(int)
{
Date ans(*this);
*this -= 1;
return ans;
}
Date& Date::operator--()
{
return *this -= 1;
}
bool Date::operator>(const Date& d)
{
if (_year > d._year ||
_year == d._year && _month > d._month ||
_year == d._year && _month == d._month && _day > d._day)
return true;
return false;
}
bool Date::operator==(const Date& d)
{
if (_year == d._year && _month == d._month && _day == d._day)
return true;
return false;
}
bool Date::operator >= (const Date& d)
{
if (_year >= d._year ||
_year == d._year && _month >= d._month ||
_year == d._year && _month == d._month && _day >= d._day)
return true;
return false;
}
bool Date::operator < (const Date& d)
{
if (_year < d._year ||
_year == d._year && _month < d._month ||
_year == d._year && _month == d._month && _day < d._day)
return true;
return false;
}
bool Date::operator <= (const Date& d)
{
if (_year <= d._year ||
_year == d._year && _month <= d._month ||
_year == d._year && _month == d._month && _day <= d._day)
return true;
return false;
}
bool Date::operator != (const Date& d)
{
if (_year != d._year ||
_year == d._year && _month != d._month ||
_year == d._year && _month == d._month && _day != d._day)
return true;
return false;
}
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 count = 0;
while (min != max) {
min++;
count++;
}
return count * flag;
}
int main()
{
Date d1;
d1.Print();
Date d2(2023, 10, 1);
d2.Print();
cout << d2 - d1 << endl;
return 0;
}