基本功能实现:
bool operator>(const Date& d);
operator>=
operator<
operator<=
operator==
operator!=
Date operator+(int day);
Date operator+=(int day);
Date operator-(int day);
Date operator-=(int day);
Date operator++();
Date operator++(int);
Date operator--();
Date operator--(int);
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool Isvalue()
{
if (_month > 0 && _month<13 &&
_day<=GetMonthday() && _day>0
&& _year>0)
return true;
else
return false;
}
bool operator<(const Date &d2)
{
assert(Isvalue());
if (_year < d2._year)
{
return true;
}
else if (_month < d2._month)
{
return true;
}
else if (_day < d2._day)
{
return true;
}
else
return false;
}
bool operator>(const Date &d2)
{
assert(Isvalue());
if (_year > d2._year)
{
return true;
}
else if (_month > d2._month)
{
return true;
}
else if (_day > d2._day)
{
return true;
}
else
return false;
}
bool operator<=(const Date &d2)
{
return !(*this > d2);
}
bool operator>=(const Date &d2)
{
return !(*this<d2);
}
bool operator!=(const Date &d2)
{
return ((*this<d2)||(*this>d2));
}
bool operator==(const Date &d2)
{
return !((*this<d2)||(*this>d2));
}
int GetMonthday()
{
int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((_month == 2)&&((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))
return 29;
else
return monthDays[_month];
}
Date operator+(int day)
{
if (day<0)
{
return *this - (-day);
}
Date tmp(*this);
tmp._day += day;
while (tmp._day >tmp.GetMonthday())
{
tmp._day -= tmp.GetMonthday();
tmp._month++;
if (tmp._month > 12)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
Date operator-(int day)
{
if (day<0)
{
return *this + (-day);
}
Date tmp(*this);
tmp._day -= day;
while (tmp._day <= 0)
{
tmp._month--;
if (tmp._month == 0)
{
tmp._year--;
tmp._month = 12;
}
tmp._day += tmp.GetMonthday();
}
return tmp;
}
Date &operator=(const Date &d)
{
if ((*this != d))
{
_year = d._year;
_month = d._month;
_day = d._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;
}
Date &operator+=(int day)
{
if (day < 0)
{
return (*this = *this - day);
}
*this = *this + day;
return *this;
}
Date &operator-=(int day)
{
if (day < 0)
{
return (*this = *this +(-day));
}
*this = *this - day;
return *this;
}
int operator-(const Date &d)
{
int tag = 1;
Date big(*this);
Date less(d);
if (*this < d)
{
big = d;
less = *this;
tag = -1;
}
int count = 0;
while (big>less)
{
++count;
less++;
}
return count*tag;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2017, 10, 26);
Date d2(2017, 2, 14);
d1.Display();
d2.Display();
cout << (d1.operator<(d2)) << endl;
cout << (d1.operator>(d2)) << endl;
cout << (d1.operator<=(d2)) << endl;
cout << (d1.operator>=(d2)) << endl;
cout << (d1.operator!=(d2)) << endl;
cout << (d1.operator==(d2)) << endl;
Date d3 = d1 + 2000;
d3.Display();
Date d4 = d1 - 52000;
d4.Display();
d1 += 6;
d1.Display();
d1 -= 6;
d1.Display();
d1++;
d1.Display();
++d1;
d1.Display();
d1--;
d1.Display();
--d1;
d1.Display();
d2 = d1 - 100;
cout<<(d1-d2)<<endl;
}
运行结果: