C++完成日期类

来源:互联网 发布:上海译文 知乎 编辑:程序博客网 时间:2024/05/18 02:50
#include <iostream>using namespace std;class Date { public: Date(int year = 2017, int month = 9, int day = 10) : _year(year) , _month(month) , _day(day) {} Date& operator=(const Date& d); // 前置++ Date& operator++(); // 后置++ Date operator++(int); Date& operator--(); Date operator--(int); //days天之后的日期 Date operator+(int days); // days天之前的日期 Date operator-(int days); // 两个日期之间的距离 int operator-(const Date& d); bool operator==(const Date& d); bool operator!=(const Date& d); bool operator>(const Date& d); bool operator<(const Date& d); private: int _year; int _month; int _day; }; Date& Date::operator=(const Date& d){    if(this!=&d)    {        _year=d._year;        _month=d._month;        _day=d._day;    }    return *this;}Date& Date::operator++(){    ++_day;//日期加1    switch(_month)//日期向月进位    {    case 1:    case 3:    case 5:    case 7:    case 8:    case 10:    case 12:        if(32==_day)//大月        {            _day=1;            ++_month;        }        break;    case 2:        if((0==_year%4)&&(0!=_year%100)||(0==_year%400))//润年        {            if(30==_day)            {                _day=1;                ++_month;            }        }        else//平年        {            if(29==_day)            {                _day=1;                ++_month;            }        }        break;    default://小月        if(31==_day)        {            _day=1;            ++_month;        }        break;    }    if(13==_month)//月向年进位    {        _month=1;        ++_year;    }    return *this;}Date Date::operator++(int){    Date tmp(*this);    ++(*this);    return tmp;}Date& Date::operator--(){    --_day;    if(0==_day)//日期为0向月借位        switch(--_month)//月份-1,再具体分情况看日期置为多少    {//--_month:0-11        case 0://月份为0向年借位            --_year;            _month=12;            _day=31;            break;        case 1:        case 3:        case 5:        case 7:        case 8:        case 10://大月            _day=31;            break;        case 2:            if((0==_year%4)&&(0!=_year%100)||(0==_year%400))//润年                _day=29;            else//平年                _day=28;            break;        default://小月            _day=30;    }    return *this;}Date Date::operator--(int){    Date tmp(*this);    --(*this);    return tmp;}Date Date::operator+(int days){    if(days<0)//输入天数为负数,改为算|days|天之前的日期        return (*this)-(-days);    while(days--)    {        ++(*this);    }    return *this;}Date Date::operator-(int days){    if(days<0)//输入天数为负数,改为算|days|天之后的日期        return (*this)+(-days);    while(days--)    {        --(*this);    }    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 (_year!=d._year)||(_month!=d._month)||(_day!=d._day);}bool Date::operator>(const Date& d){    if(_year!=d._year)//比较年份        return _year>d._year ;    else    {//年份相同比较月份        if(_month!=d._month)            return _month>d._month;        else//月份不同比较日            return _day>d._day;    }}bool Date::operator<(const Date& d){    if(_year!=d._year)//比较年份        return _year<d._year ;    else    {//年份相同比较月份        if(_month!=d._month)            return _month<d._month;        else//月份不同比较日            return _day<d._day;    }}int Date::operator-(const Date& d){    int count=0;    Date tmp(d);//防止d的值被改变    if(*this>d)//第一个日期大于第二个        while(*this!=tmp)        {            ++tmp;            ++count;        }    else        while(*this!=tmp)//第一个日期小于第二个        {            --tmp;            --count;        }    return count;}int main(){    Date a(2017,10,25);    Date b(1996,3,4);    int c=a-b;    printf("%d\n",c);    return 0;}
原创粉丝点击