日期类

//万年历
#include<iostream>
using namespace std;


class Date
{
public:
	//打印日期
	void Display()
	{
		cout<<"Year:"<<_year<<endl;
		cout<<"Month:"<<_month<<endl;
		cout<<"Day:"<<_day<<endl;
	}


	// 初始化列表进行初始化。 
	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<<"Input Error"<<endl;
		else
		;
	}


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


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)//else if
	{
		if(_year > d._year)
		{
			return true;
		}
		if (_year == d._year )
			{
				if (_month >d._month )
				{
					return true;
				}
				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)
	{
		Date tmp(*this);
		tmp._day += day;
		while(tmp._day < 0)
		{
			tmp._day -= GetMonthDay(tmp._year ,tmp._month );
			if(tmp._month == 1)
			{
				tmp._year --;
				tmp._month = 12;
			}
			else 
			 tmp._month --;
			return tmp;


		}


		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)
	{
		Date tmp(-day);
		return tmp;
	}
	Date& operator-=(int day)
	{
		Date *this(-day); 
	}
	Date& operator+=(int day)
	{
		Date *this(day);
	}


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






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


	int GetMonthDay(int year,int month)
	{
		static int DayTab[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
		if(IsLeapYear(year))
			DayTab[2] = 29;
		else 
			DayTab[2] = 28;
		return DayTab[month];
	}
	friend istream& operator >>(istream& is,const Date& d);
	friend ostream& operator <<(ostream& os,const Date& d);


	//
	// 两个日期相加没有意义
	// 计算两个日期相减以后的差的天数
	//


	int operator-(const Date& d)
	{
		int flag = 0;
		Date x1 = *this,x2 = d;
		if(x1 < x2)
		{
			flag = -1;
			x1 = d;
			x2 = *this;
		}
		int count = 0;
		while(x1 == x2)
		{
			++x2;
			count++;
		}
		return count*flag;
	}
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,const Date& d)
{
	is>>d._year>>"-">>d._month>>"_">>d._day>>endl;
	return is;
}


void TestDate()
{}


int main()
{	
	Date d1(2008,10,26);
	/*cout<<(++d1)<<endl;*/
	/*cout<<(d1 + 1)<<endl;*/
	/*Date d2(2008,10,27);*/
	//cout<<"d1 == d2?  "<<(d1 == d2)<<endl;
	//cout<<"d1 != d2?  "<<(d1 != d2)<<endl;
	//cout<<"d1 > d2?  "<<(d1 > d2)<<endl;
	//cout<<"d1 >= d2?  "<<(d1 >= d2)<<endl;
	//cout<<"d1 < d2?  "<<(d1 < d2)<<endl;
	//cout<<"d1 <= d2?  "<<(d1 <= d2)<<endl;
	/*d2.Display();
	Date d3;
	d3 = d2;
	d3.Display();*/
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值