题解 | #日期累加#

日期累加

https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d

#include <fstream>
#include <iostream>
#include <ostream>
using namespace std;

int GetMonthDay(int y, int m)
{
    int month[] = { 0, 31, 28, 31, 30, 31, 30, 31,31, 30, 31, 30, 31 };
    if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
        return 29;
    return month[m];
}

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

    friend Date GetDate(int n);
    friend ostream & operator<<(ostream & _cout,const Date& d);

    Date& operator++()
    {
        _day++;
        if (_day > GetMonthDay(_year, _month))
        {
            _month++;
            _day = 1;
            if (_month == 13)
            {
                _month = 1;
                _year++;
            }
        }
        return *this;
    }

    Date operator+(int n)
    {
        Date tmp =*this;
        while(n--)
        {
            ++tmp;
        }
       return tmp;
    }

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


ostream & operator<<(ostream & _cout,const Date &d1)
{
    if(d1._month<10&&d1._day<10)
    {
        _cout << d1._year << "-" << 0 <<d1._month << "-" << 0<<d1._day ; 
    }
    else if(d1._month<10&&d1._day>=10){
        _cout << d1._year << "-" << 0 <<d1._month << "-" << d1._day ; 
    }
    else if(d1._month>=10&&d1._day<10)
    {
        _cout << d1._year << "-" <<d1._month << "-" << 0<<d1._day ; 
    }
    else if(d1._month>=10&&d1._day>=10)
    {
        _cout << d1._year << "-" <<d1._month << "-" <<d1._day ; 
    }
    return _cout;
}


int main() {
    int n1, n2;
    int m;
    cin>>m;
    while(m--)
    {
        int n;
        int year,month,day;
        cin>>year>>month>>day>>n;
        Date d(year,month,day);
        cout<<(d+n)<<endl;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-30 18:19
点赞 评论 收藏
分享
小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务