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

news2025/7/19 16:52:44

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

😶‍🌫️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;
	}
	else
		return 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;
				else
					year_day += 365;
			}
			else
			{
				if (GetMonthDay(_year + a, 2) == 29)
					year_day += 366;
				else
					year_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.coloradmin.cn/o/1102728.html

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

相关文章

Java内存模型-Java Memory Model(JMM)-可见性、原子性、有序性

5. Java内存模型之JMM 5.1 先从大场面试开始 你知道什么是Java内存模型JMM吗&#xff1f; JMM和volatile他们两个之间的关系&#xff1f; JMM没有那些特征或者它的三大特征是什么&#xff1f; 为什么要有JMM&#xff0c;它为什么出现&#xff1f;作用和功能是什么&#xf…

C++数据结构X篇_15_求二叉树叶子数与高度(递归方法)

本篇参考求二叉树叶子数与高度&#xff08;C&#xff09;进行整理。 文章目录 1. 二叉树中叶子数与高度2. 求二叉树叶子数与高度的实现代码 1. 二叉树中叶子数与高度 我们首先来看一看二叉树中叶子数与高度的定义&#xff1a; 叶子数&#xff1a;对于一个二叉树的节点&#x…

【微信小程序】6天精准入门(第3天:小程序flex布局、轮播图组件及mock运用以及综合案例)附源码

一、flex布局 布局的传统解决方案&#xff0c;基于[盒状模型]&#xff0c;依赖display属性 position属性 float属性 1、什么是flex布局&#xff1f; Flex是Flexible Box的缩写&#xff0c;意为”弹性布局”&#xff0c;用来为盒状模型提供最大的灵活性。任何一个容器都可以…

基于五折交叉验证的支持向量机SVR回归预测研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

七大排序 (9000字详解直接插入排序,希尔排序,选择排序,堆排序,冒泡排序,快速排序,归并排序)

一&#xff1a;排序的概念及引入 1.1 排序的概念 1.1 排序的概念 排序&#xff1a;所谓排序&#xff0c;就是使一串记录&#xff0c;按照其中的某个或某些关键字的大小&#xff0c;递增或递减的排列起来的操作。 稳定性&#xff1a;假定在待排序的记录序列中&#xff0c;存在…

[Python中常用的回归模型算法大全2:从线性回归到XGBoost]

文章目录 概要多输出K近邻回归集成算法回归梯度提升决策树回归随机森林回归 概要 回归分析在数据科学领域扮演着关键角色&#xff0c;用于预测数值型目标变量。本文深入探讨了几种常用的回归模型&#xff0c;包括多输出K近邻回归&#xff0c;决策树回归&#xff0c;集成算法回…

告别手动调节!iOS 17让你全自动调节音量大小,那么如何实现个性化音量呢

多亏了iOS 17&#xff0c;你的AirPods Pro 2现在具有个性化音量功能&#xff0c;可以根据周围环境智能调整音频音量。 这很酷&#xff0c;任何喜欢尽可能降低音量以避免听力受损的人都会对此表示赞赏。使用个性化音量&#xff0c;你的iPhone将检测音量何时可以降低&#xff0c…

MyBatisPlus-02

一 查询条件的三种 1.按条件查询 //方式一&#xff1a;按条件查询QueryWrapper qw new QueryWrapper();qw.lt("age",18);List<User> userList userDao.selectList(qw);System.out.println(userList); 2.lambda格式按条件查询 //方式二&#xff1a;lambda格…

【前端学习】—函数节流(九)

【前端学习】—函数节流&#xff08;九&#xff09; 一、什么是函数节流 函数节流&#xff1a;规定在一个单位时间内&#xff0c;事件响应函数只能被触发一次&#xff0c;如果这个单位时间内触发多次函数&#xff0c;只有一次生效。 二、函数节流使用场景 window.onresize事…

pytorch代码实现之动态蛇形卷积模块DySnakeConv

动态蛇形卷积模块DySnakeConv 血管、道路等拓扑管状结构的精确分割在各个领域都至关重要&#xff0c;确保下游任务的准确性和效率。 然而&#xff0c;许多因素使任务变得复杂&#xff0c;包括薄的局部结构和可变的全局形态。在这项工作中&#xff0c;我们注意到管状结构的特殊…

安达发|大多数离散型生产模式适用APS自动排程系统

在离散型生产模式中&#xff0c;智能生产排程软件&#xff08;APS&#xff09;的应用越来越广泛。这是因为APS能够根据实时的生产需求和资源状况&#xff0c;自动进行生产计划的制定和调整&#xff0c;从而提高生产效率&#xff0c;降低生产成本&#xff0c;保证生产的顺利进行…

美国股票和加密货币平台【Alpaca】完成1500万美元融资

来源&#xff1a;猛兽财经 作者&#xff1a;猛兽财经 猛兽财经获悉&#xff0c;总部位于美国加利福尼亚州圣马特奥的股票和加密交易经纪平台提供商&#xff0c;近期宣布已从SBI集团获得了1500万美元融资。 该公司打算利用这笔资金加快业务扩张&#xff0c;并将其业务范围扩大到…

iPaaS混合集成平台,打造数字化生态

如今企业分工越来越细&#xff0c;上下游合作越来越紧密、各企业之间的业务系统需要相互协作完成业务、外部API依赖越来越多、同时企业系统运行在多个混合云环境及SaaS中&#xff0c;私有端大量业务系统与云端系统形成了错综复杂的集成关系&#xff0c;企业面临集成技术复杂多样…

Springboot整合taos时序数据库TDengine

1.首先安装TDengine服务端在linux上 TDengine多种安装包的安装和卸载 - TDengine | 涛思数据安装过程直接去官网看,非常详细简单 2.出现的问题 windows连接 invalid app version 版本不对应 版本不对应的问题,需要在linux上安装的版本和windows client版本一致,不然w…

Kubernetes基础(六)-常见 Kubernetes Pod 驱逐场景

Kubernetes Pod 被驱逐是什么意思&#xff1f; 它们被终止&#xff0c;通常是没有足够资源的结果。但是为什么会这样呢&#xff1f; 驱逐是指派给节点的Pod 被终止的过程。 Kubernetes 中最常见的情况之一是Preemption&#xff0c;为了在资源有限的节点中调度新的 Pod&#…

安卓14通过“冻结”缓存应用程序腾出CPU,提高性能和内存效率

本月早些时候&#xff0c;我们听说更新到安卓14似乎提高了谷歌Pixel 7和Pixel 6的效率——提高了电池寿命&#xff0c;并在这个过程中减少了热量的产生。现在看来&#xff0c;安卓14的增效功能细节已经公布。 安卓侦探Mishaal Rahman在X&#xff08;前身为Twitter&#xff09;…

林沛满--快递员的工作策略——TCP窗口

本文整理自&#xff1a;《Wireshark网络分析就这么简单 第1版》 作者&#xff1a;林沛满 著 出版时间&#xff1a;2014-12 假如你是一位勤劳的快递员&#xff0c;要送100个包裹到某公司去&#xff0c;怎样送货才科学? 最简单的方式是每次送1个&#xff0c;总共跑100趟。当然这…

uCOSIII实时操作系统 八 软件定时器

目录 软件定时器概述 使用步骤&#xff1a; 创建软件定时器&#xff1a; 启动软件定时器&#xff1a; 停止软件定时器&#xff1a; 删除软件定时器&#xff1a; 单次定时器&#xff1a; ​编辑周期定时器&#xff1a; 无初始化延时&#xff1a; 有初始化延时&#xff…

LabVIEW中使用Get LV Class Default Value 出现错误1498

LabVIEW中使用Get LV Class Default Value 出现错误1498 在LabVIEW中开发了一个应用程序&#xff0c;其中包含可以在执行时动态配置插件的基类。生成可执行文件后&#xff0c;当应用程序要执行子类时&#xff0c;收到以下错误信息。 Error1498 occurred at Gen LV Class Defa…