万年历

#include<iostream>
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>12||month<1
			||day<1||day>GetMonthday(year,month))
		{
			cout<<"Invalid Date"<<endl;
			_year=1900;
			_month=1;
			_day=1;
		}
	}
	 Date(const Date& d)
		 :_year(d._year)
		 ,_month(d._month)
		 ,_day(d._day)
	 {}
	 Date& operator=(const Date &d)
	 {
		 if(this!=&d)
		 {
			 _year=d._year;
			 _month=d._month;
			 _day=d._day;
		 }
		 return *this;
	 }
	 static bool IsLeapYear(int year)
	 {
		 if((year % 4 == 0 && year % 100 != 0)
			 ||(year %400 ==0))
			 return true;
		 else
			 return false;
	 }
	 static int GetMonthday(int year,int month)
	 {
		 static int arr[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
		 if(IsLeapYear(year))
		 {
			 arr[2]=29;
		 }
		 else
		 {
			 arr[2]=28;
		 }
		 return arr[month];
	 }
public:
	//
	// 比较日期的运算符重载
	//
	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(_year>d._year)
		{
			return true;
		}
		else if(_year==d._year)
		{
			if(_month>d._month)
			{
				return true;
			}
			else if(_month==d._month)
			{
				if(_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)
	{
		if(day<0)
		{
			return *this-(-day);
		}
		Date tmp(*this);
		tmp._day+=day;
		while(tmp._day>GetMonthday(tmp._year,tmp._month))
		{
			tmp._day-=GetMonthday(tmp._year,tmp._month);
            if(tmp._month==12)
			{
				tmp._year++;
                tmp._month=1;
			}
			else
			{
				tmp._month++;
			}
		}
		return tmp;
	}
	Date operator-(int day)
	{
		if(day<0)
		{
			day=0-day;
			return *this+day;
		}
		Date tmp(*this);
		tmp._day-=day;
		while(tmp._day>=0)
		{
			if(tmp._month==1)
			{
				tmp._year--;
				tmp._month=12;
			}
			else
			{
				tmp._month++;
			}
			tmp._day+=GetMonthday(tmp._year,tmp._month);
		}
		return tmp;
	}
	Date& operator-=(int day)
	{
		Date tmp=*this-day;
        *this=tmp;
		return *this;
	}
	Date& operator+=(int day)
	{
		Date tmp=*this+day;
        *this=tmp;
		return *this;
	}

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

	//
	// 两个日期相加没有意义
	// 计算两个日期相减以后的差的天数
	//
	int operator-(const Date& d)
	{
		int flag=1;
		Date x1=*this,x2=d;
		if(x1>x2)
		{
			x1=d;
			x2=*this;
		}
		else
		{
			flag=-1;
		}
		int day=0;
		while(x1!=x2)
		{
			++x1;
			++day;
		}
		return day*flag;
	}
	friend ostream& operator<<(ostream& os,const Date& d);
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& os,const Date& d)
{
	os<<d._year<<"-"<<d._month<<"-"<<d._day<<endl;
	return os;
}
istream& operator>>(istream& is,Date& d)
{
	cout<<"请依次输入年月日:"<<endl;
	is>>d._year;
	is>>d._month;
	is>>d._day;
	return is;
}
int main()
{
	Date d1(2015,1,1);
	cout<<d1<<endl;

    Date d2(2015,2,2);
	cout<<d1<<endl;

    Date d3(2015,3,2);
	cout<<"d2==d3?:"<<(d2==d3)<<endl;
    
	cout<<(d2+30)<<endl;

	cout<<(d3-d2)<<endl;
	return 0;
}

你可能感兴趣的:(万年历)