您的位置:首页 > 其它

简易日期类

2016-06-05 18:37 190 查看
【任务要求】

    1.实现:日期 + 天数 = 日期;

    2.实现:日期 - 天数 = 日期;
    3.实现:日期 - 日期 = 天数;

【代码实现】
#include <iostream>
using namespace std;

class Date
{
friend ostream& operator<<(ostream& os ,const Date& d);//友元
public:
Date(int year = 1900, int month = 1, int day = 1)//构造函数
:_year(year)
,_month(month)
,_day(day)
{
if (IsInvalidDate()) //处理无效日期
{
_year = 1900;
_month = 1;
_day = 1;
}
}
Date(const Date& d)//拷贝构造函数
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d) //赋值运算符重载
{
if(this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
public:
/*判断日期是否有效*/
bool IsInvalidDate()
{
if ((_year<1900)
||((_month < 1) || (_month > 12))
||((_day < 1) || _day > GetMonthDay()))
{
return true;
}
return false;
}
/*判断_year是否为闰年*/
bool LeapYear()
{
return (0 == _year %4 &&0 != _year % 100)||(0 == _year % 400);
}
/*得到_month有多少天*/
int GetMonthDay()
{
int Days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(LeapYear())
{
Days[2] += 1;
}
return Days[_month];
}
/*判断两个日期是否相等*/
bool operator==(const Date& d)
    {
return (this->_year == d._year) && (this->_month == d._month) && (this->_day == d._day);
    }
bool operator!=(const Date& d)
    {
return !(*this == d);
    }
/*判断*this与d的大小关系*/
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;
    }
bool operator<(const Date& d)
{
return !(*this > d);
}
bool operator>=(const Date& d)
{
return ((*this > d)||(*this == d));
}
bool operator<=(const Date& d)
{
return ((*this < d)||(*this == d));
}
public:
void GetNowDate()//得到新的日期
{
while(_day <= 0)
{
if (_month == 1)
{
_month = 12;
_year -= 1;	 
}
else 
{
_month -= 1;
}
_day += GetMonthDay();
}
while(_day > GetMonthDay())
{
_day -= GetMonthDay(); 
if(_month == 12)
{
_month = 1;
_year += 1;
}
else
{
_month += 1;
} 
}
}

Date operator+(int day)	//日期 + 天数 =日期
{
int Days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date d(*this);
d._day += day;
d.GetNowDate();
return d;
}
Date operator-(int day)	//日期 - 天数 =日期
{
int Days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date d(*this);
d._day -= day;
d.GetNowDate();
return d;
}
Date& operator+=(int day)
{
_day += day;
this->GetNowDate();
return *this;
}
Date& operator-=(int day)
{
_day -= day;
this->GetNowDate();
return *this;
}
int operator-(const Date& d) //日期-日期 =天数
{
int day = 0;
if(*this > d)
{
Date d1 = *this;
*this = d;
}
while(*this != d)
{
*this += 1;
day++;
}
return day;
}
private:
int _year;
int _month;
int _day;
};

ostream& operator<<(ostream& os ,const Date& d)  //输出运算符重载   
{
os << d._year << "-" << d._month << "-" <<d._day << endl;
    return os;
}

int main()
{				  
Date d(2016, 3, 2);
Date d1(1995, 7, 14);
int ret = d-d1;
cout << "By the end of "
     << d <<"You have already lived for "
     << ret << " days" << endl;
system("pause");
return 0;
}
结果:




本文出自 “Pzd流川枫” 博客,请务必保留此出处http://xujiafan.blog.51cto.com/10778767/1746869
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: