题解 | 日期差值

日期差值

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

#include <ratio>
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<stdbool.h>
#include<assert.h>
//#include<stdio.h>
using namespace std;
class Date {
  public:
    friend ostream& operator<<(ostream& out, const Date& d);
    friend istream& operator>>(istream& in, Date& d);
    void Print()const {
        cout << _year << "/" << _month << "/" << _day << endl;
    }
    bool CheckDate();
    // 获取某年某月的天数

    int GetMonthDay(int year, int month) {
        assert(month > 0 && month < 13);
        static int array[13] = { -1, 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;
        }
        return array[month];

    }
    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1) {

        _year = year;
        _month = month;
        _day = day;
        if (!CheckDate()) {
            cout << "日期非法" << endl;
        }
        //cout << "11111" << endl;
    }

    // 拷贝构造函数

    //Date d2(d1);

    Date(const Date& d) {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }


    // 赋值运算符重载
    // d2 = d3 -> d2.operator=(&d2, d3)
    Date& operator=(const Date& d);


    // 析构函数
    //~Date();
    // 日期+=天数
    Date& operator+=(int day);
    // 日期+天数
    Date operator+(int day)const;
    // 日期-天数
    Date operator-(int day)const;
    // 日期-=天数
    Date& operator-=(int day);



    // 前置++

    Date& operator++();



    // 后置++

    Date operator++(int);



    // 后置--

    Date operator--(int);



    // 前置--

    Date& operator--();



    // >运算符重载

    bool operator>(const Date& d)const;



    // ==运算符重载

    bool operator==(const Date& d)const;



    // >=运算符重载

    bool operator >= (const Date& d)const;



    // <运算符重载

    bool operator < (const Date& d)const;



    // <=运算符重载

    bool operator <= (const Date& d)const;



    // !=运算符重载

    bool operator != (const Date& d)const;



    // 日期-日期 返回天数

    int operator-(const Date& d)const;

  private:

    int _year;

    int _month;

    int _day;

};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
Date parseDate(long long dateStr) {
    int year = dateStr / 10000;
    int month = (dateStr % 10000) / 100;
    int day = dateStr % 100;
    return Date(year, month, day);
}
// 日期+=天数
Date& Date::operator+=(int day) {
    if (day < 0) {
        return *this -= (-day);
    }
    _day += day;
    while (_day > GetMonthDay(_year, _month)) {
        _day -= GetMonthDay(_year, _month);
        _month++;
        if (_month > 12) {
            _year++;
            _month = 1;
        }
    }
    return *this;
}
// 日期+天数
Date Date::operator+(int day)const {
    Date tmp = *this;
    tmp += day;

    return tmp;
}

Date& Date:: operator=(const Date& d) {
    _year = d._year;
    _month = d._month;
    _day = d._day;

    return *this;
}
// 日期-天数

Date Date::operator-(int day)const {
    Date tmp = *this;
    tmp -= day;

    return tmp;
}

// 日期-=天数

Date& Date::operator -= (int day) {

    if (day < 0) {
        return *this += (-day);
    }
    _day -= day;
    while (_day <= 0) {
        --_month;
        if (_month == 0) {
            --_year;
            _month = 12;
        }
        _day += GetMonthDay(_year, _month);
    }
    return *this;
}

// >运算符重载

bool Date::operator>(const Date& d)const {
    return !(*this <= d);
}

// ==运算符重载

bool Date::operator==(const Date& d)const {
    return _year == d._year
           && _month == d._month
           && _day == d._day;
}
// >=运算符重载

bool Date::operator >= (const Date& d)const {
    return !(*this < d);
}
// <运算符重载

bool Date::operator < (const Date& d)const {
    if (_year < d._year) {
        return true;
    } else if (_year == d._year) {
        if (_month < d._month) {
            return true;
        } else if (_month == d._month) {
            if (_day < d._day) {
                return true;
            }
        }
    }
    return false;
}
// <=运算符重载

bool Date::operator <= (const Date& d)const {
    return *this < d || *this == d;
}

// !=运算符重载

bool Date::operator != (const Date& d)const {

    return !(*this == d);
}

