C++ 日期计算器

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

class Date
{
public:
	Date(int year = 1900,int month = 1, int day = 1)
		:_year(year)
		,_month(month)
		,_day(day)
	{}

	//拷贝构造系统默认生成

	Date operator +( int day)//日期 +
	{
		if(day < 0)
			return *this -(-day);

		Date tmp(*this);
		tmp._day += day;

		while(Is_Invalid(tmp))
		{
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			tmp._month++;
			if(tmp._month == 13)
			{
				tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;
	}

	Date& operator +=(int day)//日期 +=
	{
		*this = *this + day;
		return *this;
	}

	Date operator -( int day)//日期 -
	{
		if(day < 0)
			return *this +(-day);

		Date tmp(*this);
		tmp._day -= day;

		while(Is_Invalid(tmp))
		{
			tmp._month--;

			if(tmp._month == 0)
			{
				tmp._year--;
				tmp._month = 12;
			}

			tmp._day += GetMonthDay(tmp._year, tmp._month);
		}
		return tmp;
	}

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

	bool operator ==(const Date& d)//判断日期相等
	{
		return (_day == d._day)\
			&&(_month == d._month)\
			&&(_year == d._year);
	}

	bool operator !=(const Date& d)//判断日期不等
	{
		return (_day != d._day)\
			||(_month != d._month)\
			||(_year != d._year);
	}

	int operator -(const Date& d)//日期相减相差的天数
	{
		Date min = *this;
		Date max = d;

		if(min._year > max._year)//用较小日期逼近较大日期
		{
			min = d;
			max = *this;
		}
		else if(min._year == max._year)
		{
			if(min._month > max._month)
			{
				min = d;
				max = *this;
			}
			else if(min._month == max._month)
			{
				if(min._day > max._year)
				{
					min = d;
					max = *this;
				}
			}
		}

		int count = 0;
		while(min != max)
		{
			min = min+1;
			count++;
		}
		return count;

		//Date tmp1(1900, 1, 1);//一某年为基准,求两个日期和它的相差的天数再求差
		//int num1 = 0;
		//while(min != tmp1)
		//{
		//	tmp1 = tmp1 + 1;
		//	num1++;
		//}
		//Date tmp2(1900, 1, 1);
		//int num2 = 0;
		//while(max != tmp2)
		//{
		//	tmp2 = tmp2 + 1;
		//	num2++;
		//}
		//int count = num1 - num2;
		//return count;
	}

	bool Is_Bissextile(int year)//判断闰年
	{
		if(((year%4 == 0)&&(year%100 != 0)) || (year%400 == 0))
			return true;
		else
			return false;
	}

	int GetMonthDay(int year, int month)//获得某年某月的天数
	{
		int Day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		if(Is_Bissextile(year))
		{
			if(month == 2)
			{
				return Day[month] + 1;
			}
			else
				return Day[month];
		}
		else
			return Day[month];
	}

	bool Is_Invalid(const Date &d)//判断日期是否无效
	{
		int get_day = GetMonthDay(d._year, d._month);

		if((d._day > get_day) || (d._day < 1)\
			||(d._month > 12) || (d._month < 1))
			return true;
		else
			return false;
	}

	void Display()
	{
		cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
	}

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

void test()//测试用例
{
	Date d1(2017, 7, 1);

	Date d2 = d1 - 100;
	d2.Display();

	Date d3 = d1 - (-100);
	d3.Display();

	Date d4 = d1 + 100;
	d4.Display();

	Date d5 = d1 + (-100);
	d5.Display();

	Date d6 = d1 += 100;
	d6.Display();

	Date d7 = d1 -= 100;
	d7.Display();

	Date d8(2017, 7, 1);
	Date d9(2017, 9, 10);
	Date d10(2017, 7, 1);

	cout<<(d8==d9)<<endl;
	cout<<(d8!=d9)<<endl;
	cout<<"两个日期相差:"<<(d8-d9)<<endl;
	cout<<"两个日期相差:"<<(d9-d10)<<endl;
}

int main()
{
	test();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值