实现日期间的运算——C++

在这里插入图片描述
请添加图片描述

😶‍🌫️Take your time ! 😶‍🌫️
💥个人主页:🔥🔥🔥大魔王🔥🔥🔥
💥代码仓库:🔥🔥魔王修炼之路🔥🔥
💥所属专栏:🔥魔王的修炼之路–C++🔥
如果你觉得这篇文章对你有帮助,请在文章结尾处留下你的点赞👍和关注💖,支持一下博主。同时记得收藏✨这篇文章,方便以后重新阅读。

  • 学习完C++入门以及类和对象,我们已经迫不及待的想写一个自己的小项目了,下面这个计算日期的小项目就是运用类和对象写出来的。

Date.h

#pragma once#include <iostream>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>using namespace std;class Date
{
public://获取某年某月的天数
int GetMonthDay(int year, int month);//全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);//拷贝构造函数
//d2(d1)
Date(const Date& d);//赋值运算符重载
//d2 = d3 -> d2.operator = (&d2,d3)
Date& operator= (const Date& d);//析构函数
~Date();// >运算符重载
bool operator>(const Date& d);// ==运算符重载
bool operator==(const Date& d);// >=运算符重载
bool operator>=(const Date& d);// <
bool operator<(const Date& d);// <=
bool operator<=(const Date& d);// !=
bool operator!=(const Date& d);//日期+=天数
Date& operator += (int day);//日期+天数
Date operator+(int day);//日期-天数
Date operator-(int day);//日期-=天数
Date& operator-=(int day);//前置++
Date& operator++();//后置++
Date operator++(int);//后置--
Date operator--(int);//前置--
Date& operator--();//日期-日期 返回天数
int operator-(const Date& d);//打印
void PrintDate();private:int _year;int _month;int _day;
};

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1#include "Date.h"//获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{static int arr[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 arr[month];
}//全缺省的构造函数
Date::Date(int year, int month, int day)
{this->_year = year;this->_month = month;_day = day;
}//拷贝构造函数
//de(d1)
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}//赋值运算符重载
Date& Date::operator=(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;return *this;
}//析构函数
Date:: ~Date()
{_year = 0;_month = 0;_day = 0;
}// >
bool Date::operator>(const Date& d)
{if (_year > d._year)return true;if (_year == d._year && _month > d._month)return true;if (_year == d._year && _month == d._month && _day > d._day)return true;return false;
}// ==
bool Date::operator==(const Date& d)
{if (_year == d._year && _month == d._month && _day == d._day)return true;return false;
}// >=
bool Date::operator>=(const Date& d)
{if (*this > d || *this == d)return true;return false;
}// <
bool Date::operator<(const Date& d)
{if (*this > d || *this == d)return false;return true;
}// <=
bool Date::operator<=(const Date& d)
{if (*this < d || *this == d)return true;return false;}// !=
bool Date::operator!=(const Date& d)
{if (*this == d)return false;return true;
}//下面这两组一共调用两次拷贝构造
// 日期+=天数
Date& Date::operator+=(int day)
{this->_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month > 12){_year++;_month = 1;}}return *this;
}// 日期+天数
Date Date::operator+(int day)
{//Date temp = *this;//与下面的等效,都是用一个已经存在的对象初始化正在创建的对象,//编译器会调用拷贝构造函数,而不是赋值运算符重载,不要被表象迷惑。Date temp(*this);temp += day;return temp;
}//下面这两组一共调用四次拷贝构造
// 日期-天数
Date Date::operator-(int day)
{Date temp(*this);while (temp._day<day){temp._month--;if (temp._month == 0){temp._year--;temp._month = 12;}temp._day += GetMonthDay(_year, _month);}temp._day -= day;return temp;
}//日期-=天数
Date& Date::operator-=(int day)
{*this = *this - day;return *this;
}//前置++
Date& Date::operator++()
{*this += 1;return *this;
}//后置++
Date Date::operator++(int)
{Date temp(*this);*this += 1;return temp;
}//后置--
Date Date::operator--(int)
{Date temp(*this);*this -= 1;return temp;
}//前置--
Date& Date::operator--()
{*this -= 1;return *this;
}//日期 - 日期 -> 返回天数
int Date::operator-(const Date& d)
{int year_day = 0;int month_day = 0;int day_day = 0;if (_year != d._year){int a = abs(_year - d._year);while (a--){if (_year > d._year){if (GetMonthDay(d._year + a, 2) == 29)year_day += 366;elseyear_day += 365;}else{if (GetMonthDay(_year + a, 2) == 29)year_day += 366;elseyear_day += 365;}}}if (_year < d._year)year_day = -year_day;if (1){int a = _month - 1;int _month_day = 0;int d_month_day = 0;while (a){a--;_month_day += GetMonthDay(_year, a);}a = d._month - 1;while (a){a--;d_month_day += GetMonthDay(d._year, a);}month_day = _month_day - d_month_day;}if (_day != d._day);{day_day += _day - d._day;}return year_day + month_day + day_day;
}//打印
void Date::PrintDate()
{cout << _year << ' ' << _month << ' ' << _day << endl;
}

