C++ 日期计算器

news2024/9/27 7:15:40

日期计算器

        • 概要
    • Date类的规划
    • Date类的实现
      • Date 构造函数
      • Date 拷贝构造函数
      • ~Date 析构函数
      • GetMonthDay 求某年某月的天数
      • operator= 赋值操作符重载
      • operator+= 加等操作符重载
      • operator+ 加号操作符重载
      • operator-= 减等操作符重载
      • operator- 减法操作符重载 (日期 - 天数)
      • operator++ 前置自增操作符重载
      • operator++ 后置自增操作符重载
      • operator-- 前置自减操作符重载
      • operator-- 后置自减操作符重载
      • operator< 小于操作符重载
      • operator== 相等操作符重载
      • operator!= 不等操作符重载
      • operator<= 小于或等于操作符重载
      • operator> 大于操作符重载
      • operator>= 大于或等于操作符重载
      • operator- 减法操作符重载(日期 - 日期)
      • Print 打印函数
    • Date 类的测试
      • 测试操作符重载(+,+=,- ,-=,x++,++x,x--,--x)
      • 测试操作符重载(>,>=,==,<,<=,!=)
      • 测试操作符重载(日期 - 日期)
        • 结语

请添加图片描述

概要

本篇主要探究C++ 实现日期计算器

Date类的规划

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int year = 2024 , int month = 2, int day = 6);
	Date(const Date& d);
	Date& operator=(const Date& d);
	~Date();
	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--();
	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;
	int operator-(const Date& d) const;
	void Print();

private:
	int _year;
	int _month;
	int _day;
};

Date类的实现

Date 构造函数

Date::Date(int year, int month, int day)
	:_year(year)
	, _month(month)
	, _day(day)
{ }

构造函数,他是在创建类的时候调用进行初始化操作,我们这里声明与定义分离,所以它的参数不需要填写缺省值,缺省值在声明的时候定义即可。

Date 拷贝构造函数

Date::Date(const Date& x)
	:_year(x._year)
	, _month(x._month)
	, _day(x._day)
{ }

拷贝构造函数和构造函数作用相似,拷贝构造函数是将已有的类的对象拷贝给新创建的类的对象,如上所示。

~Date 析构函数

Date::~Date()
{
	_year = _month = _day = 0;
}

构析函数是在对象即将销毁前所调用的函数,常用来清理数据空间,这里我们的内存都在栈上申请的,所以影响不大。

GetMonthDay 求某年某月的天数

int Date::GetMonthDay(int year,int month)
{
	int a[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;
	}
	return a[month];
}

该函数是用来求某年某月的月份天数,通过创建一个大小为13的数组a, 再对二月份进行判断闰年来确定是否为特殊情况,否则返回已设定好的月份天数。

operator= 赋值操作符重载

Date& Date::operator=(const Date& d)
{
	_year=d._year;
	_month=d._month;
	_day=d._day;
	return *this;
}

该函数用于对象与对象之间的赋值操作,在对象的年月日进行逐一复制后并返回this指针的解引用。

operator+= 加等操作符重载

Date& Date::operator+=(int day)
{
	_day+=day;
	int time=GetMonthDay(_year,_month);
	while(_day>time)
	{
		_day-=time;
		_month++;
		if(_month>12)
		{
			_month-=12;
			_year++;
		}
		time = GetMonthDay(_year,_month);
	}
	return *this;
}

该函数用于日期与天数的相加,在实现该函数时先将this->_day加上想加的day。然后再通过月份的天数以及月与年的关系进行调整,如上所示。

operator+ 加号操作符重载

Date Date::operator+(int day)
{
	Date newdate = *this;
	newdate+=day;
	return newdate;
}

通过复用加等操作符重载,在不改变*this的情况下,返回相加后的对象

operator-= 减等操作符重载

Date& Date::operator(int day)
{
	_day-=day;
	int time =GetMonthDay(_year,_month)
	while(_day>=0)
	{
		_day+=time;
		_month--;
		if(_month<=1)
		{
			_month+=12;
			_year--;
		}
		time=GetMonthDay(_year,_month);
	} 
	return *this;
}

该函数和加等操作符重载类似,先将this->_day减去day,然后进行月份的天数和年与月之间的调整即可。

operator- 减法操作符重载 (日期 - 天数)

Date Date::operator-(int day)
{
	Date newdate=*this;
	newdate-=day;
	return newdate;
}

通过复用减等操作符重载,在不改变*this的情况下,返回相加后的对象

operator++ 前置自增操作符重载

Date& Date::operator++()
{
	(*this)+=1;
	return *this;
}

operator++ 后置自增操作符重载

