1.代码
1.Date.h
#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
int& GetMonthDay(int& year, int& month)//获得该月天数
{
static int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
static int b = 29;
if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
return b;
}
return a[month];
}
Date(int year = 1, int month = 1,int day=1);//构造函数
void Print();
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);
Date operator+(int day);//加天数
Date operator-(int day);
Date& operator=(Date& d);//赋值
Date& operator+=(int day);
Date& operator-=(int day);
Date& operator++();//前置++和--
Date& operator--();
Date operator++(int);//后置++和--
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
2.Date.cpp
#include "Date.h"
Date::Date(int year, int month, int day)//构造函数
{
if (!(year >= 1 && (month >= 1 && month <= 12) && (day >= 1 && day <= GetMonthDay(year, month))))
{
cout << "日期非法" << endl;
}
else
{
_year = year;
_month = month;
_day = day;
}
}
//打印
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
//判断天数大小
bool Date::operator==(const Date& d)
{
return this->_day == d._day && this->_month == d._month && this->_year == d._year;
}
bool Date::operator>(const Date& d)
{
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;
}
else
return false;
}
}
}
bool Date::operator>=(const Date& d)
{
return *this > d || *this == d;
}
bool Date::operator<(const Date& d)
{
return !(*this >= d);
}
bool Date::operator<=(const Date& d)
{
return !(*this > d);
}
//赋值
Date& Date::operator=(Date& d)
{
if (!(d._year >= 1 && (d._month >= 1 && d._month <= 12) && (d._day >= 1 && d._day <= GetMonthDay(d._year, d._month))))
{
cout << "日期非法" << endl;
}
else
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date& Date::operator+=(int day)
{
this->_day += day;
while (this->_day > GetMonthDay(this->_year, this->_month))
{
this->_day -= GetMonthDay(this->_year, this->_month);
this->_month++;
if (this->_month == 13)
{
this->_month = 1;
this->_year++;
}
}
return *this;
}
Date& Date::operator-=(int day)//5.12 - 18
{
this->_day -= day;
while (this->_day <= 0 )
{
this->_month--;
if (this->_month == 0)
{
this->_month = 12;
this->_year--;
}
this->_day += GetMonthDay(this->_year, this->_month);
}
return *this;
}
//加天数
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
//前置++和--
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//后置++和--
Date Date::operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
Date Date::operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
3.main.cpp
#include "Date.h"
int main()
{
Date d1(2023, 1, 1);
Date d2(2023, 1, 1);
Date d3(2021, 1, 1);
(d1++).Print();
(d2--).Print();
/*(++d1).Print();
(--d2).Print();*/
/*d1 = d2;
d1.Print();
d1 = d3;
d1.Print();*/
//d1 += 1000;
//d2 -= 1000;
//d2.Print();
//(d1+1000).Print();
//(d2-1000).Print();
/*d1.Print();
d2.Print();*/
}