当前位置: 首页 > news >正文

【C++】日期类实现,与日期计算相关OJ题

文章目录

  • 日期类的设计
  • 日期计算相关OJ题
    • HJ73 计算日期到天数转换
    • KY111 日期差值
    • KY222 打印日期
    • KY258 日期累加

在软件开发中,处理日期是一项常见的任务。为了方便地操作日期,我们可以使用C++编程语言来创建一个简单的日期类。在本文中,我们将介绍如何使用C++实现一个基本的日期类,包括日期的加减、大小比较等功能。

日期类的设计

下面是日期类的基本实现代码:

#pragma once
#include<iostream>
using namespace std;class Date {
public:// 获取某年某月的天数int GetMonthDay(const int year, const int month);// 构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数Date(const Date& d);// 析构函数~Date();// 打印日期void print()const;// 赋值运算符重载Date& operator=(const Date& d);// +=运算符重载Date& operator+=(const int day);// +运算符重载Date operator+(const int day);// -=运算符重载Date& operator-=(int day);// -运算符重载Date operator-(int day);// 计算两个日期之间的天数差int operator-(const Date& d) const;// ++前置运算符重载Date& operator++();// ++后置运算符重载Date operator++(int);// --前置运算符重载Date& operator--();// --后置运算符重载Date operator--(int);// 大于运算符重载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;// 地址运算符重载const Date* operator & () const;// 输出流运算符重载friend ostream& operator << (ostream& out, const Date& d);// 输入流运算符重载friend istream& operator >> (istream& in, Date& d);private:int _year;  // 年int _month;  // 月int _day;  // 日
};// 获取某年某月的天数
int Date::GetMonthDay(const int year, const int month) {int monthDay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (2 == month && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {return 29;}return monthDay[month];
}// 构造函数
Date::Date(int year, int month, int day) {if ((year < 1) || (month < 1) || (month > 12) || (day < 1) || (day > GetMonthDay(year, month))) {cout << "非法日期:" << endl;}_year = year;_month = month;_day = day;
}// 拷贝构造函数
Date::Date(const Date& d) {_year = d._year;_month = d._month;_day = d._day;
}// 析构函数
Date::~Date() {_year = 1900;_month = 1;_day = 1;
}// 打印日期
void Date::print()const {cout << _year << "/" << _month << "/" << _day << endl;
}// 赋值运算符重载
Date& Date::operator=(const Date& d) {if (this != &d) {_day = d._day;_month = d._month;_year = d._year;}return *this;
}// +=运算符重载
Date& Date::operator+=(const int day) {_day += day;while (_day > GetMonthDay(_year, _month)) {_day -= GetMonthDay(_year, _month);++_month;if (_month == 13) {++_year;_month = 1;}}return *this;
}// +运算符重载
Date Date::operator+(const int day) {Date tmp(*this);tmp += day;return tmp;
}// -=运算符重载
Date& Date::operator-=(int day) {_day -= day;while (_day < 0) {--_month;if (_month == 0) {--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}// -运算符重载
Date Date::operator-(int day) {Date tmp(*this);tmp -= day;return tmp;
}// 计算两个日期之间的天数差
int Date::operator-(const Date& d) const {Date BigDate = *this;Date SmallDate = d;if (SmallDate > BigDate) {BigDate = d;SmallDate = *this;}int count = 0;while (SmallDate != BigDate) {++SmallDate;++count;}return count;
}// ++前置运算符重载
Date& Date::operator++() {*this += 1;return *this;
}// ++后置运算符重载
Date Date::operator++(int) {Date tmp(*this);*this += 1;return tmp;
}// --前置运算符重载
Date& Date::operator--() {*this -= 1;return *this;
}// --后置运算符重载
Date Date::operator--(int) {Date tmp(*this);*this -= 1;return tmp;
}// 大于运算符重载
bool Date::operator>(const Date& d) const {if (_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day)) {return true;}return false;
}// 等于运算符重载
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) || (*this == d);
}// 小于运算符重载
bool Date::operator < (const Date& d) const {return !(*this >= d);
}// 小于等于运算符重载
bool Date::operator <= (const Date& d) const {return !(*this > d);
}// 不等于运算符重载
bool Date::operator != (const Date& d) const {return !(*this == d);
}// 地址运算符重载
const Date* Date::operator & () const {return this;
}// 输出流运算符重载
ostream& operator << (ostream& out, const Date& d) {out << d._year << "/" << d._month << "/" << d._day;return out;
}// 输入流运算符重载
istream& operator >> (istream& in, Date& d) {in >> d._year;in >> d._month;in >> d._day;return in;
}

