c++实现日期计算器计算某天后是几月几日

极客启航
• 阅读 70
//某某天后是几月几日的程序
#include <iostream>
#include <assert.h>
using namespace std;


class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

    // 拷贝构造
    // Date d2(d1);
    Date(const Date& d)
    {
        cout << "Date(Date& d)" << endl;

        _year = d._year;
        _month = d._month;
        _day = d._day;

        /*d._year = _year;
        d._month = _month;
        d._day = _day;*/
    }

    int GetMonthDay(int year, int month)
    {
        assert(month > 0 && month < 13);

        int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0))
        {
            return 29;
        }
        else
        {
            return monthArray[month];
        }
    }

    // d1.GetAfterXDay(100);
    //Date GetAfterXDay(int x)
    //{
    ///*    Date tmp;
    //    tmp._year = _year;
    //    tmp._month = _month;
    //    tmp._day = _day;*/
    //    //Date tmp(*this);
    //    Date tmp = *this;

    //    tmp._day += x;
    //    while (tmp._day > GetMonthDay(tmp._year, tmp._month))
    //    {
    //        // 进位
    //        tmp._day -= GetMonthDay(tmp._year, tmp._month);
    //        ++tmp._month;
    //        if (tmp._month == 13)
    //        {
    //            tmp._year++;
    //            tmp._month = 1;
    //        }
    //    }

    //    return tmp;
    //}

    // +
    Date Add(int x)
    {
        Date tmp = *this;
        tmp._day += x;
        while (tmp._day > GetMonthDay(tmp._year, tmp._month))
        {
            // 进位
            tmp._day -= GetMonthDay(tmp._year, tmp._month);
            ++tmp._month;
            if (tmp._month == 13)
            {
                tmp._year++;
                tmp._month = 1;
            }
        }

        return tmp;
    }

    // +=
    Date& AddEqual(int x)
    {
        _day += x;
        while (_day > GetMonthDay(_year, _month))
        {
            // 进位
            _day -= GetMonthDay(_year, _month);
            ++_month;
            if (_month == 13)
            {
                _year++;
                _month = 1;
            }
        }

        return *this;
    }

    void Print()
    {
        //cout << _year << "/" << _month << "/" << _day << endl;
        cout << _year << "年" << _month << "月" << _day << "日" << endl;
    }

private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1(2023, 2, 3);
    Date d2 = d1.Add(100);
    Date d3 = d1.Add(150);

    d1.Print();
    d2.Print();
    d3.Print();

    d1.AddEqual(200);
    d1.Print();

    // 实现一个函数,获取多少以后的一个日期

    return 0;
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
3年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
Wesley13 Wesley13
3年前
java常用类(2)
三、时间处理相关类Date类:计算机世界把1970年1月1号定为基准时间,每个度量单位是毫秒(1秒的千分之一),用long类型的变量表示时间。Date分配Date对象并初始化对象,以表示自从标准基准时间(称为“历元”(epoch),即1970年1月1日08:00:00GMT)以来的指定毫秒数。示例:packagecn.tanjian
皕杰报表(关于日期时间时分秒显示不出来)
在使用皕杰报表设计器时,数据据里面是日期型,但当你web预览时候,发现有日期时间类型的数据时分秒显示不出来,只有年月日能显示出来,时分秒显示为0:00:00。1.可以使用tochar解决,数据集用selecttochar(flowdate,"yyyyMMddHH:mm:ss")fromtablename2.也可以把数据库日期类型date改成timestamp
Easter79 Easter79
3年前
TypeScript 的声明文件的使用与编写
作者:Angus.Fenying<i.am.x.fenying@gmail.com(https://www.oschina.net/action/GoToLink?urlmailto%3Ai.am.x.fenying%40gmail.com)\日期:2016091909:53PM1\.什么是声明文件?
Stella981 Stella981
3年前
OpenCV手部关键点检测(手势识别)代码示例
点击我爱计算机视觉标星,更快获取CVML新技术前几日分享了learnopencv.com博主SatyaMallick发表的关于OpenCVMaskRCNN实例分割的博文(详见:OpenCV4.0MaskRCNN实例分割示例C/Python实现(https://www.oschina.net/action/GoT
Wesley13 Wesley13
3年前
HTTP面试题(二):HTTP请求报文和响应报文格式
!(https://oscimg.oschina.net/oscnet/0406894fb1274bee91fc53c84c516576.jpg)看都看了还不点个赞!(https://oscimg.oschina.net/oscnet/095d444dc9a449ee85afd19b00fdf52b.png)!(h
Stella981 Stella981
3年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Wesley13 Wesley13
3年前
Java日期时间API系列30
  实际使用中,经常需要使用不同精确度的Date,比如保留到天2020042300:00:00,保留到小时,保留到分钟,保留到秒等,常见的方法是通过格式化到指定精确度(比如:yyyyMMdd),然后再解析为Date。Java8中可以用更多的方法来实现这个需求,下面使用三种方法:使用Format方法、 使用Of方法和使用With方法,性能对比,使用
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
美凌格栋栋酱 美凌格栋栋酱
4个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
极客启航
极客启航
Lv1
送子军中饮,家书醉里题。
文章
4
粉丝
0
获赞
0