Date Date::operator--(int)
{
	Date a = *this;
	(*this)+=1;
	return a;
}

operator-- 前置自减操作符重载

Date& Date::operator--()
{	
	(*this)-=1;
	return *this;
}

operator-- 后置自减操作符重载

Date Date::operator--(int)
{	
	Date a=*this;
	(*this)-=1;
	return a;
}

operator< 小于操作符重载

bool Date::operator<(const Date& d)
{
	if(_year<d._year)
	{
		return 1;
	}
	else if(_year==d._year)
	{
		if(_month<d._month)
		{
			return 1;
		}
		else if(_month==d._month)
		{
			if(_day<d._day)
			{
				return 1;
			}
		}
	}
	return 0;
}

operator== 相等操作符重载

bool Date::operator==(const Date& d)
{
	return _day==d._day&&_month==d._month&&_year==d._year;
}

operator!= 不等操作符重载

bool Date::operator!=(const Date& d)
{
	return !(*this==d);
}

operator<= 小于或等于操作符重载

bool Date::operator<=(const Date& d)
{
	return (*this<d)||(*this==d);
}

operator> 大于操作符重载

bool Date::operator>(const Date& d)
{
	return !(*this<=d);
}

operator>= 大于或等于操作符重载

bool Date::operator>=(const Date& d)
{
	return !(*this<d);
}

operator- 减法操作符重载(日期 - 日期)

int operator-(const Date& date) const
{
	Date max = *this,min = date;
	int flag=(max>=min);
	if(!flag)
	{
		max=date;
		min=*this;
		flag=-1;
	}
	int count=0;
	while(max>min)
	{
		min++;
		count++;
	}
	return flag*count;
}

通过比较选出最大的日期max与最小的日期min,让最小的日期min和记录数的count进行循环自增,直到max与min相等为止,此时刻的count就是相差的天数,而flag先前用于比较的就是它的符号,所以返回flag*count。

Print 打印函数

void Date::Print()
{
	printf("%d %d %d\n",_year,_month,_day);
}

Date 类的测试

测试操作符重载(+,+=,- ,-=,x++,++x,x–,–x)

void Test_1()
{
	std::cout << "Test_1:\n";
	Date a(2024, 2, 6);
	std::cout << "a ||";
	a.Print();
	Date b = a;
	std::cout << "b ||";
	b.Print();
	b += 30;
	std::cout << "b += 30 ||";
	b.Print();
	a -= 20;
	std::cout << "a -= 20 ||";
	a.Print();
	Date c = b - 30;
	std::cout << "c 或 b - 30 ||";
	c.Print();
	Date d = a + 20;
	std::cout << "d 或 a + 20 ||";
	d.Print();
	d++;
	std::cout << "++d ||";
	d.Print();
	c--;
	std::cout << "++c ||";
	c.Print();
	Date e = a++;
	std::cout << "e = a++ -> e||";
	e.Print();
	std::cout << "e = a++ -> a||";
	a.Print();
	Date f = a++;
	std::cout << "f = b-- -> f||";
	f.Print();
	std::cout << "f = b-- -> b||";
	b.Print();

	std::cout << "\n";
}

运行结果如下:
在这里插入图片描述

测试操作符重载(>,>=,==,<,<=,!=)

void Test_2()
{
	std::cout << "Test_2:\n";
	Date a(2024, 2, 6);
	std::cout << "a ||";
	a.Print();
	Date b = a + 999;
	std::cout << "b ||";
	b.Print();
	Date c = a - 999;
	std::cout << "c ||";
	c.Print();
	Date d = a;
	std::cout << "d ||";
	d.Print();

	std::cout << "a < b  ->" << (a < b) << std::endl;
	std::cout << "a > b  ->" << (a > b) << std::endl;
	std::cout << "a == b ->" << (a == b) << std::endl;
	std::cout << "a != b ->" << (a != b) << std::endl;

	std::cout << "a < c  ->" << (a < c) << std::endl;
	std::cout << "a > c  ->" << (a > c) << std::endl;
	std::cout << "a == c ->" << (a == c) << std::endl;
	std::cout << "a != c ->" << (a != c) << std::endl;

	std::cout << "a == d ->" << (a == d) << std::endl;
	std::cout << "a != d ->" << (a != d) << std::endl;
	std::cout << "a >= d ->" << (a >= d) << std::endl;
	std::cout << "a <= d ->" << (a <= d) << std::endl;
	std::cout << "a < d  ->" << (a < d) << std::endl;
	std::cout << "a > d  ->" << (a > d) << std::endl;

	std::cout << "\n";
}