上面的代码实现了日期类的基本功能,包括构造函数、加减运算符重载、比较运算符重载、输入输出流运算符重载等。
下面是主函数用于测试代码功能:

int main() {// 创建日期对象并打印Date d1(2023, 11, 13);cout << "日期1:";d1.print();// 拷贝构造函数测试Date d2(d1);cout << "日期2(拷贝构造):";d2.print();// 赋值运算符重载测试Date d3 = d1;cout << "日期3(赋值运算符重载):";d3.print();// += 运算符重载测试d1 += 10;cout << "日期1(+=运算符重载后):";d1.print();// + 运算符重载测试Date d4 = d2 + 5;cout << "日期4(+运算符重载):";d4.print();// -= 运算符重载测试d2 -= 3;cout << "日期2(-=运算符重载后):";d2.print();// - 运算符重载测试Date d5 = d3 - 7;cout << "日期5(-运算符重载):";d5.print();// - 运算符重载测试int diff = d5 - d4;cout << "日期4和日期5之间的天数差:" << diff << endl;// ++ 前置运算符重载测试++d1;cout << "日期1(++前置运算符重载后):";d1.print();// ++ 后置运算符重载测试Date d6 = d2++;cout << "日期6(++后置运算符重载):";d6.print();cout << "日期2(++后置运算符重载后):";d2.print();// -- 前置运算符重载测试--d3;cout << "日期3(--前置运算符重载后):";d3.print();// -- 后置运算符重载测试Date d7 = d4--;cout << "日期7(--后置运算符重载):";d7.print();cout << "日期4(--后置运算符重载后):";d4.print();// 大于运算符重载测试cout << "日期5大于日期6吗?" << (d5 > d6 ? "是" : "否") << endl;// 等于运算符重载测试cout << "日期1等于日期2吗?" << (d1 == d2 ? "是" : "否") << endl;// 不等于运算符重载测试cout << "日期3不等于日期4吗?" << (d3 != d4 ? "是" : "否") << endl;// 输出流运算符重载测试cout << "日期1的输出流运算符重载:" << d1 << endl;// 输入流运算符重载测试Date d8;cout << "请输入一个日期(年 月 日):";cin >> d8;cout << "您输入的日期为:" << d8 << endl;return 0;
}

在这里插入图片描述
C++实现一个简单的日期类,包括日期的加减、大小比较等功能。日期类的实现可以帮助我们更方便地处理日期,提高代码的可读性和可维护性。

日期计算相关OJ题

HJ73 计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded?tpId=37&&tqId=21296&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking
在这里插入图片描述

#include <iostream>
using namespace std;int main() {int month[13] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};int y, m, d;cin >> y >> m >> d;int day = d;if (2 < m && ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))){day += 1;}day += month[m];cout << day << endl;
}

KY111 日期差值

https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c?tpId=62&&tqId=29468&rp=1&ru=/activity/oj&qru=/ta/sju-kaoyan/question-ranking
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;// 判断是否为闰年
bool isLeapYear(int year) 
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}// 获取某年某月的天数
int getDaysOfMonth(int year, int month) 
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}// 计算日期距离公元0年的天数
int getDaysFromZero(int year, int month, int day) 
{int days = 0;for (int y = 1; y < year; ++y) {days += isLeapYear(y) ? 366 : 365;}for (int m = 1; m < month; ++m) {days += getDaysOfMonth(year, m);}days += day;return days;
}// 计算两个日期之间的天数差值
int getDaysDiff(const string& date1, const string& date2) 
{int year1, month1, day1, year2, month2, day2;sscanf(date1.c_str(), "%4d%2d%2d", &year1, &month1, &day1);sscanf(date2.c_str(), "%4d%2d%2d", &year2, &month2, &day2);int days1 = getDaysFromZero(year1, month1, day1);int days2 = getDaysFromZero(year2, month2, day2);return abs(days2 - days1) + 1;
}int main() 
{string date1, date2;while (cin >> date1 >> date2) {int daysDiff = getDaysDiff(date1, date2);cout << daysDiff << endl;}return 0;
}

