【C++】实现日期类


前言

今天呢我们来了解一下用类来实现日期的计算,用来加强一下我们之前学的类那一块的知识点。
在这里插入图片描述


一、日期类的全部函数展示

这里由与函数比较多,所以要创建三个文件,一个头文件.h用来声明函数,两个源文件.cpp用来定义函数和测试函数。我这里先展示头文件.h看看要完成的函数有多少。

#pragma once
#include<iostream>
#include<stdbool.h>
#include<windows.h>
using namespace std;
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);//友元声明
	friend istream& operator>>(istream& in, Date& d);//友元声明
public:
	Date(int year=2025, int month=1, int day=7);//默认构造函数
	void Print();
	int GetMonthDay(int year, int month)//获取每月多少天
	{
		static int GetMonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2&&((year % 100 != 0 && year % 400 == 0) || year % 4 == 0))
		{
			return 29;
		}
		return GetMonthDay[month];
	}
	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++(int);//后置++
	Date& operator++();//前置++
	int operator-(const Date& d); //日期-日期
	bool CheckDate();
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

由于这里的默认构造函数和打印函数比较简单所以我这里就直接省略了哦

1.日期+=整数

思路:
先把整数加到天数上面。判断总数是否超过当月的天数,如果不超过那就直接返回,
如果超过了当月的天数,那就让总数减去下个月的天数同时月份加1,如果月份满12月了,那就把月份归为1,让年份+1。依次类推,直到不超过当前月份的天数为止。
注意:
这里传的参数不仅可以是正整数,还可以是负整数。正整数就按照上面的逻辑。如果是负整数的话,那这里就是-=一个整数了。异号为负嘛。所以这里还要先判断一下传的参数是负数还是整数.

Date& Date::operator+=(int day)//这里用传引用返回从整体来看,使用引用的方式是为了优化性能、允许链式调用、直接修改对象以及保持代码的清晰和一致性
{
     //*this=*this+day; +的复用
	if (day < 0)
	{
		return *this -= (-day);
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}

2.日期+整数

思路:
这里跟上面日期+整数是一个思路,只是这里不需要拷贝一份,它是想直接改变日期。

注意:
传的参数是正整数还是负整数。如果是负整数的那就需要判断一下

Date Date::operator+(int day)  //设计成返回一个新对象而不是引用是为了符合 + 运算符的语义
{
	Date tmp = *this;  
	//tmp += day;//这里还可以使用+=的复用 ,这里比较推荐 
	if (day < 0) 
	{
		return *this -(-day);  
	} 
	tmp._day += day;
	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		++tmp._month;
		if (tmp._month == 13) 
		{ 
			++tmp._year;
			tmp._month = 1;
		}
	}
	return tmp;
}

注意这里一定要先拷贝一份,不然它就把原来的日期都改变了。就变成了+=一个整数。但是如果复用+=的话就要使得代码变得简单起来。

Date Date::operator+(int day)  //设计成返回一个新对象而不是引用是为了符合 + 运算符的语义
{
	Date tmp = *this;  
	tmp += day;//这里还可以使用+=的复用 ,这里比较推荐 
	return tmp;
}

3.日期-=整数

思路:
先把日期减去整数,如果大于0,那就直接返回,如果小于等于0,首先月份减1,如果月份小于1,那就把月份归为12,年份减1。最后再让总数+=上个月份的天数
注意:
这里为什么是上个月的天数呢?因为这个月的天数已经见过了呀。判断参数的正负

Date& Date::operator-=(int day)
{
	//*this = *this - day;//-的复用
	if (day < 0)
	{
		return *this += (-day);
	}
	_day -= day; 
	while (_day <= 0) 
	{
		--_month; 
		if (_month == 0) 
		{
			_month = 12;
			--_year; 
		}
		_day+= GetMonthDay(_year, _month); 
	}
	return *this;
}

4.日期-整数

思路:
与上面日期+=正数的思路一样。不同的是这里需要拷贝一份,怕改变原来的日期。

Date Date::operator-(int day)      
{
	//Data tmp = *this;//复用-=
	//tmp -= day;
	//return*this;

	if (day < 0)
	{
		return *this + (-day);
	}
	Date tmp = *this;
	tmp._day -= day;
	while (tmp._day <= 0)
	{
		--tmp._month;
		if (tmp._month == 0)
		{
			tmp._month = 12;
			--tmp._year;
		}
		tmp._day+= GetMonthDay(tmp._year, tmp._month); 
	}
	return tmp;
}

5.日期1<日期2

思路:
当日期1的年份小于日期的年份返回true,如果年份相同就比较月份,如果月份也相同,那就比较天数。最后都不成立,那就返回false。

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

6.日期1==日期2

思路:
分别判断日期1和日期2的年份,月份和天数是否想等,当这三个条件同时成立时那就返回true,反之返回false

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

7,其他日期比较

思路 :由于我们上面写出小于和判断时候相等的函数,那我们直接可以复用他们两个是不是就可以解决剩下来的函数:比如大于,那就直接小于取反就行了,具体看代码:

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);
}

8.前置++/- -,后置++/- -

前置++/- -:先写后读,这里直接改变日期对象,所以这里可以复用+=。即this+=1。
后置++/- -:先读后写,这里要返回改变之前的值,所以这里要拷贝一份
this。为 区分前置和后置,后置一般都回接收一个参数,用这个参数来判断是前置还是后置

