日期类(Date)_运算符重载简单应用

来源:互联网 发布:淘宝手机店铺装修尺寸 编辑:程序博客网 时间:2024/06/16 09:41

面对日期类里面的加加减减涉及到的进位和借位,我更倾向使用一个保存了每个月天数的数组代替,虽然很占内存,但是方法简化了,而且不需要遍历数组,只需要拿到月份的数字,用月份的数字当下标就可以得到这个月的天数(哈希表的简单应用),这种方法很好用,尤其是在需要不断的遍历数组的时候,可以用这种方法代替遍历.
在写代码的时候,还需要注意代码的复用,这样写代码的效率会变高,不会把时间都浪费在写很多类似的逻辑上,比如在本文的重载>,<,==,!=,>=,<=的时候就体现了出来.

#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 (_year<1900            || _month<0            || _month>12            || _day<0            || _day>calenter[_month])            exit(-1);    }    bool operator > (const Date &d) const    {        if (_year > d._year)            return true;        else if (_year == d._year&&_month > d._month)            return true;        else if (_year == d._year&&_month > d._month&&_day > d._day)            return true;        else            return false;    }    bool operator ==(const Date&d)const    {        if (_year == d._year&&_month == d._month&&_day == d._day)            return true;        else            return false;    }    bool operator >=(const Date&d)const    {        return (*this > d || *this == d);    }    bool operator <=(const Date&d)const    {        return !(*this > d);    }    bool operator >(const Date&d)const    {        return !(*this > d || *this == d);    }    bool operator !=(const Date&d)const    {        return!(*this == d);    }    bool IsLeapYear(Date d)    {        if ((d._year % 100 != 0 && d._year % 4 == 0) || (d._year % 400 == 0))        {            d.calenter[2] = 29;            return true;        }        d.calenter[2] = 28;        return false;    }    Date operator+(int day)const//+ 不是+=,只需要操作,不能给this改变值!!!    {        if (day<0)        {            *this - (-day);        }        else        {            Date tmp = *this;            tmp._day += day;            while (tmp._day > calenter[_month])            {                tmp._day -= calenter[_month];                tmp._month++;                if (tmp._month > 12)                {                    tmp._year++;                    tmp._month = 1;                    IsLeapYear(tmp);                }            }            return tmp;        }    }    Date operator+=(int day)    {        return *this = *this + day;    }    Date operator-(int day)const    {        if(day<0)        {            return *this + (-day);        }        Date tmp = *this;        tmp._day -= day;        while (tmp._day < 0)        {            if (tmp._month == 1)            {                tmp._year--;                IsLeapYear(tmp);                tmp._month = 12;                tmp._day += 31;            }            else            {                tmp._day += calenter[_month - 1];                tmp._month++;            }        }        return tmp;    }    Date operator-=(int day)    {        return *this = *this - day;    }    Date operator++()//前置    {        return ((*this) + 1);    }    Date operator++(int)//后置    {        Date tmp = *this;        *this=*this+1;        return tmp;    }    Date operator--()    {        return *this = *this - 1;    }    Date operator--(int)    {        Date tmp = *this;        *this = *this - 1;        return tmp;    }    int operator-(const Date& d)    {        Date tmp1;        Date tmp2;        int count = 0;        if (*this > d)        {            tmp1 = d;            tmp2 = *this;        }        else        {            tmp1 = *this;            tmp2 = d;        }        while (tmp1!=tmp2)        {            tmp1++;            count++;        }        return count;    }    ostream operator <<(ostream&os,const Date &d)    {        out<<d._yeaar<<-<<d._month<<-<<d.day<<endl;        return out;    }private:    int _year;    int _month;    int calenter[13]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    int _day;};
原创粉丝点击