C++实现日期类

--------------------------------------------------------------------------
          /*
          **功能:实现日期的简单操作
          **
          **
          **基本的成员函数:
          **          构造函数,拷贝构造函数,析构函数,赋值运算符重载,操作符重载(两个日期间比较大小)
          **
          **日期类功能函数:
          **                1:计算一个日期加上多少天数后的日期       
          **                2:把该日期改为加上指定数目的天数后的日期
          **                3:一个日期减上多少天数后的日期 
          **                4:把该日期改为减去指定数目的天数后的日期
          **                5:该日期加1(前置++)(后置++
          **                6:该日期减1(前置--)(后置--
          **                7:计算某日期到未来某日期间隔的天数
          **
          **
          **                                  By :niuxiaoke
          **                                                         
          */
//---------------------------------------------------------------------------
#include<iostream>
using namespace std;

class Date{
public:
    Date(int year = 1900,int month = 1,int day = 1)//构造
        :_year(year)
        ,_month(month)
        ,_day(day)
    {
        if(!isInvalidDate(_year,_month,_day))
        {
            cout<<"日期不正确,已自动修正"<<endl;
            _year = 1900;
            _month = 1;
            _day = 1;
        }
    }
    Date(const Date& d)//拷贝构造
    {
        if(this != &d)
        {
            _year = d._year;
            _month = d._month;
            _day = d._day;
        }

    }

    ~Date()//析构函数
    {}

    int GetMonthDay(int year,int month)
    {
        int days[13]= {-1,31,28,31,30,31,30,31,31,30,31,30,31};

        if((year%4 == 0 && year%100!=0) || (year%400 == 0))
        {
            days[2] = 29;
        }

        return days[month];
    }


    bool isInvalidDate(int year,int month,int day)
    {
        if( year >=0 &&
            (month > 0 && month <13)
            &&(day > 0 && day <= GetMonthDay(year,month)))

            return true;
        else
            return false;

    }

    void Display()
    {
        cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
    //d1 > d2 
        bool operator>(const Date& d)
        {
            return (_year > d._year)||
                (_year == d._year && _month > d._month)
                ||(_year == d._year && _month == d._month && _day > d._day);
        }
    bool operator>= (const Date& d)
    {
        return ((*this> d)||(*this ==d));
    }
    bool operator<(const Date& d)
    {
        return !(*this > d);
    }
    bool operator<=(const Date& d)
    {
        return (*this < d)||(*this == d);
    }
    bool operator==(const Date& d)
    {
        return (_year == d._year && _month == d._month && _day == d._day);
    }
    bool operator!= (const Date& d)
    {
        return !(*this == d);
    }
    Date& operator=(const Date& d)
    {
        if(*this != d)
        {
            _year = d._year;
            _month = d._month;
            _day = d._day;
        }
        return *this;
    }

    //d1 + 100 
    Date operator+(int day)
    {
        if(day < 0)
        {
            return operator-(-day);
        }
        Date tmp = *this;
        if(isInvalidDate(tmp._year,tmp._month,tmp._day+day))
        {
            tmp._day += day ;
            return tmp;

        }
        int Sumday = tmp._day + day;
        while(Sumday > GetMonthDay(tmp._year,tmp._month))
        {
            Sumday = Sumday-GetMonthDay(tmp._year,tmp._month);
            tmp._month++;
            if(tmp._month ==13)
            {
                tmp._year++;
                tmp._month = 1;
            }else
            {
                tmp._day = Sumday;
            }
        }
        return tmp;
    }
    Date& operator+=(int day)
    {
        *this = *this + day;
        return *this;
    }
    Date operator-(int day)
    {
        if(day < 0)
        {
            return operator+(-day);
        }
        Date tmp = *this;

        while(day >= tmp._day)
        {
            day = day -tmp._day;
            if(tmp._month == 1)
            {
                tmp._year--;
                tmp._month = 12;

            }
            else
            {
                tmp._month--;
            }
            tmp._day = GetMonthDay(tmp._year,tmp._month);
        }
        tmp._day = tmp._day - day;
        return tmp;
    }
    Date& operator-=(int day)
    {
        *this = *this - day;
        return *this;
    }
    Date& operator++() //前置加加
    {
        _day = _day+1;
        if(_day > GetMonthDay(_year,_month))
        {
            _day = _day - GetMonthDay(_year,_month);
            _month++;
            if(_month == 13)
            {
                _month = 1;
                _year++;
            }
        }
        return *this;
    }
    Date operator++(int) //后置加加
    {
        Date tmp = *this;
        operator++();
        return tmp;
    }
    Date& operator--() //前置减减
    {
        if(_day > 1)
        {
            _day --;
        }
        else
        {
            if(_month == 1)
            {
                _month = 12;
                _year --;
                _day = GetMonthDay(_year,_month);
            }else
            {
                --_month;
                _day = GetMonthDay(_year,_month);
            }
        }
        return *this;
    }
    Date operator--(int)
    {
        Date tmp = *this;
        operator--();
        return tmp;
    }
    int operator-( Date& d) //计算两个日期之间的天数
    {
        if(_year < d._year)
        {
            Date tmp = *this;
            *this = d;
            d = tmp;
        }
        else if(_year == d._year && _month < d._month)
        {
            Date tmp = *this;
            *this = d;
            d = tmp;
        }
        else if(_year == d._year && _month == d._month && _day < d._day)
        {
            Date tmp = *this;
            *this = d;
            d = tmp;
        }
        Date tmp1(*this);
        Date tmp2(d);
        int ret = 0;
        while(tmp1 != tmp2)
        {
            tmp2++;
            ret++;
        }
        return ret;
    }

private:
    int _year;
    int _month;
    int _day;


};

void test1()
{
    Date d1(2018, 3,20);
         d1.Display();
         //Date ret = d1+46;
          //   ret.Display();
             /*Date ret=d1+70;
             ret.Display()*/
             //Date ret=d1-25;
             //ret.Display();
            // Date ret = d1 += 70;
             //ret.Display();
             //d1.Display();
             /*Date ret = d1 -= 70;
             ret.Display();
             d1.Display();
             d1++;
             d1.Display();
             ++d1;
             d1.Display();
             --d1;
             d1.Display();
             d1--;
             d1.Display();*/
             Date d2(2019, 1, 1);
         d2.Display();
         int ret = d1 - d2;
         cout <<d1 - d2 << endl;
}


int main()
{
    test1();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niukeming/article/details/79621948
今日推荐