Date Date::operator++(int)//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;
}
Date& Date::operator++()
{
	*this -= 1;
	return *this;
}

9.日期-日期

思路:
用较小的日期一直加1,直到较小日期与大日期相等,然后每次加1的时候用一个变量记录天数。

难点:
怎样确定较小 日期成了这里的难题。

解决方法:

假设法:我直接假设传的第一个参数为较大天数,如果为真,那就那逻辑计算,计算的结果是过去到现在的天数,设为正整数,如果为假,那就交换一下变量,然后再执行,计算的结果是现在到未来的天数,设为负整数。为了控制正负号,我们用一个falg这个变量来控制它。

int Date::operator-(const Date& d)
{
	int falg = 1;//控制正负号
	Date max = *this;
	Date min = d;
	if (*this<d)//采用了假设法
	{
		max = d;
	    min = *this;
		falg = -1;//计算未来天数,所以要变号
	}
	int n = 0;
	while (min!= max) 
	{
		++min;
		++n;
	}
	return n * falg;
}

10.判断日期的格式是否正确

思路:
这里主要是判断月份的天数和日的天数是否正确。

bool Date::CheckDate()
{
	if (_month < 1 || _month > 12|| _day < 1 || _day > GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}

11.日期类的重载流提取(<<)和流插入(>>)

注意:因为类中的成员函数的第一个参数是this指针,为了防止这个this指针的影响,我们就把它放在类外吧。但是,如果我们在类外实现函数,当我们要访问类里面的成员呀?那该怎么办呢?这里需要在类里面友元声明一下
在这里插入图片描述

ostream&operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
istream& operator>>(istream& in, Date & d)
{
	cout << "请依次输入:" << endl;
	in >> d._year >> d._month >> d._day; 
	if (!d.CheckDate())
	{
		cout << "⽇期⾮法" << endl;
	}
	return in;
}

二、日期类的代码展现

Date.文件

#include<windows.h>
#include<stdio.h>
#include<stdbool.h>
#include<iostream>
using namespace std; 
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year = 2024, int month = 11, int day = 28);
	void Print();
	int GetMonthDay(int year, int month)
	{
		static int GetMonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2&&((year%100!=0&&year%400==0)||year%4==0))
		{
			return 29;
		}
		return GetMonthDay[month];
	}
	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++(int);
Date& operator++();
int operator-(const Date& d);
bool CheckDate();
private:
	int _year;
	int _month;
	int _day;

};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d); 

2.Date.cpp文件

#include"Data.h"
Date::Date(int year, int month , int day)
{
	_year = year; 
	_month = month;
	_day = day;
}
void Date::Print()
{
	cout << _year << "_" << _month << "_" << _day << endl;
}
	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 == 13)
			{
				++_year;
				_month = 1;
			}
		}
		return *this;
	}
Date Date::operator+(int day)  
{
	Date tmp = *this;  
	//tmp += day;//+=的复用 
	if (day < 0) 
	{
		return *this -(-day);  
	} 
	tmp._day += day;
	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		++tmp._month;
		if (tmp._month == 13) 
		{ 
			++tmp._year;
			tmp._month = 1;
		}
	}
	return tmp;
}
Date& Date::operator-=(int day)
{
	//*this = *this - day;
	if (day < 0)
	{
		return *this += (-day);
	}
	_day -= day; 
	while (_day <= 0) 
	{
		--_month; 
		if (_month == 0) 
		{
			_month = 12;
			--_year; 
		}
		_day+= GetMonthDay(_year, _month); 
	}
	return *this;
}
Date Date::operator-(int day)      
{
	//Data tmp = *this;//采复用-=
	//tmp -= day;

	if (day < 0)
	{
		return *this + (-day);
	}
	Date tmp = *this;
	tmp._day -= day;
	while (tmp._day <= 0)
	{
		--tmp._month;
		if (tmp._month == 0)
		{
			tmp._month = 12;
			--tmp._year;
		}
		tmp._day+= GetMonthDay(tmp._year, tmp._month); 
	}
	return tmp;
}
bool Date::operator<(const Date& d)
{
	if (_year < d._year)
	{ 
		return true;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			return _day < d._day;
		}
	}
	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)//int只是用来区分时前置还是后置
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
int Date::operator-(const Date& d)
{
	int falg = 1;
	Date max = *this;
	Date min = d;
	if (*this<d)
	{
		max = d;
	    min = *this;
		falg = -1;
	}
	int n = 0;
	while (min!= max) 
	{
		++min;
		++n;
	}
	return n * falg;
}
ostream&operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
istream& operator>>(istream& in, Date & d)
{
	cout << "请依次输入:" << endl;
	in >> d._year >> d._month >> d._day; 
	if (!d.CheckDate())
	{
		cout << "⽇期⾮法" << endl;
	}
	return in;
}
bool Date::CheckDate()
{
	if (_month < 1 || _month > 12|| _day < 1 || _day > GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}

3.test.cpp文件

#include"Data.h"
#include<stdio.h>

int main()
{
	Date d1(2025, 4, 9);
	Date d2(2025, 7, 9);
	cout << d1 - d2 << endl; 
	//这里小编就简单的测试一下,感兴趣的自己下去测试一下吧
	
	return 0;

}

总结

今天的分享就到这里啦。欢迎大家的讨论!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值