日期类

本文介绍了一个日期类的设计与实现,包括日期的有效性检查、日期之间的比较运算、日期加减天数的功能以及计算两个日期间相差的天数。通过具体实例展示了如何使用这个日期类进行日期的比较和加减运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进行日期的比较、加减天数及两个日期的时间差等运算。

代码如下:

#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:
	void Display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
public:
	Date(int year, int month, int day)
	{//应判断日期是否有效
		//cout << "Date(int year, int month, int day)" << endl;
		if (year >= 2000 && month > 0 && month<13 && day>0 && day <= GetMonthDay(year, month))
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			cout << "日期非法!" << endl;
			assert(false);
		}
	}
	Date (const Date& d)
	{
		//cout << "Date (const Date& d)" << endl;
		this->_year = d._year;
		this->_month = d._month;
		this->_day = d._day;
	}
	~Date()
	{
		//cout << "~Date()" << endl;
	}
public://两个日期相比较
	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);
public://日期计算器
	Date operator +(int day);
	Date& operator +=(int day);
	Date operator -(int day);
	Date& operator -=(int day);
	Date& operator++();//前置++
	Date operator++(int);//后置++
	Date& operator--();//前置--
	Date operator--(int);//后置--
	int GetMonthDay(int year, int month);
	int operator -(const Date& d);//两个日期间相差的天数
private:
	int _year;
	int _month;
	int _day;
};

bool Date::operator == (const Date& d)
{
	return this->_year == d._year&&this->_month == d._month&&this->_day == d._day;
}
bool Date::operator < (const Date& d)
{
	return this->_year < d._year ||
		(this->_year == d._year&&this->_month < d._month) ||
		(this->_year == d._year&&this->_month == d._month&&this->_day < d._day);
}
bool Date::operator <= (const Date& d)
{//复用代码
	return *this<d || *this == d;
}
bool Date::operator > (const Date& d)
{
	return this->_year > d._year ||
		(this->_year == d._year&&this->_month > d._month) ||
		(this->_year == d._year&&this->_month == d._month&&this->_day > d._day);
}
bool Date::operator >= (const Date& d)
{//复用代码
	return *this>d || *this == d;
}

//日期计算器
int Date::GetMonthDay(const int year, const int month)//获得具体某年某月的总天数
{
	int day;
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
		day = 31;
	else if (month == 2)
	{
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
			day = 29;
		else
			day = 28;
	}
	else
		day = 30;
	return day;
}

Date Date::operator +(int day)//日期加天数
{
	if (day < 0)
		return *this - (-day);
	Date tmp = *this;
	tmp._day += day;
	while (tmp._day >(GetMonthDay(tmp._year, tmp._month)))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		if (tmp._month == 12)
		{
			tmp._year += 1;
			tmp._month = 1;
		}
		else
			tmp._month += 1;
	}
	return tmp;
}
Date& Date::operator +=(const int day)
{
	return *this = *this + day;
}
Date Date::operator -(const int day)//日期减天数
{
	if (day < 0)
		return *this + (-day);
	Date tmp = *this;
	tmp._day -= day;
	while (tmp._day <= 0)
	{
		if (tmp._month == 1)
		{
			--tmp._year;
			tmp._month = 12;
		}
		else
			tmp._month--;
		tmp._day += GetMonthDay(tmp._year, tmp._month);
	}
	return tmp;
}
Date& Date::operator -=(const int day)
{
	return *this = *this - day;
}
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)//两个日期间相差的天数
{//通过较小的日期不断自加,直到等于较大的日期,统计出自加次数
	int flag = 1;
	int days = 0;
	Date max = *this;
	Date min = d;
	if (max < min)
	{//调用C++库函数中的swap(std::swap())
		std::swap(max._year, min._year);
		std::swap(max._month, min._month);
		std::swap(max._day, min._day);
		flag = -1;
	}
	while(!(min == max))
	{
		min++;
		days++;
	}
	return flag*days;
}
void Test1()
{//比较日期大小
	Date p1(2016, 1, 20);
	Date p2(2016, 1, 25);
	p1.Display();
	p2.Display();
	bool ret;
	ret = p1 == p2;
	cout << "---------ret = " << ret << endl;
	ret = p1 > p2;
	cout << "---------ret = " << ret << endl;
	ret = p1 < p2;
	cout << "---------ret = " << ret << endl;
}

void Test2()
{//日期加减天数
	Date p1(2016, 2, 20);
	Date p2(2016, 1, 25);
	p1.Display();
	p1 = p1 + 41;
	p1.Display();
	cout << endl;
	++p1;
	p1.Display();
	p1++;
	p1.Display();
	cout << endl;
	p2.Display();
	p2 = p2 - 56;
	p2.Display();
	cout << endl;
	--p2;
	p2.Display();
	p2--;
	p2.Display();
}

void Test3()
{//两个日期间相差的天数
	Date p1(2016, 2, 20);
	Date p2(2016, 1, 25);
	int days = p1 - p2;
	cout << days << endl;
}

int main()
{
	Test1();
	Test2();
	Test3();
	system("pause");
	return 0;
}


本文出自 “Scen” 博客,请务必保留此出处http://10741357.blog.51cto.com/10731357/1737263

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值