【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

相关文章

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Python 中的 with open文件操作的最佳实践

《Python中的withopen文件操作的最佳实践》在Python中,withopen()提供了一个简洁而安全的方式来处理文件操作,它不仅能确保文件在操作完成后自动关闭,还能处理文件操作中的异... 目录什么是 with open()?为什么使用 with open()?使用 with open() 进行

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim