C++实现一个日期类,可以完成日期赋值,日期比较,基本的日期计算

实现一个日期类, 可以完成日期赋值, 日期比较, 日期+天数, 日期-天数, 日期-日期等计算

代码如下:
#include<iostream>
using namespace std;

class Date
{
public:
	// 获取某月的天数
	int GetMonthDay(int year, int month)
	{
		int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		// 如果是闰年的2月份,有29天
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			monthDays[2] = 29;
		}
		return monthDays[month];
	}

	// 构造函数
	Date(int year = 2000, int month = 1, int day = 1)
	{
		if (year > 0 && month > 0 && month <= 12
			&& day > 0 && 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)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

	// 析构函数
	~Date()
	{}

	// 日期+=天数
	Date& operator+=(int day)
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			++_month;
			if (_month == 13)
			{
				++_year;
				_month = 1;
			}
		}
		return *this;
	}

	// 日期+天数
	Date operator+(int day)
	{
		Date ret(*this);  // 拷贝构造一个ret
		ret += day;  // 复用上面的
		return ret;
	}

	// 日期-天数
	Date operator-(int day)
	{
		Date ret(*this);
		ret -= day;
		return ret;
	}

	// 日期-=天数
	Date& operator-=(int day)
	{
		if (day < 0)
		{
			return *this += -day;
		}

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

	// 前置++
	Date& operator++()
	{
		*this += 1;
		return *this;  // 返回++之后的值
	}

	// 后置++
	Date operator++(int) // (int)是为了构成函数重载
	{
		Date tmp(*this);
		*this += 1;
		return tmp;  // 返回++之前的值
	}

	// 后置--
	Date operator--(int)
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}

	// 前置--
	Date& operator--()
	{
		*this -= 1;
		return *this;
	}

	// >运算符重载
	inline bool operator>(const Date& d)
	{
		if (_year > d._year   // 比较年
			|| _year == d._year && _month > d._month  // 年相等,比较月份
			|| _year == d._year && _month == d._month && _day > d._day)  // 年月相等,比较天
		{
			return true;
		}
		return false;
	}

	// ==运算符重载
	inline 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)
	{
		Date max = *this;  
		Date min = d;
		int flag = 1;  // 默认前一个日期比后一个日期大

		if (*this < d)  // 如果实际是后一个日期大于前一个,则交换它们,flag改变符号
		{
			max = d;
			min = *this;
			flag = -1;
		}

		int days = 0;  // 两个日期相差的天数
		while (min != max)
		{
			++min;
			++days;
		}
		return days * flag;
	}

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	cout << "无参构造:" << endl;
	Date d1;  // 无参构造,调用全缺省构造函数
	d1.Print();

	cout << "传参构造:" << endl;
	Date d2(2020, 5, 17);  
	d2.Print();
	
	cout << "拷贝构造:" << endl;
	Date d3(d2);
	d3.Print();

	cout << "日期赋值:" << endl;
	d3 = d1;
	d3.Print();

	cout << "日期+天数(原本的日期不会改变):" << endl;
	Date d4 = d2 + 10;
	Date d5 = d2 + 100;
	Date d6 = d2 + 1000;
	d4.Print();
	d5.Print();
	d6.Print();

	cout << "日期+=天数(原本的日期会改变):" << endl;
	Date d7 = d2 += 10;
	Date d8 = d2 += 100;
	Date d9 = d2 += 1000;
	d7.Print();
	d8.Print();
	d9.Print();

	cout << "日期-=天数(原本的日期会改变):" << endl;
	Date d10 = d2 -= 10;
	Date d11 = d2 -= 100;
	Date d12 = d2 -= 1000;
	d10.Print();
	d11.Print();
	d12.Print();

	cout << "日期-天数(原本的日期不会改变):" << endl;
	Date d13 = d2 - 10;
	Date d14 = d2 - 100;
	Date d15 = d2 - 1000;
	d13.Print();
	d14.Print();
	d15.Print();

	cout << "日期-日期:" << endl;
	int ret1 = d2 - d1;
	int ret2 = d1 - d2;
	cout << ret1 << endl;
	cout << ret2 << endl;
	return 0;
}
运行结果:

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值