// 前置++

Date& Date::operator++() {
    *this += 1;

    return *this;
}



// 后置++

Date Date::operator++(int) {
    Date tmp = *this;
    *this += 1;
    return tmp;

}



// 后置--

Date Date::operator--(int) {
    Date tmp = *this;
    *this -= 1;
    return tmp;
}



// 前置--

Date& Date::operator--() {

    *this -= 1;
    return *this;
}

// 日期-日期 返回天数

int Date::operator-(const Date& d)const {
    Date max = *this;
    Date min = d;
    int flag = 1;
    if (*this < d) {
        max = d;
        min = *this;
        flag = -1;
    }
    int n = 0;
    while (min < max) {
        n++;
        min++;
    }
    return n * flag;
}

//防止玩家恶搞
bool Date::CheckDate() {
    if (_month < 1 || _month > 12
            || _day < 1 || _day > GetMonthDay(_year, _month)) {
        return false;
    } else {
        return true;
    }
}



ostream& operator<<(ostream& out, const Date& d) {
    out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    return out;
}
istream& operator>>(istream& in, Date& d) {
    while (1) {
        cout << "请依次输入年月日:>";
        in >> d._year >> d._month >> d._day;
        if (!d.CheckDate()) {
            cout << "日期非法" << endl;
            d.Print();
            cout << "请重新输入" << endl;
        } else {
            break;
        }
    }
    return in;
}
int main() {

    long long day1, day2;
    while (cin >> day1 >> day2) {
        Date d1 = parseDate(day1);
        Date d2 = parseDate(day2);
        int day3=abs(d1-d2)+1;
        printf("%d\n",day3);

        
    }

    return 0;
}

全部评论

相关推荐

老粉都知道小猪猪我很久没更新了,因为秋招非常非常不顺利,emo了三个月了,接下来说一下我的情况吧本人是双非本&nbsp;专业是完全不着计算机边的非科班,比较有优势的是有两段大厂实习,美团和字节。秋招面了50+场泡池子泡死的:滴滴&nbsp;快手&nbsp;去哪儿&nbsp;小鹏汽车&nbsp;不知名的一两个小厂其中字节13场&nbsp;两次3面挂&nbsp;两次2面挂&nbsp;一次一面挂其中有2场面试题没写出来,其他的都是全a,但该挂还是挂,第三次三面才面进去字节,秋招加暑期总共面了22次字节,在字节的面评可以出成书了快手面了8场,2次实习的,通过了但没去,一次2面挂&nbsp;最后一次到录用评估&nbsp;至今无消息滴滴三面完&nbsp;没几天挂了&nbsp;所有技术面找不出2个问题是我回答不上来的,三面还来说我去过字节,应该不会考虑滴滴吧,直接给我干傻了去哪儿一天速通&nbsp;至今无消息小鹏汽车hr&nbsp;至今无消息美团2面挂&nbsp;然后不捞我了,三个志愿全部结束,估计被卡学历了虾皮二面挂&nbsp;这个是我菜,面试官太牛逼了拼多多二面挂&nbsp;3道题也全写了&nbsp;也没问题是回答不出来的&nbsp;泡一周后挂腾讯面了5次&nbsp;一次2面挂&nbsp;三次一面挂,我宣布腾讯是世界上最难进的互联网公司然后还有一些零零散散的中小厂,但是数量比较少,约面大多数都是大厂。整体的战况非常惨烈,面试机会少,就算面过了也需要和各路神仙横向对比,很多次我都是那个被比下去的人,不过这也正常,毕竟谁会放着一个985的硕士不招,反而去招一个双非读化学的小子感觉现在互联网对学历的要求越来越高了,不仅仅要985还要硕士了,双非几乎没啥生存空间了,我感觉未来几年双非想要进大厂开发的难度应该直线上升了,唯一的打法还是从大二刷实习,然后苟个转正,不然要是去秋招大概率是炮灰。而且就我面字节这么多次,已经开始问很多ai的东西了,你一破本科生要是没实习没科研懂什么ai啊,纯纯白给了
不知名牛友_:爸爸
秋招你被哪家公司挂了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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