运行结果如下:
在这里插入图片描述

测试操作符重载(日期 - 日期)

void Test_3()
{
	std::cout << "Test_3:\n";
	Date a(2024, 2, 6);
	Date b(2025, 2, 6);
	Date c = a + 99;
	std::cout << "c ||";
	c.Print();

	std::cout << "a ||";
	a.Print();
	std::cout << "b ||";
	b.Print();

	std::cout << "a - b ||" << (a - b) << std::endl;
	std::cout << "b - b ||" << (b - b) << std::endl;
	std::cout << "b - a ||" << (b - a) << std::endl;

	std::cout << "c - a ||" << (c - a) << std::endl;
	std::cout << "a - c ||" << (a - c) << std::endl;

	std::cout << "\n";
}

运行结果如下:
在这里插入图片描述
测试完毕,综合上述代码及运行结果,可得代码正确,可以正常模拟日期计算器。

结语

以上就是本期的全部内容了,喜欢就多多关注吧!!!

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

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

相关文章

linux C编程入门

Ubuntu 下也有一些可以进行编程的工具&#xff0c;但是大多都只是编辑器&#xff0c; 也就是只能进行代码编辑&#xff0c;如果要编译的话就需要用到 GCC 编译器&#xff0c;使用 GCC 编译器肯定就 要接触到Makefile。 1&#xff1a;hello world!!! 我们所说的编写代码包括两部…

Compose | UI组件(十四) | Navigation-Data - 页面导航传递数据

文章目录 前言传参流程实例说明普通方式传值定义接受参数格式定义接受参数类型获取参数传入参数传参和接受参数效果图 结合 ViewModel 传递参数定义ViewModel在 navigation 定义 ViewModel 实例&#xff0c;并且传入 LoginScreen传入输入框中的值&#xff0c;并且跳转传值获取值…

计算机今年炸了99%的人都踩了这个坑

24年408考研&#xff0c;如果只用王道的复习资料&#xff0c;最多考100-120分 就是这么的现实&#xff0c;王道的资料虽然好&#xff0c;但是并不能覆盖全部的知识点和考点&#xff0c;而且24年的408真题考的很怪&#xff0c;总结起来就是下面这些特点&#xff1a; 偏&#x…

2024年【G2电站锅炉司炉】模拟试题及G2电站锅炉司炉考试试题

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 G2电站锅炉司炉模拟试题是安全生产模拟考试一点通生成的&#xff0c;G2电站锅炉司炉证模拟考试题库是根据G2电站锅炉司炉最新版教材汇编出G2电站锅炉司炉仿真模拟考试。2024年【G2电站锅炉司炉】模拟试题及G2电站锅炉…

「数据结构」八大排序2:快排、归并排序

&#x1f387;个人主页&#xff1a;Ice_Sugar_7 &#x1f387;所属专栏&#xff1a;初阶数据结构 &#x1f387;欢迎点赞收藏加关注哦&#xff01; 八大排序2 &#x1f349;快速排序&#x1f34c;霍尔版本&#x1f34c;挖坑法&#x1f34c;前后指针法 &#x1f349;快排优化&am…

Docker 容器监控-CIG

目录 一、CIG说明 1. CAdvisor 2. InfluxDB 3. Grafana 二、环境搭建 1. 创建目录 2. 编写 docker-compose.yml 3. 检查并运行容器 三、进行测试 1. 查看 influxdb 存储服务 是否能正常访问 2. 查看 cAdvisor 收集服务能否正常访问 3. 查看 grafana 展现服务&#…

服装设计公司,如何用钉钉实现企业数字化成功转型?

钉钉作为数字化工作平台&#xff0c;为某服装设计公司实现了组织管理的数字化转型&#xff0c;构建了一站式的工作平台。通过钉钉赋能&#xff0c;有利于企业推进组织架构、员工沟通、产品运营和客户服务等方面的数字化、智能化转型。 借助钉钉平台&#xff0c;该服设公司轻松实…

澳福实例说明真实交易中止损单和限价单的区别

很多投资者不明白止损单和限价单的区别&#xff0c;今天澳福就举一个例子来说明真实交易中止损单和限价单的区别。 紫色椭圆显示了在欧元兑美元图表上的位置&#xff0c;在不稳定的增长之后&#xff0c;澳福 外汇看到了另一波修正&#xff0c;没有看涨的迹象。同时也发现从历史…

MOS管防反接电路设计

电子元件大都是使用直流工作&#xff0c;电源线反接就有可能就会烧坏&#xff0c;那电路如何防反接&#xff1f;首当其冲我们想到的就是二极管了&#xff0c;运用其单向导通特性可有效防止电源反接而损坏电路&#xff0c;但是随之而来的问题是二极管存在PN节电压&#xff0c;通…