test.cpp

#define _CRT_SECURE_NO_WARNINGS 1#include "Date.h"int main()
{//Date d1(2020, 1, 1);//Date d2(d1);//d2 += 2;//Date d4(++d1);//d1.PrintDate();//d2.PrintDate();//d4.PrintDate();//cout << d1 - d2 << endl;//Date d3(2018, 8, 23);//cout << d1- d3 << endl;//cout << (d1 < d2) << endl;Date d1(2004, 4, 6);Date d2(2023, 10, 13);cout << d2 - d1 << endl;return 0;
}
  • 以上就是我们通过学习类和对象编写的第一个cpp小项目,可以用来比较日期大小、相减得出两个日期间相差多少天、一个日期加上一个天数所得的另一个日期等等。


  • 博主长期更新,博主的目标是不断提升阅读体验和内容质量,如果你喜欢博主的文章,请点个赞或者关注博主支持一波,我会更加努力的为你呈现精彩的内容。

🌈专栏推荐
😈魔王的修炼之路–C语言
😈魔王的修炼之路–数据结构初阶
😈魔王的修炼之路–C++
😈魔王的修炼之路–Linux
更新不易,希望得到友友的三连支持一波。收藏这篇文章,意味着你将永久拥有它,无论何时何地,都可以立即找到重新阅读;关注博主,意味着无论何时何地,博主将永久和你一起学习进步,为你带来有价值的内容。

请添加图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/165857.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

总结一下vue中v-bind的常见用法

文章目录 &#x1f415;前言&#xff1a;&#x1f3e8;简述一下v-bind命令&#x1f9f8;其它写法1.还是当成字符串&#x1f993;其它写法2.当成对象来使用 &#x1f415;前言&#xff1a; 本篇博客主要总结一下v-bind命令在vue中的常见用法&#xff08;看完即懂&#xff09; …

英语——歌诀篇——歌诀记忆法

介词用法速记歌 年月季前要用in&#xff0c; 日子前面却不行。 遇到几号要用on&#xff0c; 上午下午又用in。 要说某时上下午&#xff0c; 用on换in才可行。 午夜黄昏和黎明&#xff0c; 要用at不用in。 差儿分到几点&#xff0c; 写个“to”在中间。 若是几点过几分&#xf…

el-input单独校验

el-input单独校验,效果图如下 <el-col :span"24"><el-form-item label"修订次数:" prop"sPublish"><el-input-numberv-model"addForm.sPublish":min"0":controls"false":precision"0"p…

Flutter视图原理之StatefulWidget,InheritedWidget

目录 StatefulElement1. 构造函数2. build3. _firstBuild3. didChangeDependencies4. setState InheritedElement1. Element类2. _updateInheritance3. InheritedWidget数据向下传递3.1 dependOnInheritedWidgetOfExactType 4. InheritedWidget的状态绑定4.1. ProxyElement 在f…

【项目设计】网络对战五子棋(上)

想回家过年… 文章目录 一、项目前置知识1. websocketpp库1.1 http1.0/1.1和websocket协议1.2 websocketpp库接口的前置认识1.3 搭建一个http/websocket服务器 2. jsoncpp库3. mysqlclient库 二、 项目设计1. 项目模块划分2. 实用工具类模块2.1 日志宏封装2.2 mysql_util2.3 j…

3D视觉硬件技术

目前市面上主流的3D光学视觉方案有三种&#xff1a; 双目立体视觉法&#xff08;Stereo Vision&#xff0c;在下文称双目法&#xff09;&#xff0c;结构光法&#xff08;Structured Light&#xff0c;在下文称结构光&#xff09;以及飞行时间法(Time of Flight, ToF在下文称T…

【JavaEE重点知识归纳】第9节:抽象类和接口

目录 一&#xff1a;抽象类 1.概念 2.语法 3.特性 4.作用 二&#xff1a;接口 1.概念 2.语法 3.接口使用 4.特性 5.实现多个接口 6.接口间的继承 7.Comparable接口 8.Clonable接口 9.抽象类和接口的区别 一&#xff1a;抽象类 1.概念 &#xff08;1&#xff0…

iOS如何实现语音转文字功能?

1.项目中添加权限 Privacy - Speech Recognition Usage Description : 需要语音识别权限才能实现语音转文字功能 2.添加头文件 #import <AVFoundation/AVFoundation.h> #import<Speech/Speech.h> 3.实现语音转文字逻辑: 3.1 根据wav语音文件创建请求 SFSpeechU…

Java String类

