日期类基本功能实现

实现基本日期类型的加减操作
demo:通过给指定日期加减一个天数
计算两个日期之间相差的天数
通过数组给月份赋值
通过operator重载符号对内置类型符号重载
*注意operator赋值时候应该满足日期类的实际情况
month < 12 && February == 29 || 28
计算月份时候必须对闰年分开处理,因为闰年的Feburary是28天
加减天数时候需要考虑闰年的情况
*具体的代码实现

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{
		if( !(year >= 0 &&
			month > 0 && month < 13 &&
			day > 0 && day <= _GetDaysOfMonth(year, month)))
		{
			_year = 1900;
			_month = 1;
			_day = 1;
		}
	}

	Date operator+(int days)
	{
		if(days < 0)
			return *this - (0 - days);

		Date temp(*this);
		temp._day += days;

		int daysOfMonth;
		while(temp._day > (daysOfMonth = _GetDaysOfMonth(temp._year, temp._month)))
		{
			temp._day -= daysOfMonth;
			temp._month++;

			if(temp._month > 12)
			{
				temp._year += 1;
				temp._month = 1;
			}
		}

		return temp;
	}

	Date operator-(int days)
	{
		if(days < 0)
			return *this + (0 - days);

		Date temp(*this);
		temp._day -= days;
		while(temp._day <= 0)
		{
			temp._month--;
			if(0 == temp._month)
			{
				temp._year -= 1;
				temp._month = 12;
			}

			temp._day += _GetDaysOfMonth(temp._year, temp._month);
		}

		return temp;
	}

	int operator-(const Date& d)
	{
		Date minDate(*this);
		Date maxDate(d);

		if(maxDate < minDate)
		{
			minDate = d;
			maxDate = *this;
		}

		size_t count = 0;
		while(minDate < maxDate)
		{
			count++;
			++minDate;
		}

		return count;
	}

	bool IsLeap()
	{
		return _IsLeap(_year);
	}


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

	Date operator++(int)
	{
		Date temp(*this);
		++(*this);
		return temp;
	}

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

	Date operator--(int)
	{
		Date temp(*this);
		--(*this);
		return temp;
	}

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

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

	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
private:
	int _GetDaysOfMonth(int year, int month)
	{
		int days[] = {0, 31,28,31,30,31,30,31,31,30,31,30,31};
		if(2 == month && _IsLeap(year))
			days[2] += 1;

		return days[month];
	}

	bool _IsLeap(int year)
	{
		if((0 == year%4 && 0 != year % 100)||
			(0 == year % 400))
		{
			return true;
		}

		return false;
	}

	friend ostream& operator<<(ostream& _cout, const Date& d)
	{
		_cout<<d._year<<"/"<<d._month<<"/"<<d._day;
		return _cout;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2018, 6,24);
	Date d2(2019, 1, 1);
	cout<<d2 - d1<<endl;
	cout<<d1-99<<endl;
	cout<<d1-999<<endl;
	cout<<d1 + (-99)<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值