关于日期类

来源:互联网 发布:淘宝延长发货时间 编辑:程序博客网 时间:2024/06/06 02:44

**

  • 是一个数据类型,它的变量就是一个对象,除了具有成员函数,还能容纳数据值。所以,在一个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;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果ipad反应慢怎么办 手机垃圾多了怎么办 ipad2内存过低怎么办 苹果平板ipad内存不足怎么办 手机dns配置错误怎么办 蓝牙已停止运行怎么办 ipad看电视闪退怎么办 ipad为什么看电视会闪退怎么办 微淘直播延迟怎么办 手机淘宝进群领金币怎么办 做淘客冲销量停止淘客后怎么办 微信中零钱提现怎么办 淘宝买家不签收怎么办 小龙虾没人下单怎么办 淘宝直播不浮现怎么办 淘宝直播看不了怎么办 理财客户说没钱怎么办 投资不给钱了怎么办 工作中遇到挫折怎么办 手机qq出现异常怎么办 农行卡出现异常怎么办 淘宝长期不发货怎么办 快递一直不发货怎么办 申请退款被拒绝怎么办 淘宝的垃圾短信怎么办 如果淘宝不退款怎么办 客服遇到客户骂怎么办 商场保证金不退怎么办 淘宝被投诉侵权怎么办 电脑wifi链接不见了怎么办 无线设备坏了怎么办 电脑网页默认了怎么办 淘宝商家停止服务怎么办? 淘宝商家停止了怎么办 做淘宝不会美工怎么办 做客服打字慢怎么办 淘宝客服学不会怎么办 代购不给退货怎么办 淘宝不能发照片怎么办 兼职刷手被骗怎么办 淘宝店商品被降权怎么办