【C++中日期类的实现】

一路,一路,一路从泥泞到风景...............................................................................................

目录

前言

一、【什么是日期类】

二、【代码实现】

1.【Date.h】部分:

2.【Date.cpp】部分:

3.【Test.cpp】部分:

总结



前言

本篇是日期类的编写,望多多指正。


一、【什么是日期类】

在学习完类和对象之后,可以试着编写一个日期类进行练习,使得该类能够加减天数,计算两个日期之间相差的天数,还可以比较两个类之间的大小。

二、【代码实现】

1.【Date.h】部分:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <stdbool.h>
#include <assert.h>
using namespace std;
class Date
{
public:
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
	int GetMonthDay(int year, int month);
	Date(int year = 1, int month = 1, int day = 1);
	Date(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);
	bool operator!=(const Date& d);
	Date& operator+=(int day);
	Date operator+(int day)const;
	Date operator-(int day)const;
	Date& operator-=(int day);
	Date& operator++();
	Date operator++(int);
	Date operator--(int);
	Date& operator--();
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};

2.【Date.cpp】部分:

#include "Date.h"
int Date::GetMonthDay(int year, int month)
{
	const static int MonthDay[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 MonthDay[month];
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (year < 1 || month < 1 || month>12 || day<1 || day>GetMonthDay(year,month))
	{
		cout << "error date" << endl;
		exit(-1);
	}
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
bool Date::operator<(const Date& d)
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year && _month < d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day < d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator==(const Date& d)
{
	
	return (_year == d._year) && (_month == d._month) && (_day == d._day);
}
bool Date::operator<=(const Date& d)
{
	return (*this < d) || (*this == d);
}
bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}
bool Date::operator>=(const Date& d)
{
	return !(*this < d);
}
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= (-day);
	}
	_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)const
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}
//Date Date::operator-(int day)
//{
//	Date tmp(*this);
//	tmp._day -= day;
//	while (tmp._day <= 0)
//	{
//		tmp._month--;
//		if (tmp._month < 1)
//		{
//			tmp._year--;
//			tmp._month = 12;
//		}
//		tmp._day += tmp.GetMonthDay(tmp._year, tmp._month);
//	}
//	return tmp;
//}
//Date& Date::operator-=(int day)
//{
//	*this = *this - day;
//	return *this;
//}


Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day);
	}

	_day -= day;

	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}

Date Date::operator-(int day) const
{
	Date tmp(*this);

	tmp -= day;

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

Date Date::operator++(int)
{
	Date tmp(*this);

	*this += 1;

	return tmp;
}


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

Date Date::operator--(int)
{
	Date tmp(*this);

	*this -= 1;

	return tmp;
}
int Date::operator-(const Date& d)
{
	Date Max = *this;
	Date Min = d;
	int flag = 1;
	if (*this < d)
	{
		Max = d;
		Min = *this;
		flag = -1;
	}
	int n = 0;
	while (Min != Max)
	{
		Min++;
		n++;
	}
	return flag * n;

}
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "/" << d._month << "/" << d._day << endl;

	return out;
}

istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

3.【Test.cpp】部分:

#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
int main()
{
	Date d1(2023,7,1);
	Date d2(2023, 7, 28);
	cout << d1 << endl;
	cout << d2 << endl;
	cout << (d2-d1) << endl;
	cout << (++d2) << endl;
	cout << (d2++) << endl;
	cout << d2 << endl; 
	cout << (d1--) << endl;
	cout << (--d1) << endl;
	Date sum = d2 + 260;
	cout << sum << endl;
	cout << (d2+=480) << endl;
	return 0;
}


总结

本片到这里就结束了,感谢观看!


.......................................................................................................你过得好吗?会想起我吗?

                                                                                                          ————《你过得好吗》

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

关于ffmpeg height not divisible by 2的错误

在我们线上视频生产过程中&#xff0c;我们用ffmpeg对视频做了resize&#xff0c;讲原有的分辨率resize到1280p&#xff0c;使用了参数 -vf "scale1280:-1"&#xff0c;作用是将原始视频宽度缩放成1280&#xff0c;-1是指高度等比例缩放。 之前一直运行的好好的&…

储能技术发展

一、政策背景 “十三五”是我国储能产业化发展的起点。自“十四五”之后&#xff0c;各类储能支持政策更是以极快的速度不断更新完善。 2023年1月17日&#xff0c;工业和信息化部等六部门发布了《关于推动能源电子产业发展的指导意见》&#xff0c;其中明确提出要在2025年实现…

吴恩达prompt 笔记2:迭代提示开发(Iterative Prompt Develelopment)

1 前言 我们很难在初次尝试中就设计出最佳的提示&#xff0c;因此需要根据ChatGPT的反馈进行分析&#xff0c;分析输出具体在哪里不符合期望&#xff0c;然后不断思考和优化提示。如果有条件的话&#xff0c;最好是利用批量的样本来改善提示&#xff0c;这样可以对你的优化结…

代码随想录阅读笔记-哈希表【三数之和】

题目 给你一个包含 n 个整数的数组 nums&#xff0c;判断 nums 中是否存在三个元素 a&#xff0c;b&#xff0c;c &#xff0c;使得 a b c 0 &#xff1f;请你找出所有满足条件且不重复的三元组。 注意&#xff1a; 答案中不可以包含重复的三元组。 示例&#xff1a; 给定数…

Python之Web开发中级教程----搭建虚拟环境

Python之Web开发中级教程----搭建Web框架二 搭建虚拟环境 虚拟环境的作用 虚拟环境可以搭建独立的python运行环境, 使得单个项目的运行环境与其它项目互不影响. 搭建虚拟环境 &#xff08;1&#xff09;安装 sudo pip install virtualenv sudo pip install virtualenvwra…

