C++日期类实现

#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 > 0 && 
            (month > 0 && month < 13) &&
            (day>0 && day < Getdaysofmonth(year, month)))) {

            _year = 1900;
            _month = 1;
            _day = 1;
        }   
    }
    //拷贝构造函数
    Date(const Date& d) {
        this->_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+(int days) {
        Date tmp = *this;
        tmp._day = tmp._day + days;
        while ((tmp._day > Getdaysofmonth(tmp._year, tmp._month))) {
                tmp._day = tmp._day - Getdaysofmonth(tmp._year, tmp._month);
                tmp._month =tmp._month + 1;
                if (tmp._month > 12) {
                    tmp._month = 1;
                    tmp._year++;
                }
            }    
        return tmp;
    }
    //日期-天数
    Date operator-(int days) {
        Date tmp = *this;
        tmp._day = tmp._day - days;
        while (tmp._day <= 0) {
            tmp._month = tmp._month - 1;
            if (tmp._month < 1) {
                tmp._month = 12;
                tmp._year = tmp._year - 1;
            }
            tmp._day = tmp._day + Getdaysofmonth(tmp._year, tmp._month);
        }
        return tmp;
    }
    //前置++
    Date& operator++() {
        *this = *this + 1;
        return *this;
    }
    //后置++
    Date operator++(int n) {
        Date tmp(*this);
        *this = *this + 1;
        return tmp;
    }
    //前置--
    Date& operator--() {
        *this = *this - 1;
        return *this;
    }
    //后置--
    Date operator--(int n) {
        Date tmp(*this);
        *this = *this - 1;
        return tmp;
    }
    //日期-日期
    int operator-(Date& d) {
        Date minDate( *this);
        Date maxDate(d);
        int count = 0;
        if (minDate > maxDate) {
            maxDate = *this;
            minDate = d;
        }
        while (minDate != maxDate) {
            minDate++ ;
            count++;
        }
        return count;
    }
    //>重载
    bool operator>(const Date& d)const {
        if (this->_year > d._year) {
            return true;
        }
        else if (this->_year == d._year) {
            if (this->_month > d._month) {
                return true;
            }
            else if (this->_month == d._month ) {
                if (this->_day > d._day) {
                    return true;
                }
            }
        }
        return false;
    }
    //==
    bool operator==(const Date& d)const {
        if (this->_year == d._year
            && this->_month == d._month
            && this->_day == d._day) {
            return true;
        }
        return false;
    }
    //!=
    bool operator!=(const Date& d)const {
        return !(*this == d);
    }
    //<
    bool operator<(const Date& d)const {
        if (!(*this > d) && (*this != d)) {
            return true;
        }
        return false;
    }
    
private:
    //判断是不是闰年
    bool Isleapyear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        }
        else {
            return false;
        }
    }
    //获取某年某个月的天数
    int Getdaysofmonth(int year, int month) {
        int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
        if (2 == month && Isleapyear(year)) {
            return days[2] + 1;
        }
        return days[month];
    }
    //<<重载 
    friend ostream& operator<<(ostream& _cout, const Date& d);
private:
        //成员变量
    int _year;
    int _month;
    int _day;
};
//cout是输出流创的一个对象
ostream& operator<<(ostream& _cout, const Date& d) {
    _cout << d._year << "-" << d._month << "-" << d._day;
    return _cout;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值