各个运算符的重载代码实现如下:
#pragma once
#include<iostream>
using namespace std;
class Date
{
private:
int _year;
int _month;
int _day;
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{
// 如何检查一个日期是否合法
int days = GetMonthDays(year, month);
if (days == -1 || day < 1 || day > days)
{
cout << "日期不合法" << endl;
Display();
exit(-1);
}
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool IsLeapYear(int year) //判断闰年
{
if (((year % 4==0) && (year % 100!=0)) || (year % 400==0))
return true;
else
return false;
}
int GetMonthDays(int year, int month)
{
if (year<1900 || month<1 || month>12)
{
return -1;
}
static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int days = monthDays[month];
if (month == 2 && IsLeapYear(year))
{
days += 1;
}
return days;
}
//d1 > d2
bool operator>(const Date& d)
{
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;
}
//d1==d2
bool operator==(const Date& d)
{
return _year == d._year&&_month == d._month&&_day == d._day;
}
//d1>=d2
bool operator>= (const Date& d)
{
return *this > d || *this == d;
}
//d1<d2
bool operator<(const Date& d)
{
return !(*this >= d);
}
//d1<=d2
bool operator<=(const Date& d)
{
return !(*this > d);
}
//d1!=d2
bool operator!=(const Date& d)
{
return !(*this == d);
}
bool IsInvalid()
{
if (_day<1 || _day>GetMonthDays(_year, _month) || _year < 1900 || _month<1 || _month>12)
return true;
else
return false;
}
Date operator-(int day)
{
if (day < 0)
{
return *this + (-day);
}
Date tmp(*this);//拷贝构造
tmp._day -= day;
while (tmp.IsInvalid())
{
--tmp._month;
if (tmp._month == 0)
{
tmp._year--;
tmp._month = 12;
}
tmp._day += GetMonthDays(tmp._year, tmp._month);
}
return tmp;
};
//d1 + 100
Date operator+(int day)
{
if (day < 0)
{
return *this - (-day);
}
Date tmp(*this);//拷贝构造
tmp._day += day;
while (tmp.IsInvalid())
{
tmp._day -= GetMonthDays(tmp._year, tmp._month);
++tmp._month;
if (tmp._month == 13)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
Date& operator+=(int day)
{
*this =*this+ day;
return *this;
}
Date& operator-=(int day)
{
*this =*this- day;
return *this;
}
Date& operator++()
{
*this += 1;
return *this;
}
Date operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& operator--()
{
*this -= 1;
return *this;
}
Date operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
//d1-d2
int operator-(const Date& d)
{
Date min = d, max = *this;
int tag = 1;
if (*this < d)
{
min = *this;
max = d;
tag = -1;
}
int count = 0;
while (min != max)
{
++min;
++count;
}
count = count*tag;
return count;
}
};
测试代码
#include"date.cpp" #include<iostream> using namespace std; void TestDate() { Date d1(2017, 7, 1); Date d2 = d1 + 1000; //Date d2 = d1 -100; d2.Display(); } int test() { Date d1(2017, 7, 1); Date d2(2015, 10, 1); return d1 - d2; } int main() { TestDate(); /*int count = test(); cout << count << endl;*/ system("pause"); return 0; }
运行结果