C++类与对象——日期类

两个日期类比较大小通过对C++类与对象的学习和思考。实际生活问题运用所学,将日期分装成一个类,完成简单的计算。
C++编写一个日期类,可以完成一些基础的日期计算,一个日期是否合法,闰年天数,两个日期相差的天数,日期的比较,日期前后多少天的计算等。

#pragma once 
#include<assert.h>

class Date
{
public:
    Date(int year=1900, int month=1, int day=1)    //构造函数以及初始化列表
        : _year(year)
        , _month(month)
        , _day(day)
    {
        assert(IsInvalid());                        //判断日期是否合法
    }

    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;
    }

    Date operator+(const int day)                    //日期类加一个整型天数
    {
        if (day < 0)
        {
            return *this - (-day);
        }
        Date tmp(*this);
        tmp._day += day;
        while (tmp.IsInvalid() == false)
        {
            tmp._day -= GetMonthDays(tmp._year, tmp._month);
            if (tmp._month == 12)
            {
                tmp._year++;
                tmp._month = 1;
            }
            else
            {
                tmp._month++;
            }
        }

        return tmp;
    }

    Date operator-(int day)                         //日期类减一个整型天数
    {
        if (day < 0)
            return *this + (-day);
        Date tmp(*this);
        tmp._day -= day;
        while (tmp.IsInvalid() == false)
        {
            if (tmp._month == 1)
            {
                tmp._year--;
                tmp._month = 12;
            }
            else
            {
                tmp._month--;
            }
            int day = GetMonthDays(tmp._year, tmp._month);
            tmp._day += day;
        }
        return tmp;
    }

    Date& operator +=(int day)                          //日期类+=一个整型天数
    {
        return *this = *this + day;
    }

    Date& operator -=(int day)
    {
        return *this = *this - day;
    }

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

    Date operator ++(int)                                //日期类后置++
    {
        Date tmp(*this);
        *this += 1;
        return tmp;
    }

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

    Date operator--(int)                                 //日期类后置--
    {
        Date tmp(*this);
        *this -= 1;
        return tmp;
    }
    int operator-(const Date& d)                          //两个日期类相减相差天数
    {
        Date tmp(*this);
        int a = 0;
        if (_year >= d._year)
        {
            if (d._month == tmp._month && d._year == tmp._year)
                return tmp._day - d._day;
            tmp._month--;
            while (!((tmp._year==d._year)&&(tmp._month==d._month)))
            {
                a += GetMonthDays(tmp._year, tmp._month);
                tmp._month--;
                if (tmp._month == 0)
                {
                    tmp._month = 12;
                    tmp._year--;
                }
            }
            return a + tmp._day + (GetMonthDays(d._year, d._month) - d._day);
        }
        else
        {
            tmp = d;
            tmp._month--;
            while (!((tmp._year == _year) && (tmp._month == _month)))
            {
                a += GetMonthDays(tmp._year, tmp._month);
                tmp._month--;
                if (tmp._month == 0)
                {
                    tmp._month = 12;
                    tmp._year--;
                }
            }
            return -(a + tmp._day + (GetMonthDays(_year, _month) - _day));
        }
    }

        void Display()                              //打印一个日期类
    {
        cout << _year << "/" << _month << "/" << _day << endl;
    }

    bool IsInvalid()                                 //判断一个日期是否合法
    {
        if (_year >= 1900 && _month > 0 && _month<13
            && _day>0 && _day <= GetMonthDays(_year, _month))
        {
            return true;
        }
        return false;
    }

    int GetMonthDays(int year, int month)             //判断是否为闰年,二月天数改变
    {
        static int _monthDates[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (month != 2)
        {
            return _monthDates[month];
        }
        else
        {
            if ((year % 4 == 0 && year % 100 != 0)
                || (year % 400 == 0))
            {
                return _monthDates[2] + 1;
            }
            return _monthDates[2];
        }
    }

    bool operator>(const Date& d)                         //两个日期类比较大小
    {
        if (_year > d._year)
            return true;
        else if (_year < d._year)
            return false;
        else
        {
            if (_month>d._month)
                return true;
            else if (_month < d._month)
                return false;
            else
            {
                if (_day>d._day)
                    return true;
                else if (_day < d._day)
                    return false;
                else
                    return false;
            }
        }
    }

    bool operator<(const Date& d)                            //两个日期类比较大小
    {
        if (_year < d._year)
            return true;
        else if (_year > d._year)
            return false;
        else
        {
            if (_month<d._month)
                return true;
            else if (_month > d._month)
                return false;
            else
            {
                if (_day < d._day)
                    return true;
                else if (_day > d._day)
                    return false;
                else
                    return false;
            }
        }
    }

    bool operator==(const Date& d)                       //两个日期类是否相等
    {
        if (d._year == _year&&d._day == _day&&d._month == _month)
            return true;
        return false;
    }

    bool operator>=(const Date& d)                       //两个日期类比较大小
    {
        if (_year > d._year)
            return true;
        else if (_year < d._year)
            return false;
        else
        {
            if (_month>d._month)
                return true;
            else if (_month < d._month)
                return false;
            else
            {
                if (_day>d._day)
                    return true;
                else if (_day < d._day)
                    return false;
                else
                    return true;
            }
        }
    }

    bool operator<=(const Date& d)                          //两个日期类比较大小
    {
        if (_year < d._year)
            return true;
        else if (_year > d._year)
            return false;
        else
        {
            if (_month<d._month)
                return true;
            else if (_month > d._month)
                return false;
            else
            {
                if (_day < d._day)
                    return true;
                else if (_day > d._day)
                    return false;
                else
                    return true;
            }
        }
    }

    ~Date()                                                 //析构函数
    {

    }

private:
    int _year;
    int _month;
    int _day;
};

需要注意:
在判断一个日期是否合法,首先需要判断是够为闰年,其次在判断天数是否合法。
两个日期类相差天数,先判断日期大小,然后在确定返回值,两个日期类相差天数=两日期类中月之间月的天数+第一个日期类的天数+第二个日期类当月的天数减去第二个类的天数例如日期类d1(2017,8,24)和日期类d2(2017,5,5)之间的天数,相当于2017年6,7月天数之和+24天+(5月天数-5))。
日期类的比较返回值为布尔值。
日期类的打印也可以修改成输入输出流的运算符重载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值