【c++】日期类相关实践:计算日期到天数转换、日期差值

相关文章:日期类(运算符重载应用)详细版


目录

前言

实践1:计算日期到天数转换

题目

方法

关键代码

完整代码

实践2:日期差值

题目

方法

关键代码

完整代码

💗感谢阅读!💗


前言

上篇文章中(【c++】类与对象实践:日期类(运算符重载应用)详细版),我们对日期类进行一个完整的实现!

那么接下来我们可以学以致用,完成下列题目!

计算日期到天数转换_牛客题霸_牛客网

日期差值_牛客题霸_牛客网 (nowcoder.com)

实践1:计算日期到天数转换

题目

​​​​​计算日期到天数转换_牛客题霸_牛客网 (nowcoder.com)

方法

获取每月天数 int GetMonthDays(int year, int month) ;

判断闰年:

不能被100整除能被4整除 或 能被400整除。

平年二月28天;

闰年29天。

1 3 5 7 8 10 12 月有31天

4 6 9 11 月有30天
日期类中实现的前置++重载,Date& operator++() ;

日期类中实现的工具方法,日期差值计算 int operator-(const Date& d) const ;

关键代码

cpp 复制代码
int GetMonthDays(int year, int month) {
        static int monthDays[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 monthDays[month];
    }



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



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


    int TheDayOfYear() {
        return (*this - Date(_year, 1, 1)) + 1;
    }

完整代码

cpp 复制代码
#include <iostream>
using namespace std;
class Date {
  public:
    Date(int year = 0, int month = 0, int day = 0) {
        _year = year;
        _month = month;
        _day = day;
    }
    bool operator>(const Date& d) const {
        if (this->_year > d._year)
            return true;
        else if (this->_year == d._year && this->_month > d._month)
            return true;
        else if (this->_year == d._year && this->_month == d._month &&
                 this->_day > d._day)
            return true;
        else
            return false;
    }
    bool operator==(const Date& d) const {
        if (this->_year == d._year
                && this->_month == d._month
                && this->_day == d._day)
            return true;
        else
            return false;
    }
    bool operator!=(const Date& d) const {
        return !(*this == d);
    }
    bool operator<(const Date& d) const {
        return !(*this > d || *this == d);
    }
    Date& operator-=(int day) {
        if (day < 0) {
            *this += -day;
            return *this;
        }
        _day -= day;
        while (_day <= 0) {
            _month--;
            if (_month == 0) {
                _month = 12;
                _year--;
            }
            _day += GetMonthDays(_year, _month);
        }
        return *this;
    }
    Date& operator+=(int day) {
        if (day < 0) {
            *this -= -day;
            return *this;
        }
        _day += day;
        while (_day > GetMonthDays(_year, _month)) {
            _day -= GetMonthDays(_year, _month);
            _month++;
            if (_month == 13) {
                _month = 1;
                _year++;
            }
        }
        return *this;
    }
    int GetMonthDays(int year, int month) {
        static int monthDays[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 monthDays[month];
    }
    Date& operator++() {
        *this += 1;
        return *this;
    }
    int operator-(const Date& d) const {
        Date max = *this;
        Date min = d;
        int flag = 1;
        int cnt = 0;
        if (*this < d) {
            max = d;
            min = *this;
            flag = -1;
        }
        while (min != max) {
            ++cnt;
            ++min;
        }
        return cnt * flag;
    }
    int TheDayOfYear() {
        return (*this - Date(_year, 1, 1)) + 1;
    }
  private:
    int _year;
    int _month;
    int _day;
};

int main() {
    int year, month, day;
    while (cin >> year >> month >> day) {
        Date d(year, month, day);
        cout << d.TheDayOfYear() << endl;
    }
    return 0;
}

实践2:日期差值

题目

日期差值_牛客题霸_牛客网 (nowcoder.com)

方法

轻而易举,在日期类中,我们已经实现了对于计算日期差值的工具方法。

对于这道题目,我们可以直接套用!!

关键代码

cpp 复制代码
 void getDaynums(const Date& d) {
        Date max = d;
        Date min = *this;

        if (!(max > min)) {
            max = *this;
            min = d;
        }

        int day = 0;
        while (min != max) {
            min++;
            day++;
        }


        day++;

        cout << day << endl;
    }

完整代码

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;


class Date {
  private:
    int  _year;
    int _day;
    int _month = 0;

    int getMonthDay(int year, int month) {
        int monthArr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        //闰年 二月不同
        if (month == 2 && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))) {
            return 29;
        }

        return monthArr[month];
    }
  public:
    Date(int year, int month, int day) : _year(year), _month(month), _day(day) {  }

    bool operator>(const Date& d) {
        if (_year > d._year) {
            return true;
        } else if (_year == d._year && _month > d._month) {
            return true;
        } else if (_year == d._year && _month == d._month && _day > d._day) {
            return true;
        }

        return false;
    }

    bool operator==(const Date& d) {
        if (_year == d._year && _month == d._month && _day == d._day) {
            return true;
        }
        return false;
    }
    Date& operator-=(int day) {
        if (day < 0) {
            return *this += (-day);
        }

        _day -= day;
        while (_day < 0) {
            _month--;
            if (_month == 0) {
                _month = 12;
                _year--;
            }
            _day += getMonthDay(_year, _month);

        }

        return *this;
    }
    bool operator != (const Date& d) {
        if (*this == d)
            return false;
        return true;
    }
    Date& operator+=(int day) {
        // a+=b,a本身变化了,因此最后返回a


        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 operator++(int) {
        Date temp(*this);
        *this += 1;
        return temp;
    }

    void getDaynums(const Date& d) {
        Date max = d;
        Date min = *this;

        if (!(max > min)) {
            max = *this;
            min = d;
        }

        int day = 0;
        while (min != max) {
            min++;
            day++;
        }


        day++;

        cout << day << endl;
    }

};


int main() {
    int day1, day2, mon1, mon2, year1, year2;
    scanf("%4d%2d%2d", &year1, &mon1, &day1);
    scanf("%4d%2d%2d", &year2, &mon2, &day2);
    Date d(year1, mon1, day1);
    Date d2(year2, mon2, day2);
    d.getDaynums(d2);
}
// 64 位输出请用 printf("%lld")

💗感谢阅读!💗


相关推荐
John_ToDebug3 分钟前
JS 与 C++ 双向通信实战:基于 WebHostViewListener 的消息处理机制
前端·c++·chrome
亮亮爱刷题4 分钟前
算法提升之树上问题-(LCA)
数据结构·算法·leetcode·深度优先
papership11 分钟前
【入门级-C++程序设计:11、指针与引用-引 用】
c语言·开发语言·c++·青少年编程
火车叨位去194914 分钟前
力扣top100(day03-01)--二叉树 03
算法·leetcode·职场和发展
岁忧14 分钟前
(LeetCode 每日一题) 1780. 判断一个数字是否可以表示成三的幂的和 (数学、三进制数)
java·c++·算法·leetcode·职场和发展·go
常乐か1 小时前
VS2022+QT5.15.2+OCCT7.9.1的开发环境搭建流程
开发语言·qt·opencascade
浩少7021 小时前
LeetCode-16day:栈
java·数据结构·算法
Evand J2 小时前
【MATLAB例程】滑动窗口均值滤波、中值滤波、最小值/最大值滤波对比。附代码下载链接
开发语言·matlab·均值算法
IT毕设实战小研2 小时前
基于SpringBoot的救援物资管理系统 受灾应急物资管理系统 物资管理小程序
java·开发语言·vue.js·spring boot·小程序·毕业设计·课程设计