日期类:对一个或者多个日期进行比较、加、减、自增、自减等运算,得到想要的结果。
class Date
{
public:
Date(int year = 1990, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (_year<1990 || _month<1 || _month >12 || _day<1 || _day > GetMonth(_year,_month))
{
cout << "Invaid Date" << endl;
return;
}
}
bool IsLeap(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
return true;
}
return false;
}
int GetMonth(int year,int month)
{
int monthday[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeap(year))
{
monthday[2] = 29;
}
else
{
monthday[2] = 28;
}
return monthday[month];
}
Date(const Date& d)
:_year(d._year)
, _month(d._month)
, _day(d._day)
{}
Date& operator= (const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
~Date()
{}
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);
bool operator<= (const Date& d);
Date operator+ (int day);
Date& operator+= (int day);
Date operator- (int day);
Date& operator-= (int day);
Date operator++ (int);
Date& operator++ ();
Date operator-- (int);
Date& operator-- ();
int operator- (const Date& d);
friend ostream& operator<< (ostream& os, const Date& d);
private:
int _year;
int _month;
int _day;
};
类外定义成员函数如下:
ostream& operator<< (ostream& os, const Date& d)
{
os << d._year << ".";
os << d._month << ".";
os << d._day << endl;
return os;
}
bool Date::operator== (const Date& d)
{
return _year == d._year
&& _month == d. _month
&& _day == d._day;
}
bool Date::operator!= (const Date& d)
{
return !(*this == d);
}
bool Date::operator> (const Date& d)
{
if (_year > d._year)
{
return true;
}
if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
if (_month = d._month)
{
if (_day > d._day)
{
return true;
}
}
}
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+ (int day)
{
if (day < 0)
{
day = _day;
return (*this - day);
}
Date tmp(*this);
if (day == 0)
{
return tmp;
}
tmp._day += day;
int GetDay = GetMonthDay(tmp._year,tmp._month);
if (tmp._day > GetDay)
{
tmp._day -= GetDay;
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
{
tmp._month++;
}
}
return tmp;
}
Date& Date::operator+= (int day)
{
Date tmp(*this);
tmp = (*this) + day;
*this = tmp;
return *this;
}
Date Date::operator- (int day)
{
if (day < 0)
{
day = -day;
return (*this + day);
}
Date tmp(*this);
if (day == 0)
{
return tmp;
}
tmp._day -= day;
while (tmp._day <= 0)
{
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
{
tmp._month--;
}
tmp._day = GetMonthDay(tmp._year, tmp._month) + tmp._day;
}
return tmp;
}
Date& Date::operator-= (int day)
{
Date tmp(*this);
tmp = (*this) - day;
*this = tmp;
return *this;
}
Date Date::operator++ (int)
{
Date tmp(*this);
(*this) += 1;
return tmp;
}
Date& Date::operator++ ()
{
(*this) += 1;
return *this;
}
Date Date::operator-- (int)
{
Date tmp(*this);
(*this) -= 1;
return tmp;
}
Date& Date::operator-- ()
{
(*this) -= 1;
return *this;
}
int Date::operator- (const Date& d)
{
Date tmp = *this;
Date ret = d;
int day = 0,flag = 0;
if (tmp < ret)
{
tmp = d;
ret = *this;
flag = 1;
}
while (ret != tmp)
{
ret += 1;
day++;
}
if (flag)
{
day = -day;
}
return day;
}
解析:
+、+=、-、-+、++、- - 等运算符都是对一个对象实现加1天操作