关于日期类

本文介绍了一个用C++实现的简单日期计算器。通过定义一个日期类Date,实现了日期的加减运算、比较运算以及获取两个日期间相差的天数等功能。该类能够处理日期合法性检查、平年与闰年的判断等问题。

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

**

  • 是一个数据类型,它的变量就是一个对象,除了具有成员函数,还能容纳数据值。所以,在一个C++程序中,类的定义应该是一个数据类型定义,它描述变量能容纳哪些种类的值,以及成员函数有哪些。而下面定义的Data类型是一个类定义,这个类的容纳对象是日期值。

  • 在生活中我们想知道某天之后或某天之前是哪一天?或者距离这一天距离那天还有多少天?
    下面代码就是一个简单的模拟日历的计算器

**
代码如下:

#include <iostream>  
using namespace std;

//日期类  
class Date
{
public:
    Date(int year = 2016, int month = 10, int day = 29)
        : _year(year)
        , _month(month)
        , _day(day)
    {
        if (year < 0|| (month<0 || month>12) || (day<0 || day> Getmonthday(_year, _month)))
        {
            exit(1); //日期不合法,退出
        }
    } 

    Date& operator=(const Date& d)
    {
        if (*this != d)
        {
            _year = d._year;    
            _month = d._month;
            _day = d._day;
        }
        return *this;
    } 
    Date(const Date& d) 
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    ~Date()
    {} 
    int Getmonthday(int year, int month)
    {
        int arr[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))//考虑平年闰年二月份天数不同
        {
            arr[1] += 1;
            return arr[month];
        }
        return arr[month];
    }
    //前置++
    Date& operator++()
    {
        _day += 1;
        if (_day > Getmonthday(_year, _month))
        {
            _month += 1;
            if (_month > 12)
            {
                _year += 1;
                _month = 1;
            }
        }
        return *this;
    }
    // 后置++,返回旧值,先赋值,后自增   
    Date operator++(int)
    {
        return *this;
    }
    //前置--  
    Date& operator--()
    {
        _day -= 1;
        if (_day == 0)
        {
            _month -= 1;
            if (_month == 0)
            {
                _year -= 1;
                _month = 12;
            }
            _day = Getmonthday(_year, _month);//天数也需要变化;  
        }
        return *this;
    }
    //后置--  
    Date operator--(int)
    {
        return *this;
    }
    //day天之后的日期
    Date operator+(int day)
    {
        if (day<0)
        {
            return *this - (-day);
        }
        _day += day;
        while (_day > Getmonthday(_year, _month))
        {
            _day = _day - Getmonthday(_year, _month);
            _month += 1;
            if (_month > 12)
            {
                _year += 1;
                _month = 1;
            }
        }
        return *this;
    }
    // days天之前的日期  
    Date operator-(int day)
    {
        if (day < 0)
        {
            return *this + (-day);//如果减去天数小于0,则相当于向后+多少天
        }
        _day -= day;
        while (_day <= 0)
        {
            _month -= 1;
            if (_month == 0)
            {
                _month = 12;
                _year -= 1;
            }
            int k = Getmonthday(_year, _month);
            _day = k - (-_day);
        }
        return *this;
    }
    // 两个日期之间的距离 
    int operator-(const Date& d)
    {
        int count = 0;
        Date min = *this;
        Date max = d;
        if (min > max)
        {
            Date temp = min;
            min = max;
            max = temp;
        }
        while (min != max)
        {
            max.operator--();  //大的日期自减,减一天count加一天,两个日期正好相等时,返回count
            count++;
        }
        return count;
    }
    //逻辑运算符,与或非,&& || !
    bool operator==(const Date& d)
    {
        return (_year == d._year && _month == d._month && _day == d._day);
    }
    bool operator!=(const Date& d)
    {
        return (_year != d._year || _month != d._month || _day != d._day);
    }
    bool operator>(const Date& d)
    {
        if (_year > d._year)
        {
            return true;
        }
        else
        {
            if (_month > d._month)
            {
                return true;
            }
            else
            {
                if (_day > d._day)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    bool operator<(const Date& d)
    {
        if (_year < d._year)
        {
            return true;
        }
        else
        {
            if (_month < d._month)
            {
                return true;
            }
            else
            {
                if (_day < d._day)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    void Display()
    {
        cout << _year << " " << _month << " " << _day << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};

void FunTest()
{
    Date d1(2017, 10, 29);
    d1.Display();
    Date d2(2017, 10, 20);
    int ret = d2 - d1;
    cout << ret << endl;
}

int main()
{
    FunTest();
    getchar();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值