您的位置:首页 > 编程语言 > C语言/C++

C++ 日期类函数 完善

2017-03-05 11:23 399 查看
#include<iostream>
using namespace std;

class Date
{
public:
friend ostream& operator<<(ostream& _cout,const Date& d)//友元函数,取ostream的引用使返回值连续输出
{
_cout<<d._year<<"-"<<d._month<<"-"<<d._day<<endl;
return _cout;
}

Date(int year = 1990,int month = 1,int day = 1)//构造函数
:_year(year)
,_month(month)
,_day(day)
{
if(!(_year > 0&&
(_month > 0&&_month < 13)&&
(_day > 0&&_day <= _GetMonthDays(_year,_month))))//判断非法日期
{
_year = 1990;
_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)//判断是否自己给自己赋值
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;//返回当前对象(返回d也可)
}
Date& operator++()//前置加加,返回引用
{
*this = *this + 1;
return *this;
}
Date& operator--()
{
*this = *this - 1;
return *this;
}
Date operator++(int)//后置加加,传参int区分前置加加,返回值不传引用
{
Date temp(*this);
*this = *this + 1;
return temp;
}
Date operator--(int)
{
Date temp(*this);
*this = *this - 1;
return temp;
}
Date operator+(int days)
{
if(days < 0)//加负天数
{
return *this - (0 - days);//返回Date operator-(int days)函数
}
Date temp(*this);//将*this值保存到临时变量temp中
int daysInMonth = 0;//用于接收_GetMonthDays(temp._year,temp._month)函数的值
temp._day += days;
while(temp._day > daysInMonth == _GetMonthDays(temp._year,temp._month))//
{
if(temp._month == 12)//如果为十二月则年份加1
{
temp._year += 1;
temp._month = 1;
}
else
temp._month += 1;//月份加一

temp._day = temp._day - _GetMonthDays(temp._year,temp._month);//加法运算后的天数减去这个月的天数
}
return temp;
}

Date operator-(int days)
{
if(days<0)
{
return *this + (0 - days);//返回Date operator+(int days)函数
}
Date temp(*this);
int daysInMonth = 0;
temp._day -= days;
while(temp._day <= 0)//减去days后若小于0则一直循环
{
if(temp._month = 1)
{
temp._year -= 1;
temp._month = 12;
}
else
temp._month -= 1;

temp._day += _GetMonthDays(temp._year,temp._month);//加上上一个月的天数
}
return temp;
}
int operator-(Date& d)
{
Date minDate(*this);
Date maxDate(d);
if(minDate > maxDate)
{
minDate = maxDate;
maxDate = *this;
}
size_t count = 0;
while(minDate < maxDate)
{
++minDate;
++count;
}

return count;
}

bool operator>(Date& d)
{
if(_year>d._year||
(_year == d._year && _month>d._month)||
(_year == d._year && _month == d._month && _day>d._day))
return true;

return false;
}
bool operator==(Date& d)
{
if(_year == d._year && _month == d._month && _day == d._day)
return true;

return false;
}
bool operator!=(Date& d)
{
return !(*this == d);
}
bool operator<(Date& d)
{
return !(*this>d || *this==d);//不是大于或等于
}

private:
int _GetMonthDays(int year,int month)//获取每个月的最大天数函数
{
int daysInMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//定义一个存放每个月天数(非闰年)的数组
if(2 == month&&Isleap(_year))
{
daysInMonth[month] += 1;
}
return daysInMonth[month];
}
bool Isleap(int year)const//判断闰年
{
if((0 == year%4&&0 != year%100)||0 == year%400)
{
return true;
}
return false;
}
private:
int _year;
int _month;
int _day;
};

int main()
{
Date d1(2017,3,4);
Date d2(2017,2,13);
cout<<d1++<<endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息