iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++逐步介绍日期类的使用
  • 910
分享到

C++逐步介绍日期类的使用

2024-04-02 19:04:59 910人浏览 独家记忆
摘要

目录头文件详细步骤第一步第二步总代码我们今天实现一个简单的计算日期 我们这里先给出头文件,后面一个一个解释 头文件 #pragma once #include<iOStrea

我们今天实现一个简单的计算日期

我们这里先给出头文件,后面一个一个解释

头文件

#pragma once
#include<iOStream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
	//定义构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	//打印日期
	void Print()
	{
		cout << _year <<" " <<_month<<" " << _day << endl;
	}
	//运算符重载
	Date operator+(int day);
	Date operator-(int day);
	Date& operator+=(int day);
	Date& operator-=(int day);
	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++();//前置
	Date operator++(int);//后置
	Date& operator--();//前置
	Date operator--(int);//后置
	int operator-(Date d);//计算两个日期的差值
private:
	int _year;
	int _month;
	int _day;
};

详细步骤

第一步

计算闰年和判断日期是否合法

inline int GetMonthDay(int year, int month)
{
	//多次调用的时候static能极大减小内存消耗,但是也可以删除,并不影响程序运行
	static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = _monthArray[month];
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//判断是否是闰年
	{
		day = 29;
	}
	return day;
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))//看日期是否合法
	{
		cout << "fail" << endl;
        exit(-1);
	}
}

我们将year,month,day赋值之后,首先进行判断,如果出现异常天数直接终结程序(如2022,1,32,这种不存在的日期)

随后一个问题就出来了,因为闰年和非闰年的2月不同,所以我们又使用一个函数getmonthday,来判断闰年的同时获取当月天数;

有些同学可能直接是【0,31,59(31+28 1月+2月),89(1月+2月+3月)......+.....365】

我们稍后再解释为什么要用我这种定义方式。

第二步

各类运算符重载

我们首先来看运算符”+”和”+=”

这是一般情况下的+

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp._day += day;
	//判断天数是否大于当月的天数
		while (tmp._day > GetMonthDay(tmp._year, tmp._month))
		{
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			tmp._month++;
			//判断月数是否大于12
			if (tmp._month > 12)
			{
		        tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;
}

这是一般情况下的+=

Date& Date::operator+=(int day)
{
	Date ret(*this);
		ret._day += day;
		//判断天数是否超越了当月的天数
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			//判断月数是否超过了12
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
	}
	return *this;
}

我们可以发现两者其实都用了同一个while循环来判断日期是否合法。

我们可以选择将while循环打包成一个新的函数,也可以选择直接复用

+复用+=

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;//直接调用operator的+=
		return tmp;
}

总代码

#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"
inline int GetMonthDay(int year, int month)
{
	static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = _monthArray[month];
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
		day = 29;
	}
	return day;
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))
	{
		cout << "!legal" << endl;
	}
}
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this -= -day;
	}
	else {
		_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)
{
	if (day < 0)
	{
		*this += -day;
	}
	else {
		_day -= day;
		while (_day <= 0)
		{
			if (_month == 1)
			{
				--_year;
				_month = 12;
				_day += GetMonthDay(_year, _month);
			}
			else
			{
				--_month;
				_day += GetMonthDay(_year, _month);
			}
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
		return tmp;
}
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
		return tmp;
}
Date& Date::operator++()//前置
{
	return 	*this += 1;;
}
Date  Date::operator++(int)//后置
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator--()//前置
{
	return *this -= 1;
}
Date  Date::operator--(int)//后置
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
		return true;
	else if (_year < d._year)
		return false;
	else//_year < d._year
	{
		if (_month > d._month)
			return true;
		else if (_month < d._month)
			return false;
		else//_month < d._month
		{
			if (_day > d._day)
				return true;
			else
				return false;
		}
	}
}
bool  Date::operator<(const Date& d)
{
	return !(*this > d || *this == d);
}
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)
{
	if (_year == d._year)
	{
		if (_month == d._month)
		{
			if (_day == d._day)
				return true;
		}
	}
	return false;
}
bool  Date::operator!=(const Date& d)
{
	//复用==
	return !(*this == d);
}
int Date::operator-(Date d)
{
	int count = 0;
	Date tmp(*this);
	if (tmp < d)
	{
		while (tmp != d)
		{
			++count;
			tmp += 1;
		}
	}
	else if (tmp > d)
	{
		while (tmp != d)
		{
			--count;
			tmp -= 1;
		}
	}
	return count;
}

到此这篇关于c++逐步介绍日期类的使用的文章就介绍到这了,更多相关C++日期类内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++逐步介绍日期类的使用

