c++---实现日期类

更多点子:c++—目录索引(知识小渠道)


#pragma once

#include<iostream>
#include<assert.h>
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<1 || month>12
            || day<1 || day>GetMonthDay(year, month))
            assert(false);
    }
    /*
    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;
    }
    bool IsLeapYear(int year)
    {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            return true;
        }
        return false;
    }
    int GetMonthDay(int year, int month)
    {
        assert(month > 0 && month<13);
        static int monthdays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int days = monthdays[month];
        if (month == 2&&IsLeapYear(year))
            days += 1;
        return days;
    }
    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 true;
        return false;
    }
    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 operator==(const Date& d) const
    {
        return _year == d._year
            && _month == d._month
            && _day == d._day;
    }
    bool operator!=(const Date& d) const
    {
        return !(*this == d);
    }
    Date operator+(int day) const const
    {
        if (day<0)
        {
            return *this - (-day);
        }
        Date ret(*this);
        ret._day += day;
        while (ret._day > GetMonthDay(ret._year, ret._month))
        {
            ret._day -= GetMonthDay(ret._year, ret._month);
            ret._month++;
            if (ret._month == 13)
            {
                ret._year++;
                ret._month = 1;
            }
        }
        return ret;
    }
    Date operator-(int day) const
    {
        if (day < 0)
        {
            return *this + (-day);
        }
        Date ret(*this);
        ret._day -= day;
        while (ret._day <= 0)
        {
            ret._month--;
            if (ret._month==0)
            {
                ret._year--;
                ret._month = 12;
            }
            ret._day += GetMonthDay(ret._year, ret._month);
        }
        return ret;
    }
    Date& operator+=(int day)
    {
        *this=*this + day;
        return *this;
    }
    Date& operator-=(int day)
    {
        *this = *this - day;
        return *this;
    }
    Date& operator++()//前置 编译器约定了前置没有参数
    {
        *this += 1;
        return *this;
    }
    Date operator++(int) const//后置,编译器约定好这是int,int(),int i
    {
        Date tmp(*this);
        *this += 1;
        return tmp;
    }
    Date operator--() const//前置
    {
        *this -= 1;
        return *this;
    }
    Date operator--(int) const//后置
    {
        Date tmp(*this);
        *this -= 1;
        return tmp;
    }
    int operator-(const Date& d) const
    {
        int flag = 1;
        Date max = *this, min = d;
        if (*this < d)
        {
            max = d;
            min = *this;
            flag = -1;
        }
        int days = 0;
        while (min < max)
        {
            ++min;
            ++days;
        }
        return days*flag;
    }

    void Show() const
    {
        cout << _year << "-" << _month << "-" << _day << endl; 
    }
private:
    int _year;
    int _month;
    int _day;
};

void testDate()
{
    Date d1(2008, 4, 6);
    d1.Show();
    Date().Show();//匿名对象--生命周期只有当前一行
}

如果有什么不对的地方,可以评论告诉我,望指导!

猜你喜欢

转载自blog.csdn.net/phonycat/article/details/79835956
今日推荐