C++_实现日期类

#include <iostream>

using namespace std;

class Date
{
public:
	friend int GetDaysofMonth(int year, int month);
	friend ostream& operator<<(ostream& _cout, const Date &d);
	Date(int year, int month, int day)
		: _year(year)
		, _month(month)
		, _day(day)
	{
		cout << "Date(int,int,int): "<<this<<endl;
	}

	Date(const Date& d)
		: _year(d._year)
		, _month(d._month)
		, _day(d._day)
	{
		//cout << "Date(const Date& ): " <<this<< endl;
	}

	// 赋值运算符重载 
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

		// 求当前日期第days天后是哪一天? 
	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;
	}

	// 求当前日期第days天前是哪一天? 
	Date operator-(int days)
	{
		if (days < 0)
			return (*this + (0 - days));
		Date tmp(*this);
		tmp._day -= days;
		while (tmp._day<0)
		{
			if (tmp._month>1){
				tmp._day += (GetDaysofMonth(tmp._year, tmp._month - 1));
			}
			else
			{
				tmp._day += (GetDaysofMonth(tmp._year-1, 12));
			}
			tmp._month -= 1;
			if (tmp._month <1)
			{
				tmp._year -= 1;
				tmp._month = 12;
			}
		}
		return tmp;
	}

	// 求两个日期之间的差值 
	int operator-(const Date& d)
	{
		Date fast(*this);
		Date slow(d);
		int value = 0;
		if (fast < slow)
		{
			fast = d;
			slow = *this;
		}
		while (fast != slow)
		{
			++slow;
			value++;
		}
		return value;
	}

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

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


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

		// 判断两个日期是否不等 
	bool operator!=(const Date& d)
	{
		if (*this == d)
			return false;
		else
			return true;
	}
	bool operator>(const Date& d)
	{
		Date tmp(*this);
		if (tmp._year > d._year)
			return true;
		else if (tmp._year < d._year)
			return false;
		else
		{
			if (tmp._month > d._month)
				return true;
			else if (tmp._month  < d._month)
				return false;
			else
			{
				if (tmp._day > d._day)
					return true;
				else if (tmp._day < d._day)
					return false;
				else
					return false;
			}
		}
	}
	bool operator>=(const Date& d)
	{
		Date tmp(*this);
		if (tmp._year > d._year)
			return true;
		else if (tmp._year < d._year)
			return false;
		else
		{
			if (tmp._month > d._month)
				return true;
			else if (tmp._month  < d._month)
				return false;
			else
			{
				if (tmp._day > d._day)
					return true;
				else if (tmp._day < d._day)
					return false;
				else
					return true;
			}
		}
	}
	bool operator<(const Date& d)
	{
		if (*this>=d)
			return false;
		else
			return true;
	}
	bool operator<=(const Date& d)
	{
		if (*this>d)
			return false;
		else
			return true;
	}


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

int GetDaysofMonth(int year, int month)
{
	int days[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	//是闰年
	if (month == 2){
		if (((year % 400) == 0 && (year % 100) == 0) || (year % 4 == 0 && year % 100 != 0))
			return days[1] -1;
	}
	return days[month - 1];
}

ostream& operator<<(ostream& _cout, const Date &d)
{
	_cout << d._year << "-" << d._month << "-" << d._day << endl;

	return _cout;
}



猜你喜欢

转载自blog.csdn.net/warrior_harlan/article/details/80602254