目录
一、Date.h
#pragma once
#include <iostream>
using namespace std;
class Date
{
// 流插入运算符(<<)和流提取运算符(>>)重载
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
Date(int year = 1949, int month = 10, int day = 1)
: _year(year), _month(month), _day(day) {}
// 关系运算符重载
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 GetDay() const;
// 算术运算符重载
Date& operator+=(int day); // 日期 += 天数
Date operator+(int day) const; // 日期 + 天数
Date& operator-=(int day); // 日期 -= 天数
Date operator-(int day) const; // 日期 - 天数
int operator-(const Date& d) const; // 日期 - 日期
// 单目运算符(++ 和 --)重载
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
二、Date.cpp
#include "Date.h"
// 流插入运算符(<<)和流提取运算符(>>)重载
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "-" << d._month << "-" << d._day;
return out;
}
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
// 关系运算符重载
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);
}
bool Date::operator<(const Date& d) const
{
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) 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::GetDay() const
{
// 一三五七八十腊,三十一天永不差,
// 四六九冬三十天,平年二月二十八,闰年二月把一加。
static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[_month];
if (_month == 2)
{
if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0))
{
day += 1;
}
}
return day;
}
// 算术运算符重载
// 日期 += 天数
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
// day >= 0
_day += day;
while (_day > GetDay())
{
_day -= GetDay();
_month += 1;
if (_month > 12)
{
_month = 1;
_year += 1;
}
}
return *this;
}
// 日期 + 天数
Date Date::operator+(int day) const
{
Date tmp = *this;
tmp += day;
return tmp;
}
// 日期 -= 天数
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
// day >= 0
_day -= day;
while (_day <= 0)
{
_month -= 1;
if (_month < 1)
{
_month = 12;
_year -= 1;
}
_day += GetDay();
}
return *this;
}
// 日期 - 天数
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
// 日期 - 日期
int Date::operator-(const Date& d) const
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
flag = -1;
max = d;
min = *this;
}
int day = 0;
while (min < max)
{
min += 1;
day += 1;
}
return flag * day;
}
// 单目运算符(++ 和 --)重载
Date& Date::operator++()
{
return *this += 1;
}
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date& Date::operator--()
{
return *this -= 1;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
三、Test.cpp
#include "Date.h"
void test1()
{
Date d1(2023, 5, 1);
Date d2(2023, 6, 1);
cout << d1 << " " << d2 << endl; // 2023-5-1 2023-6-1
Date d;
cin >> d; // 假设输入:2023 7 1
cout << d << endl; // 2023-7-1
}
void test2()
{
Date d1(2023, 5, 1);
Date d2(2023, 6, 1);
if (d1 == d2)
cout << "d1 == d2" << endl;
else
cout << "d1 != d2" << endl;
// d1 != d2
if (d1 < d2)
cout << "d1 < d2" << endl;
else
cout << "d1 >= d2" << endl;
// d1 < d2
}
void test3()
{
Date d1(2023, 5, 1);
d1 += 100;
cout << d1 << endl; // 2023-8-9
d1 + 100;
cout << d1 << endl; // 2023-8-9
d1 -= 100;
cout << d1 << endl; // 2023-5-1
d1 - 100;
cout << d1 << endl; // 2023-5-1
d1 -= -100;
cout << d1 << endl; // 2023-8-9
d1 += -100;
cout << d1 << endl; // 2023-5-1
Date d2(2023, 8, 9);
cout << d1 - d2 << endl; // -100
cout << d2 - d1 << endl; // 100
}
void test4()
{
Date d1(2023, 5, 1);
Date d2(2023, 5, 1);
Date ret1 = d1++; // 后置++: 先使用,后++
Date ret2 = ++d2; // 前置++: 先++,后使用
cout << ret1 << " " << d1 << endl; // 2023-5-1 2023-5-2
cout << ret2 << " " << d2 << endl; // 2023-5-2 2023-5-2
ret1 = d1--; // 后置--: 先使用,后--
ret2 = --d2; // 前置--: 先--,后使用
cout << ret1 << " " << d1 << endl; // 2023-5-2 2023-5-1
cout << ret2 << " " << d2 << endl; // 2023-5-1 2023-5-1
}
int main()
{
// test1();
// test2();
// test3();
test4();
return 0;
}