实现日期间的运算——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.mzph.cn/news/111600.shtml

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

相关文章

SLAM 14 notes

4.23 推导 f ( x ) f(x) f(x)在点a处的泰勒展开 f ( x ) ∑ n 0 ∞ f ( n ) a n ! ( x − a ) n f(x) \sum_{n0}^\infty \frac{f^{(n)}a}{n!}(x-a)^n f(x)∑n0∞​n!f(n)a​(x−a)n l n x lnx lnx的n阶导数 l n ( n ) x ( − 1 ) n − 1 ( n − 1 ) ! x n ln^{(n)}x \fr…

react 中获取多个input输入框中的值的 俩种写法

目录 1. 使用受控组件 2. 使用非受控组件 1. 使用受控组件 这是React中最常见的方法&#xff0c;每个输入框都与React组件的state相关联&#xff0c;并通过onChange事件来更新state。 代码示例&#xff1a; import React, { Component } from react;class MultipleInputExam…

在thonny软件里安装python包 比如 numpy pygame

有一些程序使用了第三方库。如果本地没有安装相应的Python包&#xff0c;这个程序就不能正常运行了。 Python包管理工具提供了对Python 包的查找、下载、安装、卸载的功能。 网络上有很多第三方库&#xff0c;不管要下载哪一个&#xff0c;都需要通过正确的名称来下载安装。 …

websocket+node+vite(vue)实现一个简单的聊天

1.前端逻辑 本项目基于之前搭建的vite环境&#xff1a;https://blog.csdn.net/beekim/article/details/128083106?spm1001.2014.3001.5501 新增一个登录页和聊天室页面 <template><div>登录页</div><div>用户名:<input type"text" pl…

无人机电力巡检:国网安徽实际案例解析

在科技快速发展的今天&#xff0c;传统行业正在经历前所未有的转型。电力巡检&#xff0c;这一看似传统且乏味的任务&#xff0c;却因为无人机技术的介入而焕发新生。今天&#xff0c;让我们深入了解一个具体的案例&#xff0c;探索无人机如何革新电力巡检。 案例背景&#xff…

【Linux】:权限

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本期来给大家解读一下有关Linux的基础知识点&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入门到精通 数…

mac电脑安装雷蛇管理软件,实现调整鼠标dpi,移动速度,灯光等

雷蛇官网只给了win版本驱动 mac版本驱动到这里下载: GitHub - 1kc/razer-macos: Color effects manager for Razer devices for macOS. Supports High Sierra (10.13) to Monterey (12.0). Made by the community, based on openrazer. 安装后会显示开发者不明,请丢弃到垃圾桶.…

ORACLE内存结构

内存体系结构 ​​​​​​​ 目录 内存体系结构 2.1自动内存管理 2.2自动SGA内存管理 2.3手动SGA内存管理 2.3.1数据库缓冲区 2.3.1.1保留池 2.3.1.2回收池 2.3.2共享池 2.3.2.1SQL查询结果和函数查询结果 2.3.2.2库缓存 2.3.2.3数据字典缓存 2.3.3大池 2.3.4 …

Redux详解(二)

1. 认识Redux Toolkit Redux Toolkit 是官方推荐的编写 Redux 逻辑的方法。 通过传统的redux编写逻辑方式&#xff0c;会造成文件分离过多&#xff0c;逻辑抽离过于繁琐&#xff08;具体可看上篇文章 Redux详解一&#xff09;&#xff0c;React官方为解决这一问题&#xff0c;推…

【excel】列转行

列转行 工作中有一些数据是列表&#xff0c;现在需要转行 选表格内容&#xff1a;在excel表格中选中表格数据区域。点击复制&#xff1a;在选中表格区域处右击点击复制。点击选择性粘贴&#xff1a;在表格中鼠标右击点击选择性粘贴。勾选转置&#xff1a;在选择性粘勾选转置选…

Ubuntu 18.04 LTS中cmake-gui编译opencv-3.4.16并供Qt Creator调用

