C++日期类

学会调用写好的函数完成其他更复杂的函数。


#include <iostream>
using namespace std;

class Date
{
public:
    friend ostream& operator<<(ostream&, Date&);
    Date(int year = 2018, int month = 6, int day = 24)
        : _year(year)
        , _month(month)
        , _day(day)
    {
        //检测合法
        if (!is_valid()) {
            _year = 1900;
            _day = 1;
            _month = 1;
        }
    }

    Date(const Date& d)
        : _year(d._year)
        , _month(d._month)
        , _day(d._day)
    {}

    // 当前日期days天后是什么日期? 
    Date operator+(int days)
    {
        Date tmp(*this);
        tmp._day += days;
        int monthDay = 0;
        while (tmp._day > (monthDay = get_month_day(tmp._year, tmp._month))) {
            tmp._day -= monthDay;
            ++tmp._month;
            if(tmp._month == 13){
                tmp._month = 1;
                tmp._year += 1;
            }
        }
        return tmp;
    }

    // 当前日期days天前是什么日期? 
    Date operator-(int days)
    {
        Date tmp(*this);
        int monthDay = 0;
        tmp._day -= days;
        while (tmp._day <= 0) {
            --tmp._month;
            monthDay = get_month_day(tmp._year, tmp._month);
            tmp._day += monthDay;
            if (tmp._month == 0) {
                tmp._month = 12;
                tmp._year -= 1;
            }
        }
        return tmp;
    }

    // 两个日期之间差了多少天? 
    int operator-(const Date& d)
    {
        Date greater = *this;
        Date less = d;
        if (greater < less) {
            Date tmp(less);
            less = greater;
            greater = tmp;
        }
        int count = 0;
        while (greater != less) {
            --greater;
            ++count;
        }
        return count;
    }

    // 日期比大小 
    bool operator>(const Date& d)
    {
        if (_year < d._year
            || _year == d._year && _month < d._month
            || (_year == d._year && _month == d._month) && _day <= d._day) {
            return false;
        }
        return true;
    }
    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 !operator==(d);
    }
    Date& operator=(const Date& d)
    {
        if (this != &d) {
            _day = d._day;
            _month = d._month;
            _year = d._year;
        }
        return *this;
    }

    // 重载取地址符号 
    Date* operator&()
    {
        return this;
    }

    // 前置++ 
    Date& operator++()
    {
        (*this) = (*this) + 1;
        return *this;
    }

    // 后置++ 
    Date operator++(int)
    {
        Date tmp(*this);
        ++(*this);
        return tmp;
    }

    // 前置-- 
    Date& operator--()
    {
        (*this) = (*this) - 1;
        return *this;
    }

    // 后置--
    Date operator--(int)
    {
        Date tmp(*this);
        --(*this);
        return tmp;
    }
private:
    int is_valid()
    {
        if (_year<1 || _month < 1 || _month > 12 ||  _day <0 ||_day> get_month_day(_year, _month)) {
            return false;
        }
        return true;
    }
    int get_month_day(int year, int month)
    {
        int MonthDay[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
        if(is_leap_year(year)){
            MonthDay[1] += 1;
        }
        return MonthDay[month - 1];
    }
    int is_leap_year(int year)
    {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
    int _year;
    int _month;
    int _day;
};

ostream& operator<< (ostream& cout, Date& d) {
    cout << d._year << "/" << d._month << "/" << d._day;
    return cout;
}
int main()
{
    Date d(2018, 6, 23);
    Date d2(2018, 10, 1);
    cout << d << endl;
    d + 100;
    d2 - 100;
    d - d2;
    d+100 < d2;
    Date d3 = d2++;
    d3--;
}

猜你喜欢

转载自blog.csdn.net/hanzheng6602/article/details/80794426