实现日期间的运算——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…

Qt文件系统模型

创建文件系统模型&#xff1a;QFileSystemModel* model new QFileSystemModel(this); 设置根目录&#xff1a;model->setRootPath(QDir::currentPath()); 为视图设置模型&#xff1a; ui.treeView->setModel(model);ui.listView->setModel(model);ui.tableView-&g…

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

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

TCP ZeroWindow 问题

TCP Zero Window问题是指在TCP连接中&#xff0c;发送方为了保障可靠传输&#xff0c;会根据接收方反馈的窗口大小来控制发送窗口的大小&#xff0c;但当接收方窗口大小为0时&#xff0c;发送方就会停止发送&#xff0c;从而导致通讯中断的问题。下面我们将从多个方面详细阐述T…

在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…

【SA8295P 源码分析 (一)】14 - Passthrough配置文件 /mnt/vm/images/linux-la.config 内容分析

【SA8295P 源码分析】14 - Passthrough配置文件 /mnt/vm/images/linux-la.config 内容分析 系列文章汇总见:《【SA8295P 源码分析 (一)】系统部分 文章链接汇总 - 持续更新中》 本文链接:《【SA8295P 源码分析 (一)】14 - Passthrough配置文件 /mnt/vm/images/linux-la.confi…

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

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

香港闯关相关法律

在香港&#xff0c;与闯关活动相关的法律主要包括以下几个方面&#xff1a; 刑事法律&#xff1a;闯关活动可能涉及犯罪行为&#xff0c;如非法入境、非法越境等&#xff0c;这些行为都可能触犯香港的刑事法律。 消费者保护法律&#xff1a;由于闯关活动通常涉及消费者付费购买…

【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;推…

MVC、MVP、MVVM理解 在什么情况下用什么页面架构

1、不用页面架构 优点&#xff1a;简单 缺点&#xff1a;可读性、可维护性、可扩展性很弱 应用场景&#xff1a;欢迎、关于、帮助、隐私条款、用户协议等待 2、mvc M&#xff1a;model 模型&#xff0c;包括数据请求及操作 V&#xff1a;xml C&#xff1a;Controller activit…

vue报错不能将类型“number”分配给类型“string”

目录 前情提要 报错信息&#xff1a; 适用场景&#xff1a; 解决方法&#xff1a; 代码案例中的逻辑&#xff1a; 修改方式&#xff1a; 前情提要 报错信息&#xff1a; 不能将类型“number”分配给类型“string” 适用场景&#xff1a; 前端是string类型的数据例如单选框…

【excel】列转行

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

c 语言基础:L1-047 装睡

你永远叫不醒一个装睡的人 —— 但是通过分析一个人的呼吸频率和脉搏&#xff0c;你可以发现谁在装睡&#xff01;医生告诉我们&#xff0c;正常人睡眠时的呼吸频率是每分钟15-20次&#xff0c;脉搏是每分钟50-70次。下面给定一系列人的呼吸频率与脉搏&#xff0c;请你找出他们…

【网络空间实战攻防能力训练】DHCP欺骗

注意!!!!! 本实验方法一定不要用来攻击公网的服务器,仅能在自己的虚拟机里进行操作!不然可能构成违法行为,大家一定注意! DHCP欺骗 0x01 实验环境搭建0x02 部署DHCP服务器1、配置Windows Server 20162、在Windows 2016 Server上添加DHCP服务器3、设置Kali Linux与Wind…

前端页面根据后端返回的文本将换行符(“↵”)进行换行展示

有时我们会遇到这种情况&#xff0c;后端传递了一大段包含了回车符的文本内容&#xff0c;前端展示的时候所有文字堆在一起&#xff0c;没有换行展示。 以下方法中content为后端返回的文本内容 方法一&#xff1a; “↵”符号在html中会识别别为\r,\n等转义字符&#xff0c;…

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…