一、安装opencv 1.下载opencv-3.4.16的源码并解压 2.在解压后的文件夹内新建文件夹build以及opencv_install 3.启动cmake-gui并设置 sudo cmake-gui&#xff08;1&#xff09;设置界面中source及build路径 &#xff08;2&#xff09;点击configure&#xff0c;选择第一个def…

正点原子嵌入式linux驱动开发——Linux按键输入

在前几篇笔记之中都是使用的GPIO输出功能&#xff0c;还没有用过GPIO输入功能&#xff0c;本章就来学习一下如果在Linux下编写GPIO输入驱动程序。正点原子STM32MP1开发板上有三个按键&#xff0c;就使用这些按键来完成GPIO输入驱动程序&#xff0c;同时利用原子操作来对按键值进…

Ubuntu20.4 设置代理

主要是涉及2个代理 涉及apt 可以在、/etc/apt/apt.conf 中进行修改 在系统全局可以在/etc/profile中进行修改

【深度学习基础知识(一):卷积神经网络CNN基础知识】

深度学习基础知识 深度学习基础知识&#xff08;一&#xff09;&#xff1a;卷积神经网络CNN基础知识 卷积神经网络CNN基础知识 0、目录 1. CNN卷积神经网络的特点 2. 卷积操作基础知识 2.1 卷积操作的概念2.2 卷积操作的种类2.3 卷积操作后特征图谱大小计算公式 3. 池化操…

02、Python ------- 简单爬取下载小视频

简单爬取小视频 1、装模块 按键盘 winr 输入cmd , 输入命令&#xff1a; pip install requests 也有说在这个目录下面执行命令 pip install requests 执行失败 执行如果失败&#xff0c;在命令后面添加镜像 pip install requests -i https://mirrors.aliyun.com/pypi/sim…

Steam中如何设置HTTP服务器防封

要在 Steam 中设置HTTP服务器&#xff0c;请按照以下步骤操作&#xff1a; 1、打开 Steam 客户端。 2、点击“设置”&#xff08;即齿轮图标&#xff09;&#xff0c;然后选择“网络”。 3、在“网络”页面中&#xff0c;找到“HTTP服务器”部分。 4、首先&#xff0c;将“使…

显示杂谈(二)winscope的使用

WinScope 提供了用于在窗口转换期间和转换后记录和分析 WindowManager 状态和 SurfaceFlinger 状态的基础架构和工具。WinScope 将所有相关的系统服务状态记录在一个跟踪文件中&#xff0c;您可以使用该文件重现并逐步查看转换。 抓winscope相关文件: 通过快捷设置记录跟踪情…

Nacos 401 Client not connected

jar包在本地运行没有问题&#xff0c;但是把包放到linux上就运行不起来&#xff0c;报错如下&#xff08;远程debug截的图&#xff09; 后来看到文章-猜测可能和连接时间有关系 就是本地连接快&#xff0c;linux建立连接慢&#xff0c;采用上面文章的人工强制sleep建议&#…

Vue2基础知识(二) 计算属性/侦听器/生命周期

&#x1f48c; 所属专栏&#xff1a;【Vue2】&#x1f600; 作 者&#xff1a;长安不及十里&#x1f4bb;工作&#xff1a;目前从事电力行业开发&#x1f308;目标&#xff1a;全栈开发&#x1f680; 个人简介&#xff1a;一个正在努力学技术的Java工程师&#xff0c;专注基础和…

[云原生1.] Docker容器的简单介绍和基本管理

文章目录 1. Docker容器的基本概述1.1 简介1.2 容器的优点1.3 Docker与虚拟机的区别1.4 Docker核心组成1.4.1 镜像1.4.2 容器1.4.3 仓库 1.5 容器在内核中支持2种重要技术1.5.1 linux六大namespace&#xff08;命名空间&#xff09; 1.6 Docker的使用场景 2. Docker的部署2.1 前…