本文链接: https://www.lsjlt.com/news/153742.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++逐步介绍日期类的使用
    目录头文件详细步骤第一步第二步总代码我们今天实现一个简单的计算日期 我们这里先给出头文件,后面一个一个解释 头文件 #pragma once #include<iostrea...
    99+
    2022-11-13
  • JavaCalendar日历类的使用介绍
    目录创建一个Candendar对象Calendar的常用方法创建一个Candendar对象 我们都知道创建一个类的对象最简单的方法是从他的构造方法入手,我们看一下它的构造方法。 pr...
    99+
    2022-11-13
  • C++ plog日志使用方法介绍
    目录一、下载plog二、在VS中搭建plog编译环境三、使用plog日志库四、QTCreator使用plog日志库五、总结一、下载plog 下载链接:https://github.c...
    99+
    2022-11-13
  • C#抽象类的用法介绍
    假设有2个类,一个类是主力球员,一个类是替补球员。 public class NormalPlayer { public int ID { get; ...
    99+
    2022-11-13
  • PHP日期相关函数的介绍及用法
    这篇文章主要介绍“PHP日期相关函数的介绍及用法”,在日常操作中,相信很多人在PHP日期相关函数的介绍及用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PHP日期相关函数的介绍及用法”的疑惑有所帮助!接下来...
    99+
    2023-06-20
  • vue日期选择框之时间范围的使用介绍
    目录vue日期选择框之时间范围实现效果如下vue日期控件解析以上template视图层script 逻辑层vue日期选择框之时间范围 实现效果如下 <a-col :xl="...
    99+
    2022-11-13
  • C#中Parallel类For、ForEach和Invoke使用介绍
    一、简介: Parallel类提供了数据和任务的并行性;Paraller.For()方法类似于C#的for循环语句,也是多次执行一个任务。使用Paraller.For()方法,可以并...
    99+
    2022-11-13
  • C++第三方日志库Glog的安装与使用介绍
    目录一、glog介绍二、glog下载三、环境介绍三、glog的编译详解3.1 利用CMake进行编译,生成VS解决方案3.2 利用VS对项目进行编译四、glog的基本使用4.1 创建...
    99+
    2022-11-13
  • C++类中const修饰的成员函数及日期类怎么使用
    这篇文章主要介绍“C++类中const修饰的成员函数及日期类怎么使用”,在日常操作中,相信很多人在C++类中const修饰的成员函数及日期类怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++类中co...
    99+
    2023-07-05
  • 基于Android今日事今日毕的使用介绍
    今日事今日毕,这是高效工作的最重原则。没有什么能比从待办事项列表里划掉一些条目更让人觉得舒服的事了。做为一个高效的人,您需要一个优秀的待办事项管理工具,一个优秀的待办事项列表可...
    99+
    2022-06-06
    Android
  • C语言中static的使用介绍
    目录1.static 可以修饰局部变量2. static 可以修饰全局变量3.static 可以修饰函数总结1.static 可以修饰局部变量 首先让我看看这段代码 #inclu...
    99+
    2022-11-12
  • C++List链表的介绍和使用
    目录1. list的介绍及使用1.1 list的介绍1.2 list的使用1.2.1 list的构造1.2.2 list iterator的使用1.2.3 list capacity...
    99+
    2023-03-07
    C++ List链表 C++ List使用
  • C++堆栈的使用方法介绍
    本篇内容介绍了“C++堆栈的使用方法介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在开发这个大的领域,C++堆栈是一个不容忽视的概念,但...
    99+
    2023-06-17
  • BeanUtils工具类的介绍和使用
    BeanUtils是Apache Commons BeanUtils库中的一个工具类,用于简化JavaBean之间的属性复制。它提供...
    99+
    2023-09-21
    BeanUtils
  • 如何使用PHP将日期转化为时间戳?4种方法介绍
    PHP是一种服务器端编程语言,被广泛应用于Web开发中。时间戳是一种数字表示方式,它表示从1970年1月1日0时0分0秒(即UNIX时间戳的起始时间)到现在的秒数。在Web开发中,日期和时间戳之间的转化是经常会遇到的问题。本文将介绍如何使用...
    99+
    2023-05-14
  • C++11时间日期库chrono的使用
    目录时钟与时间点clock时间显示运行计时时间间隔durationduration模板duration_castratiochrono是C++11中新加入的时间日期操作库,可以方便地...
    99+
    2022-11-13
  • C++中对象与类的详解及其作用介绍
    目录什么是对象面向过程 vs 面向对象面向过程面向对象什么是类类的格式类的成员函数函数访问权限方法一方法二方法三inline 成员函数什么是对象 任何事物都是一个对象, 也就是传说中...
    99+
    2022-11-12
  • C++中cout的格式使用详细介绍
    1.cout和i/i++/++i的组合使用 i++ 和 ++i 是有着不同的含义,和 cout 组合使用也会得到不同的结果,下面给出一段代码: #include <iost...
    99+
    2022-11-12
  • C语言简明介绍指针的使用
    目录1. 指针类型2. 野指针3. 指针的运算3.1 指针+-整数3.2指针-指针3.3 指针的关系运算4. 指针数组1. 指针类型 指针以字节为单位; 指针类型决定了解引用时能访问...
    99+
    2022-11-13
  • C语言中数组的介绍及使用
    这篇文章主要讲解了“C语言中数组的介绍及使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C语言中数组的介绍及使用”吧!目录数组一维数组初始化使用总结:内存存储二维数组创建初始化数组越界问题...
    99+
    2023-06-20
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作