C++实现date类

 

 

# include<iostream>
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
using namespace std;
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)))//非法的日期,重新调整到1900/1/1
		{
			_year = 1900;
			_month = 1;
			_day = 1;
		}
	}
	//看看加上具体的天数之后是未来的什么日子
	Date operator+(int days)
	{
		if (days < 0)
			return *this - (0 - days);//防止加的是一个负数
		Date tmp(*this);
		tmp._day += days;
		//若是日期的天数超过本月的天数则是非法的,处理过程:
		int dayofmonth;
		//临时变量里的天数大于本月的天数,则是非法的
		while (tmp._day > (dayofmonth = _GetDaysOfMonth(tmp._year, tmp._month)))
		{
			tmp._day -= dayofmonth;//天数减,可能会去到下一个月
			tmp._month++;
			if (tmp._month > 12)
			{
				tmp._year += 1;
				tmp._month = 1;
			}
		}
		return tmp;
	}
	Date operator-(int days)
	{
		if (days < 0)
			return *this + (0 - days);//减一个负数相当于加一个正数
		Date tmp(*this);
		tmp._day -= days;
		while (tmp._day<=0)//非法,月份减小,天数加
		{
			tmp._month--;
			if (0 == tmp._month)//month等于0,减到上一年的12月
			{
				tmp._year -= 1;
				tmp._month = 12;
			}
			tmp._day += _GetDaysOfMonth(tmp._year, tmp._month);
		}
		return *this;
	}
	//两个日期之间差了多少天
	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 tmp(*this);
		++(*this);
		return tmp;
	}
	Date &operator--()
	{
		*this = *this - 1;
		return *this;
	}
	Date &operator--(int)
	{
		Date tmp(*this);
		--(*this);
		return tmp;
	}
	//内置类型才能比较大小
	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, 5, 124);
	Date d2(1223, 1, 2);
	cout << (d2 - d1 )<< endl;
	cout << d1 + 99 << endl;
	cout << d1 + (-99 )<< endl;
	system("pause");
	return 0;
}

 

 

 

posted on 2018-07-28 02:17  众里寻他2018  阅读(71)  评论(0)    收藏  举报

导航