【C++】万年历(时间计数器)

#include
using namespace std;

class Date
{
public:
	Date(int year = 1900,int month = 1,int day = 1)		//构造函数
		:_year(year)
		,_month(month)
	    ,_day(day)
	{
		//初始化列表

		//检查输入的日期是否合法
		if( year < 1900
			|| month < 1 || month > 12
			|| day < 1 || day > GetMonthDay(year,month))
		{
			cout<<"IlegalDate"<_year = d._year;
			this->_month = d._month;
			this->_day = d._day;
		}

		return *this;
	}

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

	static int GetMonthDay(int year,int month)
	{
		static int array[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
		if(IsLeapYear(year))
		{
			array[2] = 29;
		}
		else
		{
			array[2] = 28;
		}
		return array[month];
	}

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

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

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

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

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

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

	//计算一个日期加减天数以后的日期
	Date operator + (int day)
	{
		Date temp(*this);
		
		if(day < 0)
		{
			day = -day;
			return *this - day;
		}

		temp._day += day;
		while(temp._day > GetMonthDay(temp._year,temp._month))
		{
			temp._day -= GetMonthDay(temp._year,temp._month);
			if(temp._month == 12)
			{
				temp._year ++;
				temp._month = 1;
			}
			else
			{
				temp._month ++;
			}
		}
		return temp;
	}

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

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

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

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

	Date operator++(int)	//后置++
	{
		Date temp(*this);
		*this +=  1;

		return temp;
	}

	const Date& operator--()		//前置--
	{
		*this -= 1;

		return *this;
	}

	Date operator--(int)			//后置--
	{
		Date temp(*this);
		*this -= 1;

		return temp;
	}

	//计算两个日期相减所差的天数
	int operator-(const Date& d)
	{
		Date x1 = *this,x2 = d;

		if(x1 > x2)
		{
			x1 = d;
			x2 = *this;
		}
		
		int day = 0;
		while(x1 != x2)
		{
			x1++;
			++day;
		}

		return day;
	}

	friend istream& operator>>(istream& is,Date& d);
	friend ostream& operator<<(ostream& os,const Date& d);

	void DisPlay()
	{
		cout<<_year<<"_"<<_month<<"_"<<_day<>(istream& is,Date& d)
{
	cout<<"请依次输入年月日: "<>d._year;
	is>>d._month;
	is>>d._day;

	return is;
}

ostream& operator<<(ostream& os,const Date& d)
{
	os< d2?:"<<(d3 > d2)< d2?:"<<(d1 > d2)<= d2?:"<<(d3 >= d2)<= d2?:"<<(d1 >= d2)<>input;
		switch(input)
		{
		case 0:
			cout<<"Exit."<>d1;
			cout<<"输入推后的天数:"<>day;
			cout<<"(d1+day):"<<(d1+day)<>d1;
			cin>>d2;
			cout<<"d1-d2 : "<<(d1-d2)<

你可能感兴趣的:(C++)