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

2024-09-01 03:04

本文主要是介绍【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 ;

关键代码

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;elsereturn 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;}

完整代码

#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;elsereturn false;}bool operator==(const Date& d) const {if (this->_year == d._year&& this->_month == d._month&& this->_day == d._day)return true;elsereturn 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;elsereturn 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)

方法

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

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

关键代码

 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;}

完整代码 

#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本身变化了,因此最后返回aif (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")

💗感谢阅读!💗


这篇关于【c++】日期类相关实践:计算日期到天数转换、日期差值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125765

相关文章

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http