一起学数据分析_2

写在前面&#xff1a;代码运行环境为jupyter&#xff0c;如果结果显示不出来的地方就加一个print()函数。 一、数据基本处理 缺失值处理&#xff1a; import numpy as np import pandas as pd#加载数据train.csv df pd.read_csv(train_chinese.csv) df.head()# 查看数据基本…

Vue3-响应式基础:单文件和组合式文件

单文件&#xff1a;html <!DOCTYPE html> <html> <head><title>响应式基础</title> </head> <body><div id"app" ><!-- dynamic parameter:同样在指令参数上也可以使用一个 JavaScript 表达式&#xff0c;需要包…

简易版 RPC 框架实现 1.0 -http实现

RPC 是“远程过程调用&#xff08;Remote Procedure Call&#xff09;”的缩写形式&#xff0c;比较通俗的解释是&#xff1a;像本地方法调用一样调用远程的服务。虽然 RPC 的定义非常简单&#xff0c;但是相对完整的、通用的 RPC 框架涉及很多方面的内容&#xff0c;例如注册发…

离散时间傅里叶变换和离散傅里叶变换

离散时间傅里叶变换和离散傅里叶变换 { X ( k ) DFT [ x ( n ) ] ∑ n 0 N − 1 x ( n ) W N n k k 0 , 1 , . . . , N − 1 x ( n ) IDFT [ X ( k ) ] 1 N ∑ n 0 N − 1 x ( n ) W N − n k n 0 , 1 , . . . , N − 1 \begin{cases} X(k)\textbf{DFT}[x(n)]\sum\limi…

解决:IDEA编译Java程序时报编译失败

1、问题展示&#xff1a; 2、解决方法&#xff1a;

Magical Combat VFX

这个包包含30个可供游戏使用的VFX,有各种口味,为您的游戏增添趣味! 所有VFX都经过了很好的优化,可以在所有平台上使用。 这个包特别有一堆闪电魔法,有两种主要的变体,一种是深色的,另一种是浅色的。但它也提供了一系列其他视觉效果,如神圣咒语、音乐主题等等! 我们提供…

【CSP】2021-09-2 非零段划分 索引+递推/差分+前缀和

2021-09-2 非零段划分 索引递推/差分前缀和 2021-09-2 非零段划分 索引递推/差分前缀和索引递推思路差分前缀和思路遇到的问题索引递推完整代码差分前缀和完整代码 2021-09-2 非零段划分 索引递推/差分前缀和 一开始写的时候没有发现直接从a数组求q的规律&#xff0c;怎么也想…

NCV8705MTADJTCG稳压器芯片中文资料规格书PDF数据手册引脚图图片价格功能

产品概述&#xff1a; NCV8705 是一款低噪音、低功耗和低泄漏线性电压稳压器。该器件具有卓越的噪音和 PSRR 规格&#xff0c;适用于使用视频接收器、成像传感器、音频处理器或需要外部洁净电源的任何部件的产品。NCV8705 使用创新的自适应接地电流电路 可确保轻负载调节下的超…

基于DFA敏感词查询的算法简析

基于DFA敏感词查询的算法简析 1.背景 项目中需要对敏感词做一个过滤&#xff0c;首先有几个方案可以选择&#xff1a; a.直接将敏感词组织成String后&#xff0c;利用indexOf方法来查询。 b.传统的敏感词入库后SQL查询。 c.利用Lucene建立分词索引来查询。 d.利用DFA算法…

3.python安装Selenium框架

1. 命令安装 pip install selenium下载慢,可以换源: pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/查看是否换源成功 pip config get global.index-url安装好后,查看版本信息: pip show selenium2.下载对应的浏览器驱动 https://registry.npmm…

【Elasticsearch】windows安装elasticsearch教程及遇到的坑

一、安装参考 1、安装参考&#xff1a;ES的安装使用(windows版) elasticsearch的下载地址&#xff1a;https://www.elastic.co/cn/downloads/elasticsearch ik分词器的下载地址&#xff1a;https://github.com/medcl/elasticsearch-analysis-ik/releases kibana可视化工具下载…

Vue2 引入使用ElementUI详解

目录 1 安装2 引入2.1 全局引入2.1.1 引入2.1.2 使用 2.2 按需引入2.2.1 引入2.2.2 使用 3 总结 1 安装 推荐使用 npm 的方式安装&#xff0c;它能更好地和 webpack打包工具配合使用。&#xff08;本项目使用安装方式&#xff09; npm i element-ui -S也可以使用其他的包管理…

Notepad++从文件夹查找文本内容

目录 一、背景二、Notepad搜索2.1 测试用例2.2 操作说明 一、背景 在日常的办公、学习或编程中&#xff0c;我们时长会遇到需要在大量文件中搜索特定文本内容的情况&#xff1a; 无论是快速定位某个项目中的代码片段&#xff1b;还是检索文档资料库中的相关信息等。 掌握如何…

蓝桥杯:模拟、枚举

目录 引言一、修剪灌木二、特殊年份三、刷题统计 引言 本篇文章主要介绍蓝桥杯的模拟和枚举的题目&#xff0c;这种题在 B B B 组还是比较简单的&#xff0c;后续也会一直往里加新的真题&#xff0c;加油&#xff01; 一、修剪灌木 标签&#xff1a;第十三届蓝桥杯省赛C B组…

音乐创作利器FL Studio21水果软件助你轻松实现音乐创意

音乐创作利器——FL Studio21水果软件&#xff0c;让你的音乐梦想起航&#xff01; 副标题&#xff1a;一款强大的电脑数码编曲软件&#xff0c;助你轻松实现音乐创意&#xff01; 一、FL Studio21水果软件——音乐制作的得力助手** 在音乐创作的道路上&#xff0c;有一款得心…