实现日期类

本文介绍了一个日期类的设计与实现,包括日期的加减运算、比较运算等,并演示了如何使用该类进行日期间的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include<Windows.h>
using namespace std;


class Date
{
    friend ostream& operator<<(ostream& _cout, const Date& d);
public:
    Date(int year = 1900, int month = 1, int day = 1)
        : _year(year)
        , _month(month)
        , _day(day)
    {
        if ((_year<0)||
            ((_month<1) || (_month>12)) ||
            ((_day<0) || (_day>GetMonthdays(month))))
        {
            _year = 1990;
            _month = 1;
            _day = 1;
        }
    }

    Date operator+(int days)
    {
        if (days < 0)
        {
            return *this - (0 - days);
        }
        Date temp(*this);
        temp._day += days;
        int daysinmonth;
        while (temp._day > (daysinmonth =temp.GetMonthdays(temp._month)))
        {
            temp._day -= daysinmonth;
            temp._month++;
            if (temp._month > 12)
            {
                temp._year++;
                temp._month = 1;
            }
        }
        return temp;
    }
    Date operator-(int days)
    {
        if (days < 0)
        {
            return *this + (0 - days);
        }
        Date temp(*this);
        temp._day -= days;
        int daysinmonth = temp.GetMonthdays(temp._month-1);
        while (temp._day <1)
        {
            temp._month--;
            if (temp._month <1)
            {
                temp._year--;
                temp._month = 12;
            }
            temp._day += (daysinmonth=temp.GetMonthdays(temp._month ));
        }
        return temp;
    }
    Date& operator++()
    {
        *this = *this + 1;
        return *this;

    }
    Date operator++(int)
    {
        Date temp = *this;
        ++(*this);
        return temp;
    }
    Date& operator--()
    {
        *this = *this - 1;
        return *this;
    }
    Date operator--(int)
    {
        Date temp = *this;
        --(*this);
        return temp;
    }
    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)
    {
        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)
    {
        if (((_year == d._year) && (_month == d._month) && (_day == d._day)))
        {
            return true;
        }
        return false;
    }
    bool operator!=(const Date& d)
    {
        if (((_year != d._year) ||(_month != d._month) || (_day != d._day)))
        {
            return true;
        }
        return false;
    }
    int operator-(const Date& d)
    {
        int sumDay = 0;
        if (*this == d)
        {
            return 0;
        }
        else if (*this>d)
        {
            Date temp = d;
            while (temp != *this)
            {
                temp++;
                sumDay++;
            }
            return sumDay;
        }
        else
        {
            Date temp = *this;
            while (temp != d)
            {
                temp++;
                sumDay++;
            }
            return sumDay;
        }
    }
private:
    int _year;
    int _month;
    int _day;
    int GetMonthdays(int month)
    {
        int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (Isleapyear()&&2 == month    )
        {
            days[2] += 1;
        }
        return days[month];
    }
    bool Isleapyear()const
    {
        if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))
        {
            return true;
        }
        return false;
    }
};

ostream& operator<<(ostream& _cout, const Date& d)
{
    _cout << d._year << "-" << d._month<<"-"<<d._day;
    return _cout;
}


int main()
{
    Date d1(2017, 2, 28);
    Date d2(2016, 2, 28);
    int sum = d2- d1;
    cout << d1;
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值