实现日期类的一些功能:
<span style="font-size:18px;">#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)
{ //参数检测
if ((year >= 1900)
&& (month >= 1 && month <= 12)
&& (day >= 1 && day <= _GetMonthDay(year, month)))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "非法日期" << endl;
_year = 1900;
_month = 1;
_day = 1;
}
// cout << "Date(size_t year , size_t month , size_t day)" << endl;
}
//构造函数
Date(const Date& d) //拷贝构造
{
_year = d._year;
_month = d._month;
_day = d._day;
}
~Date() //析构函数
{
cout << "~Date()" << endl;
}
操作符重载
Date& 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); // <
bool operator<=(const Date& d); //<=
Date operator+(int days); // 加天数
Date& operator+=(int days);// +=
Date operator-(int days); //减天数
int operator-(const Date& d); // 两个日期相隔天数
Date& operator-=(int days); //-=
Date& operator++(); //自加 天数 //前置++
Date operator++(int); //后置++
Date& operator--(); //前置--
Date operator--(int); //后置
bool IsLeap();//判断闰年?
int _GetMonthDay(int year,int month); //一个月有多少天
friend ostream& operator<<(ostream& os, const Date& d); //输出
friend istream& operator>>(istream& is, Date& d); //输入
private:
int _year;
int _month;
int _day;
};
/*********************************** 类外 *************************************/
bool Date:: IsLeap()
{
if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0)
{
return true;
}
return false;
}
int Date:: _GetMonthDay(int year,int month) //一个月有多少天
{
assert(month > 0 && month <= 12);
int arr_monthday[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day;
if ((!IsLeap()) && month == 2)
{
_day = arr_monthday[month] - 1;
}
day = arr_monthday[month];
return day;
}
Date& Date:: operator=(const Date& d) //赋值
{
if (this == &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
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)
{
return (_year >d._year) //年
|| (_year == d._year) && (_month > d._month)// 年-月
|| (_year == d._year) && (_month == d._month) && (_day > d._day);
//年-月-日
}
bool Date:: operator>=(const Date& d)
{
return (*this == d) || (*this > d);
}
bool Date:: operator<(const Date& d)
{
return (_year <d._year)
|| (_year == d._year) && (_month < d._month)
|| (_year == d._year) && (_month == d._month) && (_day < d._day);
}
bool Date:: operator<=(const Date& d)
{
return (*this == d) || (*this < d);
}
Date& Date::operator++() //自加 天数 //前置++
{
//Date tmp(*this);
_day++;
if (_day > _GetMonthDay(_year, _month))
{
_day -= _GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator++(int) //后置++
{
Date temp(*this) ;
_day++;
if (_day > _GetMonthDay(_year,_month))
{
_day = _GetMonthDay(_year, _month)-_day ;
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return temp;
}
Date& Date::operator--() //前置--
{
_day--;
if (_day < 1)
{
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
_day += _GetMonthDay(_year, _month) ;
}
return *this;
}
Date Date::operator--(int) //后置--
{
Date temp(*this);
_day--;
if (_day < 1)
{
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
_day += _GetMonthDay(_year, _month);
}
return temp;
}
Date Date::operator+(int days) /*****************加天数*****************/
{
Date tmp(*this);
if (days < 0)
{
return tmp - (-days);
}
tmp._day += days;
while (tmp._day> _GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= _GetMonthDay(tmp._year, tmp._month);
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
{
tmp._month++;
}
}
return tmp;
}
Date& Date:: operator +=(int days)// +=
{
*this = *this + days;
return *this; //返回*this,所以用 引用做且返回值
}
Date Date::operator-(int days) /***************减天数******************/
{
Date tmp(*this);
if (days < 0)
{
return tmp + (-days);
}
tmp._day -= days;
while (tmp._day <= 0) //注意没有1月0号。。。
{
if (tmp._month==1)
{
tmp._year--;
tmp._month = 12;
}//校正月数
else
{
--tmp._month;
}
tmp._day += _GetMonthDay(tmp._year, tmp._month);//上一个月的天数!!!
}
return tmp;
}
Date& Date:: operator-=(int days) //-=
{
*this = *this - days;
return *this;
}
int Date::operator-(const Date& d) //两个日期相隔天数
{//思路:比较两个日期的大小,找出最大的(最小的)
//通过最大的--(最小的++),days++
//(最大的==最小的)就可以了
Date Max(*this);
Date Min(d);
int flag = 1;
if ( *this < d)
{
//使用库函数 std::swap(Max,Min);
Max = d;
Min= *this;
flag = -1;
}
int days = 0;
while (Max != Min)
{
--Max;
++days;
}
return days*flag;
}
ostream& operator<<(ostream& os, const Date& d)
{
os << d._year << "-" << d._month << "-" << d._day << endl;
//os ;
//os ;
return os;
}
istream& operator>>(istream& is, Date& d)
{
cout << "输入日期:";
is >> d._year;
is >> d._month;
is>> d._day;
return is;
}</span>
/********************************* 测试 ***************************************/
int main()
{
Date d1(2016, 5, 31);
Date d2(2016,12,1);
cout << d1;
int ret= d2-d1 ;
Date d3 = d2 + 1;
Date d4(d2);
Date d5 = d1 - 1;
Date d6 = d1++;
Date d7;
cin >> d7;
cout << d7;
d1 += 1;
return 0;
}