c++日期类的实现

#include <iostream>
#include<stdlib.h>
using namespace std;
class Date
{
    friend ostream& operator<<(ostream& out, const Date& d);
public:
    // 全缺省的构造函数
    Date(int year = 0, int month = 0, int day = 0) :m_year(year), m_month(month), m_day(day)
    {
    }
    // 拷贝构造函数
    Date(const Date& d)
    {
        m_year = d.m_year;
        m_month = d.m_month;
        m_day = d.m_day;
    }
    // 赋值运算符重载
    Date& operator=(const Date& d)
    {
        if (this != &d)
        {
            m_year = d.m_year;
            m_month = d.m_month;
            m_day = d.m_day;
        }
    }
    //获取天数
    int GetDayByYM(int year, int month)
    {
        //保存一到十二月的天数
        int days[] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };
        if (month == 2)
        {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                month = 0;
        }
        return days[month];
    }
    // 日期+=天数
    Date& operator+=(int n)
    {
        int days = GetDayByYM(m_year,m_month);
        int day = n + m_day;
        while (day > days)
        {
            m_month++;
            if (m_month > 12)
            {
                m_year++;
                m_month = 1;
            }
            n -= days;
            days = GetDayByYM(m_year,m_month);
            day = n + m_day;
        }
        m_day += n;
        return *this;
    }
    // 日期+天数
    Date operator+(int n)
    {
        Date tmp = *this;
        int days = GetDayByYM(tmp.m_year, tmp.m_month);
        int day = n + tmp.m_day;
        while (day > days)
        {
            tmp.m_month++;
            if (tmp.m_month > 12)
            {
                tmp.m_year++;
                tmp.m_month = 1;
            }
            n -= days;
            days = GetDayByYM(tmp.m_year, tmp.m_month);
            day = n + tmp.m_day;
        }
        tmp.m_day += n;
        return tmp;
    }
    // 日期-天数
    Date operator-(int day)
    {
        
        Date tmp = *this;
        if (day < 0)
            return tmp + abs(day);
        tmp.m_day -= day;
        while (tmp.m_day <= 0)
        {

            if (tmp.m_month == 1)
            {
                tmp.m_year--;
                tmp.m_month = 12;
            }
            else
            {
               tmp.m_month--;
            }
            tmp.m_day += GetDayByYM(tmp.m_year, tmp.m_month);

        }
        return tmp;
    }
    // 日期-=天数
    Date& operator-=(int day)
    {
        m_day -= day;
        while (m_day <= 0)
        {

            if (m_month == 1)
            {
                m_year--;
                m_month = 12;
            }
            else
            {
                m_month--;
            }
            m_day += GetDayByYM(m_year, m_month);

        }
        return *this;
    }
    // 前置++
    Date& operator++()
    {
        *this = *this + 1;
        return *this;
    }
    // 后置++
    Date operator++(int)
    {
        Date tmp = *this;
        ++*this;
        return tmp;
    }
    // 后置--
    Date operator--(int)
    {
        Date tmp = *this;
        --* this;
        return tmp;
     }
    // 前置--
    Date& operator--()
    {
        *this = *this - 1;
        return *this;
    }
    // >运算符重载
    bool operator>(const Date& d)
    {
        return (m_year > d.m_year || m_year == d.m_year && m_month > d.m_month || m_year == d.m_year && m_month == d.m_month && m_day > d.m_day);
    }
    // ==运算符重载
    bool operator==(const Date& d)
    {
       return m_year == d.m_year && m_month == d.m_month && m_day == d.m_day;
    }
    // >=运算符重载
    inline bool operator <= (const Date& d)
    {
        return !(*this < d);
    }
    // <运算符重载
    bool operator < (const Date& d)
    {
        return (m_year < d.m_year || m_year == d.m_year && m_month < d.m_month || m_year == d.m_year && m_month == d.m_month && m_day < d.m_day);
    }
    // <=运算符重载
    bool operator >= (const Date& d)
    {
        return !(*this < d);
    }
    // !=运算符重载
    bool operator != (const Date& d)
    {
        return !(*this == d);
    }
    // 日期-日期 返回天数

  int operator-(const Date& d)
    {
        int flag = 1;
        Date Max=*this;
        Date Min=d;
        if ((*this) < d)
        {
            std::swap(Max, Min);
            flag = -1;
        }
        int days = 0;
        while (Max != Min)
        {
            --Max;
            ++days;
        }
        return days * flag;
    }
private:

    int m_year;
    int m_month;
    int m_day;

};
ostream& operator<<(ostream& out, const Date &d)
{
    cout << d.m_year << "年" << d.m_month << "月" << d.m_day << "日" << endl;
    return out;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值