[c++]——实现日期计算器

日期类:

class Date
{
public:
	int Getday(int year, int month, int day)
	{
		if (month == 0)
		{
			return 31;
		}
		int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (month == 2 && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
		{
			return 29;
		}
		else
		{
			return arr[month];
		}
	}

	Date(int year = 2019, int month = 1, int day = 1)
	{
		if (year > 0 && month > 0 && month<13 && day>0 && day <= Getday(year, month, day))
		{
			this->_year = year;
			this->_month = month;
			this->_day = day;
		}
		else
		{
			cout << "日期输入有误" << endl;
		}
	}

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

	bool operator!=(const Date& b)
	{
		return !(*this == b);
	}

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

	bool operator>(const Date& b)
	{
		return !((*this == b) || (*this < b));
	}		

	bool operator>=(const Date& b)
	{
		return !(*this < b);
	}

	bool operator<=(const Date& b)
	{
		return !(*this > b);
	}

	Date& operator+=(int day)
	{
		if (day < 0)
		{
			return *this -= (-day);
		}
		_day = _day + day;
		while (_day > Getday(_year,_month,_day))
		{			
			_day -= Getday(_year, _month, _day);
			_month += 1;
			if (_month == 13)
			{
				_year += 1;
				_month = 1;
			}
		}
		return *this;
	}

	Date& operator-=(int day)
	{
		if (day < 0)
		{
			return *this += (-day);
		}
		_day = _day - day;
		while (_day < 1)
		{
			_month -= 1;
			if (_month == 0)
			{
				_month = 12;
				_year -= 1;
			}
			_day += Getday(_year, _month, _day);
		}
		return *this;
	}

	Date operator-(int day)
	{
		Date ret = *this;
		return ret -= day;
	}

	Date operator+(int day)
	{
		Date ret = *this;
		return ret += day;
	}

	Date& operator++()
	{
		return *this += 1;
	}

	Date operator++(int a)
	{
		Date ret(*this);
		*this += 1;
		return ret;
	}

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

	Date operator--(int a)
	{
		Date ret(*this);
		*this -= 1;
		return ret;
	}

	Date& operator=(const Date& date)
	{
		if (this != &date)
		{
			this->_year = date._year;
			this->_month = date._month;
			this->_day = date._day;
		}
		return *this;
	}	

	int operator-(const Date& b)
	{	
		int flag = 1;
		int day = 0;
		Date max = *this;
		Date min = b;
		if (*this < b)
		{
			max = b;
			min = *this;
			flag = -1;
		}
		while (min != max)
		{
			++min;
			++day;
		}
		return day*flag;
	}

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

private:
	int _year;
	int _month;
	int _day;
};
(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、付费专栏及课程。

余额充值