KY222 打印日期

https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId=69&&tqId=29669&rp=1&ru=/activity/oj&qru=/ta/hust-kaoyan/question-ranking
在这里插入图片描述

#include <iostream>
using namespace std;bool isLeapYear(int year)
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}int getMonthDay(int year, int month)
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}int main() {int year, day;while (cin >> year >> day) { int month = 1;while (day > getMonthDay(year, month)){day -= getMonthDay(year, month);++month;}printf("%4d-%02d-%02d\n",  year, month, day);}
}

KY258 日期累加

https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40&&tqId=31013&rp=1&ru=/activity/oj&qru=/ta/kaoyan/question-ranking
在这里插入图片描述

#include <iostream>
using namespace std;bool isLeapYear(int year)
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}int getMonthDay(int year, int month)
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}
int main() {int t, year, month, day, add;cin >> t;while (t--) { // 注意 while 处理多个 casecin >> year >> month >> day >> add;day += add;while (day > getMonthDay(year, month)){day -= getMonthDay(year, month);++month;if (13 == month){month = 1;++year;}}printf("%4d-%02d-%02d\n",  year, month, day);}
}

相关文章:

【C++】日期类实现,与日期计算相关OJ题

文章目录 日期类的设计日期计算相关OJ题HJ73 计算日期到天数转换KY111 日期差值KY222 打印日期KY258 日期累加 在软件开发中&#xff0c;处理日期是一项常见的任务。为了方便地操作日期&#xff0c;我们可以使用C编程语言来创建一个简单的日期类。在本文中&#xff0c;我们将介…...

前端404页面的制作

1、背景 前端开发经常遇到输入路径不存在的问题&#xff0c;为此&#xff0c;把之前项目的404拿出来供大家参考。代码很简单&#xff0c;适合新手入手&#xff0c;效果如下&#xff1a; 2、代码引用的是element-plus框架 <template><div><el-result icon"…...

深兰科技轮腿家用AI机器人荣获“2023年度城市更新科创大奖”

近日&#xff0c;“2023金砖论坛第五季金立方城市更新科创大会”在上海举行&#xff0c;会上发布了《第12届金砖价值榜》&#xff0c;深兰科技研发出品的轮腿式家用AI机器人(兰宝)&#xff0c;因其AI技术的创新性应用&#xff0c;荣获了“2023年度城市更新科创大奖”。 在10月2…...

669.修剪二叉树

原题链接:669.修剪二叉树 全代码&#xff1a; class Solution { public:TreeNode* trimBST(TreeNode* root, int low, int high) {if (root nullptr ) return nullptr;if (root->val < low) {TreeNode* right trimBST(root->right, low, high); // 寻找符合区间[l…...

论文绘图-机器学习100张模型图

在现代学术研究和技术展示中&#xff0c;高质量的图表和模型结构图是至关重要的。这尤其在机器学习领域更为显著&#xff0c;一个领域以其复杂的算法和复杂的数据结构而闻名。机器学习是一种使用统计技术使计算机系统能够从数据中学习和改进其任务执行的方法&#xff0c;而有效…...

PHP项目学习笔记-萤火商城-增加一个模块(表涉及到的操作和文件)

背景 是在store的后台添加一个页面&#xff0c;显示的如满意度调查的页面 在router.config.js里面配置一个新的菜单 路径&#xff1a;yoshop2.0-store\src\config\router.config.js 代码如下&#xff0c;很简单&#xff0c;定义了这菜单点击的时候进入的页面&#xff0c;和下面…...

如何用Java设计自动售货机?

如何用Java设计自动售货机?是大多在高级Java开发人员面试中经常被问到的好问题之一。在典型的编码面试中,你会得到一个问题描述来开发一个售货机,在有限的时间内,通常2到3小时内,你需要在Java中编写设计文档、工作代码和单元测试。这种Java面试的一个关键优势是可以一次测试候…...

JAVA数据代码示例

首先&#xff0c;我们需要导入一些必要的Java库 java import java.net.URL; import java.net.HttpURLConnection; import java.io.BufferedReader; import java.io.InputStreamReader; 然后&#xff0c;我们可以创建一个URL对象&#xff0c;表示我们要爬取的网页的URL。 jav…...

github常用搜索指令

一、常用搜索指令 以下指令可分开用&#xff0c;也可组合使用 根据关键字搜索 in:name xx继上一步&#xff1a;指定开发语言 language:Java in:name XX language:Java继上一步&#xff0c;指定更新日期 pushed:>2022-06-06 in:name XX language:Java pushed:>2022-0…...

为什么esp8266刷入了固件,无法接受AT指令

我遇到的解决方法是&#xff1a;是串口调试助手出了问题。所以需要更换一个串口调试助手软件。 上面这个就是我换了的软件 在开发的时候&#xff0c;经常会遇到软件故障&#xff0c;导致正确的方法&#xff0c;但是没有效果&#xff0c;好比以前用盗版的8.7版本的Proteus模拟…...

Scala---字符串、集合

一、字符串 StringStringBuilder 可变string操作方法举例 比较:equals比较忽略大小写:equalsIgnoreCaseindexOf&#xff1a;如果字符串中有传入的assci码对应的值&#xff0c;返回下标 1./** 2.* String && StringBuilder 3.*/ 4.val str "abcd" 5.val s…...

Power Automate-当收到HTTP请求时触发流程

选择创建自动化云端流&#xff0c;点跳过 第一个操作搜索HTTP&#xff0c;点击当收到HTTP请求时 点击使用示例有效负载生成架构 写入JSON&#xff0c;点击完成 正文JSON架构就自动生成了&#xff0c;再点击左下角的显示高级选项 Method根据需求选择 可以选择JSON中的参数赋值给…...

学习c#的第十四天

目录 C# 接口&#xff08;Interface&#xff09; 接口的特点 定义接口 接口继承 接口和抽象类的区别 C# 命名空间&#xff08;Namespace&#xff09; using 关键字 定义命名空间 嵌套命名空间 C# 接口&#xff08;Interface&#xff09; 接口定义了所有类继承接口时应…...

6.jvm中对象创建流程与内存分配

目录 概述对象的创建流程对象的内存分配方式对象怎样才会进入老年代大对象直接进入老年代内存担保 jvc 相关指令查看jdk默认使用的gc查看当前jdk支持的有哪些gc查看指定进程当前正在使用的gc 结束 概述 相关文章在此总结如下&#xff1a; 文章地址jvm基本知识地址jvm类加载系…...

算法--搜索与图

这里写目录标题 主要内容DFS思想 BFS思想 DFS与BFS的比较一级目录二级目录二级目录二级目录 一级目录二级目录二级目录二级目录 一级目录二级目录二级目录二级目录 主要内容 DFS 思想 会优先向深处搜索 一旦到达最深处 那么会回溯 但是在回溯的过程中 会边回溯边观察是否有能继…...

ROS 文件系统

ROS文件系统级指的是在硬盘上ROS源代码的组织形式&#xff0c;ROS 的文件系统本质上都还是操作系统文件&#xff0c;可以使用Linux命令来操作这些文件&#xff0c;文件操作&#xff0c;包含增删改查与执行等操作&#xff0c;ROS文件系统的一些常用命令如下&#xff1a; 1.增加…...

车载通信与DDS标准解读系列(1):DDS-RPC

▎RPC & DDS-RPC RPC&#xff1a;Remote Procedure Call&#xff0c;远程过程调用。 远程过程调用是一种进程间通信&#xff0c;它允许计算机程序在另一个地址空间中执行子程序&#xff0c;就好像用别人的东西像用自己的一样&#xff0c;常用于分布式系统。 远程过程调用…...

通过构造树形结构介绍map的用法

构造TreeSelect树形结构&#xff1a; 当我们拿到的数据与我们要用的数据不一致时&#xff0c;就要改造成自己想要的数据结构。 后端拿到的数据结构&#xff1a; public class TPMGroup{public string DepName { get; set; }public List<staff> TPMList { get; set; }pu…...

代码随想录算法训练营Day 53 || 1143.最长公共子序列、1035.不相交的线、53. 最大子序和

1143.最长公共子序列 力扣题目链接 给定两个字符串 text1 和 text2&#xff0c;返回这两个字符串的最长公共子序列的长度。 一个字符串的 子序列 是指这样一个新的字符串&#xff1a;它是由原字符串在不改变字符的相对顺序的情况下删除某些字符&#xff08;也可以不删除任何…...

Oracle JDBC数据库驱动程序介绍

Maven Central上所有Oracle JDBC数据库驱动程序 现在不仅可以在Maven Central上使用甲骨文数据库最新版本&#xff0c;而且还可以获得所有受支持的Oracle JDBC驱动程序发行版&#xff0c;包括19.3.0.0、18.3.0.0、12.2.0.1和11.2.0.4。从现在开始&#xff0c;Maven Central确实…...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

Unity3D中Gfx.WaitForPresent优化方案

前言 在Unity中&#xff0c;Gfx.WaitForPresent占用CPU过高通常表示主线程在等待GPU完成渲染&#xff08;即CPU被阻塞&#xff09;&#xff0c;这表明存在GPU瓶颈或垂直同步/帧率设置问题。以下是系统的优化方案&#xff1a; 对惹&#xff0c;这里有一个游戏开发交流小组&…...

线程同步:确保多线程程序的安全与高效!

全文目录&#xff1a; 开篇语前序前言第一部分&#xff1a;线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分&#xff1a;synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分&#xff…...

uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖

在前面的练习中&#xff0c;每个页面需要使用ref&#xff0c;onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入&#xff0c;需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...

理解 MCP 工作流:使用 Ollama 和 LangChain 构建本地 MCP 客户端

&#x1f31f; 什么是 MCP&#xff1f; 模型控制协议 (MCP) 是一种创新的协议&#xff0c;旨在无缝连接 AI 模型与应用程序。 MCP 是一个开源协议&#xff0c;它标准化了我们的 LLM 应用程序连接所需工具和数据源并与之协作的方式。 可以把它想象成你的 AI 模型 和想要使用它…...

CentOS下的分布式内存计算Spark环境部署

一、Spark 核心架构与应用场景 1.1 分布式计算引擎的核心优势 Spark 是基于内存的分布式计算框架&#xff0c;相比 MapReduce 具有以下核心优势&#xff1a; 内存计算&#xff1a;数据可常驻内存&#xff0c;迭代计算性能提升 10-100 倍&#xff08;文档段落&#xff1a;3-79…...

土地利用/土地覆盖遥感解译与基于CLUE模型未来变化情景预测;从基础到高级,涵盖ArcGIS数据处理、ENVI遥感解译与CLUE模型情景模拟等

&#x1f50d; 土地利用/土地覆盖数据是生态、环境和气象等诸多领域模型的关键输入参数。通过遥感影像解译技术&#xff0c;可以精准获取历史或当前任何一个区域的土地利用/土地覆盖情况。这些数据不仅能够用于评估区域生态环境的变化趋势&#xff0c;还能有效评价重大生态工程…...

Spring数据访问模块设计

前面我们已经完成了IoC和web模块的设计&#xff0c;聪明的码友立马就知道了&#xff0c;该到数据访问模块了&#xff0c;要不就这俩玩个6啊&#xff0c;查库势在必行&#xff0c;至此&#xff0c;它来了。 一、核心设计理念 1、痛点在哪 应用离不开数据&#xff08;数据库、No…...

MySQL JOIN 表过多的优化思路

当 MySQL 查询涉及大量表 JOIN 时&#xff0c;性能会显著下降。以下是优化思路和简易实现方法&#xff1a; 一、核心优化思路 减少 JOIN 数量 数据冗余&#xff1a;添加必要的冗余字段&#xff08;如订单表直接存储用户名&#xff09;合并表&#xff1a;将频繁关联的小表合并成…...

自然语言处理——文本分类

文本分类 传统机器学习方法文本表示向量空间模型 特征选择文档频率互信息信息增益&#xff08;IG&#xff09; 分类器设计贝叶斯理论&#xff1a;线性判别函数 文本分类性能评估P-R曲线ROC曲线 将文本文档或句子分类为预定义的类或类别&#xff0c; 有单标签多类别文本分类和多…...