Dast.h
cpp
#pragma once
#define DATE_H
class Date
{
public:
Date(int year = 0, int month = 1, int day = 1);
void Print()const;
Date& operator +=(int day);
Date operator +(int day) const;
Date& operator -=(int day);
Date operator -(int day) const;
Date& operator ++();
Date operator ++(int);//后
Date& operator --();
Date operator --(int);//后
bool operator > (const Date& d) const;
bool operator == (const Date& d) const;
bool operator >= (const Date& d) const;
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator!=(const Date& d) const;
int operator -(const Date& d) const;
private:
int GetMonthDay(int year, int month) const;
int _year;
int _month;
int _day;
};
Dast.cpp(包含测试代码)

cpp
#include"Date.h"
#include<iostream>
using namespace std;
Date::Date(int year, int month, int day)
{
if (year >= 0
&& month >= 1 && month <= 12
&& day >= 1 && day <= GetMonthDay(year, month) )
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "非法日期" << endl;
_year = 0;
_month = 1;
_day = 1;
}
}
void Date::Print() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
int Date::GetMonthDay(int year, int month) const
{
static int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = days[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;
}
Date& Date::operator+=(int day)
{
//1 加日期 2 判断日期是否大于该月 减该月日期月++ 3判断月份 大于12 年++
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;//返回当前对象本身的引用
}
Date Date::operator+(int day) const
{
Date tmp(*this);
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
//1 减出天 2 循环若天一直小于1 月--
// 3判断月小于1 小于则年-- 月= 12 没循环一次就加上一个月的天数
_day -= day;
while (_day < 1)
{
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day) const
{
Date tmp(*this);//拷贝构造一个临时对象
tmp -= day;
return tmp;
}
Date& Date::operator++ ()
{
*this += 1;
return *this;
}
Date Date::operator++ (int)
{
Date tmp(*this); //保存当前状态
*this = tmp + 1;
return tmp;
}
Date& Date::operator-- ()
{
*this -= 1;
return *this;
}
Date Date::operator-- (int)//返回临时对象的值而非引用
{
Date tmp(*this); //保存当前状态
*this = tmp - 1;
return tmp;
}
//年小则小 年等月 小则小 年月等 天小则小
bool Date::operator> (const Date& d) const
{
if (_year > d._year)
return true;
else if (_year == d._year)
{
if (_month > d._month)
return true;
else if (_month == d._month)
{
if (_day > d._day)
return true;
}
}
return false;
}
bool Date::operator== (const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator>= (const Date& d) const
{
return *this > d || *this == d;
}
bool Date::operator<(const Date& d) const
{
return !(*this > d) && !(*this == d);
}
bool Date::operator<=(const Date& d) const
{
return !(*this > d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
//日期-日期
int Date::operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if (max < min)
{
max = d;
min = *this;
flag = -1;
}
int DayCount = 0;
while (min != max)
{
++min;
++DayCount;
}
return DayCount * flag;
}
void TestDate() {
// 测试构造函数
Date d1(2023, 10, 1);
Date d2(2024, 2, 29); // 2024是闰年,合法日期
Date d3(2023, 2, 29); // 2023不是闰年,非法日期
cout << "d1: ";
d1.Print(); // 2023-10-1
cout << "d2: ";
d2.Print(); // 2024-2-29
cout << "d3: ";
d3.Print(); // 非法日期,输出默认值0-1-1
// 测试+=和+运算符
Date d4(2023, 12, 31);
d4 += 1;
cout << "d4 += 1: ";
d4.Print(); // 2024-1-1
Date d5 = d4 + 31;
cout << "d4 + 31: ";
d5.Print(); // 2024-2-1
// 测试-=和-运算符
Date d6(2024, 1, 1);
d6 -= 1;
cout << "d6 -= 1: ";
d6.Print(); // 2023-12-31
Date d7 = d6 - 30;
cout << "d6 - 30: ";
d7.Print(); // 2023-11-30
// 测试前置++和后置++
Date d8(2023, 12, 30);
++d8;
cout << "++d8: ";
d8.Print(); // 2023-12-31
Date d9 = d8++;
cout << "d8++后d8: ";
d8.Print(); // 2024-1-1
cout << "d8++返回值: ";
d9.Print(); // 2023-12-31
// 测试前置--和后置--
Date d10(2024, 1, 2);
--d10;
cout << "--d10: ";
d10.Print(); // 2024-1-1
Date d11 = d10--;
cout << "d10--后d10: ";
d10.Print(); // 2023-12-31
cout << "d10--返回值: ";
d11.Print(); // 2024-1-1
// 测试比较运算符
Date d12(2023, 10, 1);
Date d13(2023, 12, 31);
Date d14(2024, 1, 1);
cout << "d12 > d13: " << (d12 > d13 ? "true" : "false") << endl; // false
cout << "d13 < d14: " << (d13 < d14 ? "true" : "false") << endl; // true
cout << "d12 == d13: " << (d12 == d13 ? "true" : "false") << endl; // false
cout << "d12 != d13: " << (d12 != d13 ? "true" : "false") << endl; // true
cout << "d13 <= d14: " << (d13 <= d14 ? "true" : "false") << endl; // true
// 测试日期-日期
Date d15(2023, 1, 1);
Date d16(2023, 12, 31);
int days = d16 - d15;
cout << "2023-12-31 与 2023-1-1 相差: " << days << "天" << endl; // 364天
Date d17(2024, 1, 1);
days = d17 - d15;
cout << "2024-1-1 与 2023-1-1 相差: " << days << "天" << endl; // 365天
}
int main() {
TestDate();
return 0;
}