zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++日期类

2023-06-13 09:17:43 时间

日期类

构造函数与获取天数

这里我们不算公元前的日期。

首先是构造函数的创建: 在调用构造函数的时候我们可以设置全缺省参数。

	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

日期类有一个很重要的核心函数,你在加减天数的时候轻则月份会随之变化,重则年份也会随之变化,我们知道闰年和平年的2月不同,每个月和每个月的天数也不同,只有年和月是有规律的,所以可以写一个获取天数的函数,后续就可以用这个来判断你在某年某月加天数之后是否合法。 闰年和平年就差了一个二月份,所以可以将这个二月份进行判断。 所以通过数组更方便一些,只要返回对应的下标就可以了,下标为0的就忽略它。

	int GetMonthDay(int year, int month)
	{
		static int day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//因为这个函数被频繁调用的原因,每次都要创建数组,所以将它放到静态区
		if (month == 2 && (year % 100 != 0 && year % 4 == 0) || year % 400 == 0)//如果是闰年的二月就返回29天
		{
			return 29;
		}
		else
		{
			return day[month];//返回的正还是对应的天数
		}
	}

有了上面的这个函数检查构造时期的日期合法性就很简单了。

	Date(int year = 1, int month = 1, int day = 1)
	{
		if (year >= 1 &&//判断年份
			month <= 12 && month >= 1 &&//判断月份
			day >= 1 && day <= GetMonthDay(year, month))//如果年和月是合法的就判断天数
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			cout << "日期不合法" << endl;
		}
	}

加天数

首先要判断合法性,天数不能超过当月的天数,月不能超过12月,这里就不限制年份了。

	Date& operator+=(int day)
	{
		_day += day;//先把天数加上
		while (_day > GetMonthDay(_year, _month))//判断天数合法性
		{
			_day -= GetMonthDay(_year, _month);//不合法就减去当月的天数
			++_month;//减去当前月份天数就说明过去一个月了
			if (_month > 12)//判断月份合法性
			{
				_month = 1;//月份是不能等于0的,这个函数中的天数就不用担心
				++_year;//如果月份大于12个月年份就+1
			}
		}
		return *this;
	}

这里要注意的就是返回值,因为是+=,两个数相加是临时值,需要进行储存,+=就是两数相加并且进行储存,也就等于this指针指向的内容出函数之后不会被销毁,所以返回用引用。

如果你只想看50天之后的天数并且不影响本身的值,那么就不要+=了。 这里只需要拷贝一份,然后加到拷贝的上面,返回那个临时对象就可以了。

	Date operator+(int day)
	{
		Date s(*this);
		s += day;//这里会调用上面写的函数
		return s;
	}

减天数

这里和加天数差不多,只是到0天的时候就要减月份,到0月就要减年份(这里就不考虑年份小于等于0了)

	Date& operator-=(int day)
	{
		_day -= day;
		while (_day <= 0)
		{
			--_month;//这里要先减月,因为这个月如果天数是负数就要向上个月借天数
			if (_month == 0)//如果月份归零就说明要向去年的12月借天数了
			{
				_month = 12;
				--_year;
			}
			_day += GetMonthDay(_year, _month);//加上上个月天数
		}
		return *this;
	}

日期的比较

	bool 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;
		}
		return false;
	}

日期减日期

这里我们看相差多少天。 日期减日期的过程,年份和月份的问题要注意,处理起来很麻烦。 我们之前写过一个函数就是前置++,如果让小的日期不断+1,直到等于大的日期为止。 如果是大的日期减小的日期,那么减完是正数,如果是小日期减大日期就是负数。

	int operator-(const Date& d)//这里不是-=
	{
		Date max = *this;//这里算是拷贝,因为max正在创建的过程中
		Date min = d;
		int flag = 1;//这个用处是让最后的结果变化
		if (max < min)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int n = 0;//相差的天数
		while (min != max)
		{
			++min;
			++n;
		}
		return n * flag;//如果调用这个函数的时候是大日期减小日期返回的就是正数
						//如果是小日期减大日期就返回负数
	}

这里flag就是改变最后结果的一个变量。

打印和输入日期

平时用cin和cout输入输出的时候只能是内置类型。

Date s(2022, 10, 13);
cout << s;

cin和cout都是在头文件istream中定义的对象,cin是istream类,cout是osteram类的。 其实能自动识别内置类型是函数的重载。 这次我们来对cout和cin进行重载。 那么进行<<和>>重载的时候不会放入成员函数,因为在进行重载的时候,隐藏的this指针必定储存日期类的结构体,就会变成这个样子。