协同算法的无人机集群控制理论技术分析,无人机集群飞行技术详解

随着无人机技术的普及和发展&#xff0c;无人机集群控制也逐渐成为了研究热点之一。而协同算法是实现无人机集群控制的重要手段之一。 在无人机集群控制中&#xff0c;协同算法确实是非常关键的部分。这些算法帮助无人机在复杂的飞行环境中保持队形&#xff0c;同时避免碰撞&a…

Python调用matlab程序

matlab官网&#xff1a;https://ww2.mathworks.cn/?s_tidgn_logo matlab外部语言和库接口&#xff0c;包括 Python、Java、C、C、.NET 和 Web 服务。 matlab和python的版本 安装依赖配置 安装matlab的engine 找到matlab的安装目录&#xff1a;“xxx\ extern\engines\python…

提示由于找不到msvcp120dll无法继续执行此代码怎么办

在计算机系统中&#xff0c;MSVCP120.dll是一个至关重要的动态链接库文件&#xff0c;它是Microsoft Visual C Redistributable Package的一部分&#xff0c;对于许多基于Windows的应用程序运行至关重要。当系统提示“msvcp120dll丢失”时&#xff0c;意味着该文件可能由于误删…

收到微信发的年终奖。。。

大家好&#xff0c;我是小悟 还剩一天就过除夕了&#xff0c;很多单位都已经放假了&#xff0c;街上的人越来越少&#xff0c;门店关着的很多&#xff0c;说明大家都陆陆续续回自己的家乡过年了。 或许你还在搬砖&#xff0c;坚守节前最后一波工作&#xff0c;或许你正在回家的…

必收藏!第六版CCF推荐会议C类国际学术会议!(中国计算机学会)

中国计算机学会 中国计算机学会&#xff08;CCF&#xff09;是全国性、学术性、非营利的学术团体&#xff0c;由从事计算机及相关科学技术领域的个人和单位自愿组成。作为独立社团法人&#xff0c;CCF是中国科学技术协会的成员之一&#xff0c;是全国一级学会&#xff01; CCF的…

算法——前缀和算法

1. 什么是前缀和算法 前缀和算法&#xff08;Prefix Sum&#xff09;是一种用于快速计算数组元素之和的技术。它通过预先计算数组中每个位置前所有元素的累加和&#xff0c;将这些部分和存储在一个新的数组中&#xff0c;从而在需要计算某个区间的和时&#xff0c;可以通过简单…

电脑没有声音是怎么回事?几招快速解决

当电脑突然失去声音&#xff0c;这可能成为一种令人烦恼的体验&#xff0c;尤其是在你期望享受音乐、观看视频或进行在线会议的时候。幸运的是&#xff0c;大多数时候&#xff0c;电脑没有声音的问题是可以迅速解决的。电脑没有声音是怎么回事&#xff1f;本文将为你介绍一些常…

2024.2.7日总结(小程序开发4)

页面导航 页面导航是页面之间的相互跳转&#xff1a; <a>链接location.href 小程序中实现页面导航的两种方式&#xff1a; 声明式导航 在页面上声明一个<navigator>导航组件通过点击<navigator>组件实现页面跳转 编程式导航 调用小程序的导航API&…

【数据结构与算法】二叉树(Binary Tree)

相关推荐&#xff1a;堆&#xff08;Heap&#xff09; / 堆排序&#xff08;HeapSort&#xff09; / TopK 文章目录 1.树1.1 树相关概念1.2 举例树的应用 2. 二叉树2.1 二叉树分类2.2 特殊的二叉树2.3 二叉树的存储结构 3. 二叉树实现与热门问题 1.树 树是一种非线性的数据结构…

详解C++类和对象(下)完结篇

文章目录 写在前面1. 进一步认识构造函数1.1 初始化列表1.2 初始化列表的特性1.3 explicit关键字 2. static成员变量和static成员函数2.1 static成员的概念2.2 static成员的特性 3. 友元3.1 友元函数3.1 友元类 4. 内部类5.匿名对象 写在前面 本篇文章详细介绍了C类和对象中几…

基于SpringBoot+Vue的校园博客管理系统

末尾获取源码作者介绍&#xff1a;大家好&#xff0c;我是墨韵&#xff0c;本人4年开发经验&#xff0c;专注定制项目开发 更多项目&#xff1a;CSDN主页YAML墨韵 学如逆水行舟&#xff0c;不进则退。学习如赶路&#xff0c;不能慢一步。 目录 一、项目简介 二、开发技术与环…