万年历-----日期计数器

日期计数器---->就是可以实现日期的加减天数,日期加日期,日期减日期,两个日期之间大小的判断等等。

#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
	Date(int year=1,int month=1,int day=1)
		:_year(year)
		,_month(month)
		,_day(day)
	{}
	Date (Date &d)
	{
		_year=d._year ;
		_month=d._month ;
		_day=d._day ;
	}
	void SetDate();            //获取日期
	void Display();           //显示日期
	bool IsLeepYear(int year)  //判断是否为闰年
	{
		if(((year/4==0)&&(year/100!=0))||(year/400==0))
		{
			return true;
		}
		else
			return false;
	}
	bool IsValiData()        //判断日期是否合法
	{
		if((_year<1990)||(_month<1)||(_month>12)||(_day<1)||(_day>DayofMonth(_year,_month)))
			return false;
		else
			return true;
	}
	int DayofMonth(int year,int month)      //判断每个月的天数
	{
		int arr[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
		if(IsLeepYear(year)==true)   //闰年二月为29天
		{
			arr[2]=29;
		}
		return arr[month];
	}
	int LastDayofMonth(int year,int month)
	{
		int arr[13]={0,31,31,28,31,30,31,30,31,31,30,31,30} ;  //上个月的最大天数
		if(IsLeepYear(year)==true)
		{
			arr[3]=29;
		}
		return arr[month];
	}
	Date& operator+(const int day)
	{
		if(day>=0)
		{
			_day+=day;
			while(_day>this->DayofMonth(_year,_month))
			{
				int tmp=_day-DayofMonth(_year,_month);
				if(_month<12)
				{
					_month++;
					_day=tmp;
				}
				else
				{
					_year++;
					_month=1;
					_day=tmp;
				}
			}
			
		}
		else 
		{
			operator-(-day);
		}
		return *this;
	}
	Date& operator-(const int day)
	{
		if(day>=0)
		{
			_day-=day;
			while(_day<1)
			{
                int tmp=this->LastDayofMonth (_year,_month)-(-_day);
				if(_month>1)
				{
					_month--;
					_day=tmp;
				}
				else
				{
					_year--;
					_month=12;
					_day=tmp;
				}
			}
		}
		else
		{
			operator+(-day);
		}
		return *this;
	}
	int  operator-(const Date& d)
	{
		int day=0;
		if(IsLeepYear(d._year)==true)
			 day=d._year*366;
		else
			 day=d._year *365;
		int m=d._month ;
		while(m)
		{
			day=day+DayofMonth(d._year ,d._month );
			m--;
		}
		day+=d._day ;
		int days=0;
		if(IsLeepYear(_year)==true)
			 days=_year*366;
		else
			 days=_year *365;
		int n=_month ;
		while(n)
		{
			days=days+DayofMonth(_year ,_month );
			n--;
		}
		days+=_day ;
		int value=day-days;
		if(value<0)
			return (-value);
		else
			return value;
	}
	Date& operator=(const Date& d)
	{
		_year=d._year ;
		_month=d._month ;
		_day=d._day ;
		return *this;
	}
    bool operator==(const Date& d)
	{
		if((_year==d._year)&&(_month==d._month )&&(_day==d._day ))
			return true;
		else 
			return false;
	}
	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;
	}
    Date& operator++()    //前置++
	{
		_day+=1;
		if(_day> DayofMonth(_year,_month))
		{
			_month++;
			_day=1;
			if(_month>12)
			{
				_year++;
				_month=1;
			}
		}
		return *this;
	}
	Date operator++(int)   //后置++
	{
		Date tmp=*this;
		_day+=1;
		if(_day> DayofMonth(_year,_month))
		{
			_month++;
			_day=1;
			if(_month>12)
			{
				_year++;
				_month=1;
			}
		}
		return tmp;
	}
	Date& operator--()     //前置--
	{
		_day-=1;
		if(_day==0)
		{
			_day=LastDayofMonth(_year,_month);
			_month--;
			if(_month==0)
			{
				_year--;
				_month=12;
			}
		}
		return *this;
	}
	Date operator--(int)  //后置--
	{
		Date tmp=*this;
		_day-=1;
		if(_day==0)
		{
			_day=LastDayofMonth(_year,_month);
			_month--;
			if(_month==0)
			{
				_year--;
				_month=12;
			}
		}
		return tmp;
	}
	
private:
	int _year;
	int _month;
	int _day;
};
void Date::SetDate()
{
	cout<<"请输入年月日"<<endl;
	cin>>_year>>_month>>_day;
}
void Date::Display()
{
	cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
int main()
{
	//测试相差几天
	/*Date d1,d2;
	d1.SetDate();
	d2.SetDate();
	int ret=d1-d2;
	cout<<"相差天数为:"<<ret<<endl;*/

	//测试加上30天为几号
	/*Date d;
	d.SetDate ();
	d+100;
	d.Display();*/

	//测试前置++,后置++
	Date d;
	d.SetDate ();
	//++d;
	d++;
	d.Display();
	system("pause");
	return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值