字符串转义符号为 \ 常见的转义字符 转移字符对应的英文是 escape character , 转义字符串&#xff08; Escape Sequence &#xff09; 字母前面加上捺斜线 "" 来表示常见的那些不能显示的 ASCII 字符 . 称为转义字符 . 如 \0,\t,\n 等&#xff0c;就称为转 义字…

【【萌新的FPGA学习之快速回顾 水 水 】】

萌新的FPGA学习之快速回顾 水 水 上一条FPGA的更新在9 25 并且2个礼拜没写 verilog 了 正好 刷新一下记忆 FPGA CPU DSP 的对比 在数字电路发展多年以来&#xff0c;出现了 CPU、DSP 和 FPGA 三种经典器件&#xff0c;每个都是具有划时代意义的器件。CPU、DSP 和 FPGA 都有各…

QT 操作Windows系统服务

Windows服务是在Windows操作系统上运行的后台应用程序&#xff0c;它们在系统启动时自动启动&#xff0c;并在后台持续运行&#xff0c;不需要用户交互。Windows服务的作用包括但不限于以下几个方面&#xff1a;1. 提供系统功能&#xff1a;许多Windows服务提供了系统级的功能和…

大数据技术学习笔记(二)—— Hadoop运行环境的搭建

目录 1 模版虚拟机准备1.1 修改主机名1.2 修改hosts文件1.3 修改IP地址1.3.1 查看网络IP和网关1.3.2 修改IP地址 1.4 关闭防火墙1.5 创建普通用户1.6 创建所需目录1.7 卸载虚拟机自带的open JDK1.8 重启虚拟机 2 克隆虚拟机3 在hadoop101上安装JDK3.1 传输安装包并解压3.2 配置…

腾讯地图基本使用(撒点位,点位点击,弹框等...功能) 搭配Vue3

腾讯地图的基础注册账号 展示地图等基础功能在专栏的上一篇内容 大家有兴趣可以去看一看 今天说的是腾讯地图的在稍微一点的基础操作 话不多说 直接上代码 var marker ref(null) var map var center ref(null) // 地图初始化 const initMap () > {//定义地图中心点坐标…

不想加班的小伙伴们,请把这四个神器焊在电脑上~

今天又来给大家分享干货啦&#xff0c;如果你下载视频没渠道&#xff0c;写方案没灵感思路&#xff0c;做表格太慢&#xff0c;做海报太复杂&#xff0c;那你一点要看这一篇&#xff0c;今天分享的四个宝藏网站专门解决以上问题&#xff0c;一起来看看吧&#xff01; 一、WeDow…

SAP-QM-动态检验规则

Dynamic Modification Rule &#xff08;动态修改规则&#xff09; 1、决定样本大小的方式有3种&#xff1a; 手动输入比例大小采样过程 物料主数据质量视图 2、采样过程的创建方式有2种 跟批量大小有关系&#xff1a;百分比/AQL跟批量大小没有关系&#xff1a;固定值 而当…

【RNA biology】RNA的多功能性与早期生命进化

文章目录 RNARNA plays core functions in Central Dogma of BiologyrRNAsnRNA RNA worldReplication催化作用感知环境变化并作出响应 来自Manolis Kellis教授&#xff08;MIT计算生物学主任&#xff09;的课 油管链接&#xff1a;6.047/6.878 Lecture 7 - RNA folding, RNA wo…

概念解析 | 心脏电活动和机械活动之间的关系

注1:本文系“概念解析”系列之一,致力于简洁清晰地解释、辨析复杂而专业的概念。本次辨析的概念是:心脏电活动和机械活动之间的关系。 心跳的交响乐:心脏电活动与机械活动之间的关联 一、背景介绍 心脏通过不断跳动将血液输送到我们身体的每一个角落。而这个跳动过程,是…

3D测量之圆孔测量 拟合圆 点云变换

0. 效果展示 1. 圆孔测量介绍 此文中的圆孔测量是一项3D视觉技术,旨在精确测量物体表面上的圆孔的直径和中心坐标。通过使用高精度3D相机(线激光轮廓仪或结构体等)采集原始点云数据,通过3D视觉算法能够快速、准确地分析物体上的圆孔特征,为制造和工程领域提供了强大的测量…

插入排序(学习笔记)

插入排序 每一轮插入排序后的结果与打扑克牌取牌原理相似&#xff0c;将取到的牌插入到合适的位置&#xff0c;但在程序实现方面还是基于交换的算法。 它的基本思想是将一个记录插入到已经排好序的有序表中&#xff0c;从而一个新的、记录数增1的有序表。 import java.util.…

【(数据结构)—— 基于单链表实现通讯录】

&#xff08;数据结构&#xff09;—— 基于单链表实现通讯录 一.通讯录的功能介绍1.基于单链表实现通讯录(1). 知识要求(2). 功能要求 二.通讯录的代码实现1.通讯录的底层结构(单链表)(1).思路展示(2).底层代码实现(单链表)1.单链表头文件 —— &#xff08;函数的定义&#x…