【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

相关文章

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方