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

 更新时间:2022年07月07日 11:01:06   作者:幻荼  
下面小编就为大家带来一篇C++实现日期类(Date类)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

头文件

#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++日期类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • cocos2dx-3.10 C++实现滚动数字

    cocos2dx-3.10 C++实现滚动数字

    这篇文章主要为大家详细介绍了cocos2dx-3.10 C++实现滚动数字效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • OpenCV利用对比度亮度变换实现水印去除

    OpenCV利用对比度亮度变换实现水印去除

    OpenCV中去除水印最常用的方法是inpaint,通过图像修复的方法来去除水印。本文将介绍另一种方法:利用对比度亮度变换去除水印,需要的朋友可以参考一下
    2021-11-11
  • C# 使用反射来实现对象的深度复制方法

    C# 使用反射来实现对象的深度复制方法

    下面小编就为大家带来一篇C# 使用反射来实现对象的深度复制方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • C++通过COM接口操作PPT

    C++通过COM接口操作PPT

    这篇文章主要为大家详细介绍了C++通过COM接口操作PPT的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • C++ throw关键字实现抛出异常和异常规范

    C++ throw关键字实现抛出异常和异常规范

    本文主要介绍了C++ throw关键字实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • c++ 预处理的图灵完备之引言

    c++ 预处理的图灵完备之引言

    这篇文章主要介绍了c++ 预处理的图灵完备之引言,需要的朋友可以参考下
    2017-07-07
  • C C++ LeetCode题解在二叉树中增加一行示例详解

    C C++ LeetCode题解在二叉树中增加一行示例详解

    这篇文章主要为大家介绍了C C++ LeetCode题解在二叉树中增加一行示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • VC中Tab control控件的用法详细解析

    VC中Tab control控件的用法详细解析

    以下是对VC中Tab control控件的用法进行了详细的介绍,需要的朋友可以过来参考下哦
    2013-09-09
  • C语言进阶教程之预处理

    C语言进阶教程之预处理

    C语言提供了多种预处理功能,如宏定义、文件包含、条件编译等,下面这篇文章主要给大家介绍了关于C语言进阶教程之预处理的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-02-02
  • C语言中联合体与共用体和枚举使用语法示例

    C语言中联合体与共用体和枚举使用语法示例

    这篇文章主要介绍了C语言中联合体与共用体和枚举使用语法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-12-12

最新评论