void operator<< (ostream& out)
{
	out << _year << "年" << _month << "月" << _day << "天" << endl;
}

调用的时候也只能是

s<<cout; 也等价于s.operator<<(cout);

这样不是我们预期的标准,所以只能将这个重载函数放在全局中,这样第一位置就可以是cout对的了。 顺便注意一下私有保护和链式调用。 这个函数是经常调用的函数,设置成内联函数比较好。

inline ostream& operator<<(ostream& out, const Date s)
{
	out << s._year << "年" << s._month << "月" << s._day << "日" << endl;
	return out;
}

在日期类进行了这个函数的友元,所以可以访问私有成员了。

日期类完整代码

我将部分成员函数的声明和定义分离,因为太长读起来不是很方便,并且还有一些新的成员函数。 因为类里面的成员函数默认是内联,所以就不用添加内联属性了。 date.h

#include <iostream>
using namespace std;
class Date
{
public:
	friend ostream& operator<<(ostream& out, const Date& s);
	friend istream& operator>>(istream& in, Date& s);
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		static int day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//平年的12个月的一天
		if (month == 2 && (year % 100 != 0 && year % 4 == 0) || year % 400 == 0)
		{
			return 29;
		}
		else
		{
			return day[month];
		}
	}
	// 全缺省的构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		if (year >= 1 &&//判断年份
			month <= 12 && month >= 1 &&//判断月份
			day >= 1 && day <= GetMonthDay(year, month))//判断天数
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			cout << "日期不合法" << endl;
		}
	}
	// 拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	// 赋值运算符重载
	Date& operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;//这里要注意支持连续赋值
	}
	// 析构函数
	~Date()
	{
		_year = 0;
		_month = 0;
		_day = 0;
	}
	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day)
	{
		Date s(*this);
		s += day;//这里会调用上面写的函数
		return s;
	}
	// 日期-=天数
	Date& operator-=(int day);
	// 日期-天数
	Date operator-(int day)
	{
		Date s(*this);
		s -= day;
		return s;
	}
	// 前置++
	Date& operator++()
	{
		return *this += 1;
	}
	// 后置++
	Date operator++(int)
	{
		Date s(*this);
		s += 1;
		return s;
	}
	// 后置--
	Date operator--(int)
	{
		Date s(*this);
		s -= 1;
		return s;
	}
	// 前置--
	Date& operator--()
	{
		return *this -= 1;
	}
	// >运算符重载
	bool operator>(const Date& d);
	// ==运算符重载
	bool operator==(const Date& d)
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}
	// >=运算符重载
	bool operator >= (const Date& d)
	{
		return *this > d || *this == d;//这里会直接调用上面的大于和等于函数
	}
	// <运算符重载
	bool operator < (const Date& d)
	{
		return !(*this >= d);
	}
	// <=运算符重载
	bool operator <= (const Date& d)
	{
		return !(*this > d);
	}
	// !=运算符重载
	bool operator != (const Date& d)
	{
		return !(*this == d);
	}
	// 日期-日期 返回天数
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};
inline ostream& operator<<(ostream& out, const Date& s)
{
	out << s._year << "年" << s._month << "月" << s._day << "日" << endl;
	return out;
}
inline istream& operator>>(istream& in, Date& s)//因为要改变s中成员变量,所以里面不能加const
{
	in >> s._year >> s._month >> s._day;
	return in;
}

Date.cpp

#include "date.h";
// 日期+=天数
Date& Date::operator+=(int day)
{
	if (day < 0)//如果是加了一个负天数
	{
		return *this -= abs(day);
	}
	_day += day;//先把天数加上
	while (_day > GetMonthDay(_year, _month))//判断天数合法性
	{
		_day -= GetMonthDay(_year, _month);//不合法就减去当月的天数
		++_month;//减去当前月份天数就说明过去一个月了
		if (_month > 12)//判断月份合法性
		{
			_month = 1;
			++_year;
		}
	}
	return *this;
}

// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)//如果是减了一个负天数
	{
		return *this += abs(day);
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}


// >运算符重载
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;
	}
	return false;
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)//这里不是-=
{
	Date max = *this;//这里算是拷贝,因为max正在创建的过程中
	Date min = d;
	int flag = 1;//这个用处是让最后的结果变化
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;//相差的天数
	while (min != max)
	{
		++min;
		++n;
	}
	return n * flag;//如果调用这个函数的时候是大日期减小日期返回的就是正数
	//如果是小日期减大日期就返回负数
}