c++日期类

#include<iostream>
#include<stdlib.h>
using namespace std;


class Date
{
	friend ostream& operator<<(ostream& os,const Date& d);
	friend istream& operator>>(istream& is,Date& d);
public:
	Date(int year=0,int month=0,int day=0);
	Date(const Date& d);
	Date& operator=(const Date& d);
	~Date();
public:
	Date& operator++();//因为可能会遇到++(++a);所以引用返回。
	Date operator++(int);
	Date& operator--();//同上--(--b);
	Date operator--(int);
	Date operator+(int);
	Date operator-(int);
	Date& operator+=(int day); 
	Date& operator-=(int);	
	int operator-(const Date& d);
public:
	bool operator>(const Date& d);
	bool operator>=(const Date& d);
	bool operator<(const Date& d);
	bool operator<=(const Date& d);
	bool operator!=(const Date& d);
	bool operator==(const Date& d);
	void Display();
private:
	int Leapyear(int year)
	{
		if((year%4==0&&year%100!=0)||(year%400==0))
			return 1;
		else
			return -1;
	}
	int Getday(int year,int month)
	{
		int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};		
		int ret=Leapyear(year);
		if(ret==1) 
		{
			D[2]=29;
		}
		return D[month];
	}
	int _year;
	int _month;
	int _day;
	
};
Date::Date(int year,int month,int day)
	:_year(year),_month(month),_day(day)
{}
Date::Date(const Date& d)
	:_year(d._year),_month(d._month),_day(d._day)
{}
Date& Date::operator=(const Date& d)
{
	if(this!=&d)
	{
		_year=d._year;
		_month=d._month;
		_day=d._day;
	}
	return *this;
}
Date::~Date()
{}
Date &Date::operator++()
{
	_day++;
	if(_day>Getday(_year,_month))
	{
		if(_month>12)
		{
			_year++;
			_month=1;
			_day=1;
		}
		else
		{
			_month++;
			_day=1;
		}

	}
	return*this;
}
Date Date::operator++(int)
{
	Date tmp=*this;
	if(tmp._day>Getday(_year,_month))
	{
		if(tmp._month>12)
		{
			tmp._year++;
			tmp._month=1;
			tmp._day=1;
		}
		else
		{
			tmp._month++;
			tmp._day=1;
		}
	}
	tmp._day++;
	return tmp;
}
Date &Date::operator--()
{
	_day--;
	if(_day==0)
	{
		_month--;
		if(_month==0)
		{
			_year--;
			_month=12;
		}
		_day=Getday(_year,_month);
	}
	return *this;
}
Date Date::operator--(int)
{
	Date tmp=*this;
	if(tmp._day==1&&tmp._month==1)
	{
		tmp._year--;
		tmp._month=12;
		tmp._day=Getday(tmp._year,tmp._month);
	}
	else if(tmp._day==1&&tmp._month!=1)
	{
		tmp._month--;
		tmp._day=Getday(tmp._year,tmp._month);
	}
	else
	{
		tmp._day--;
	}
	return tmp;
}
Date Date::operator+(int day)
{
	Date tmp=*this;
	if(day<0)
	{
		return tmp-day;
	}
	tmp._day=tmp._day+day;
	while(tmp._day>Getday(tmp._year,tmp._month))
	{
		tmp._day=tmp._day-Getday(tmp._year,tmp._month);
		if(tmp._month==12)
		{
			tmp._year++;
			tmp._month=1;
		}
		tmp._month++;
	}
	return tmp;
}
Date Date::operator-(int day)
{
	Date tmp=*this;
	if(day>0)
	{
		return tmp+day;
	}
	tmp._day=tmp._day-day;
	while(tmp._day>0&&tmp._day<Getday(tmp._year,tmp._month))
	{
		tmp._day=tmp._day+Getday(tmp._year,tmp._month);
		if(tmp._month==1)
		{
			tmp._year--;
			tmp._month=12;
		}
		tmp._month--;
	}
	return tmp;
}
Date& Date::operator+=(int day)
{
	_day+=day;
	while(_day>Getday(_year,_month))
	{
		_day-=Getday(_year,_month);
			if(_month=12)
			{
				_year++;
				_month=1;
			}
			else
				_month++;
	}
	return *this;
}
Date& Date::operator-=(int day)
{
	_day-=day;
	while(_day<0)
	{
		_day=_day+Getday(_year,_month);
		if(_month==1)
		{
			_year--;
			_month=12;
		}
		else
			_month--;
	}
	return *this;
} 
int Date::operator-(const Date& d)
{
	 Date Maxd=*this;
	 Date Mind=d;
	 if(Maxd < Mind)
	 {
	  	Maxd= d;
		 Mind=*this;
	 }
	 int day=0;
	 while(1)
	 {
		 if(Maxd==Mind+day)
		 {
			 break;
		 }
		 day++;
	 }
	 return day;
}
bool Date::operator>(const Date& d)
{
	if(_year>d._year||(_year==_year)&&(_month>d._month)||(_month==d._month)&&(_day>_day))
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator>=(const Date&d)
{
	if(*this<d)
	{
		return false;
	}
	else
	{
		return true;
	}
}
bool Date::operator<(const Date& d)
{
	if(_year<d._year||(_year==_year)&&(_month<d._month)||(_year==d._year)&&(_month==d._month)&&(_day<_day))
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator<=(const Date& d)
{
	if((*this)>d)
	{
		return false;
	}
	else
		return true;
}
bool Date::operator==(const Date& d)
{
		return (_year==d._year)&&(_month==d._month)&&(_day==d._day);
	
}
bool Date::operator!=(const Date& d)
{
	return (_year!=d._year)||(_month!=d._month)||(_day!=d._day);
}
void Date::Display()
{
	cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
ostream& operator<<(ostream& os,const Date& d)
{
	os<<d._year<<"-"<<d._month<<"-"<<d._day<<endl;
	return os;
}
istream& operator>>(istream& is, Date& d)
{
	is>>d._year>>d._month>>d._day;
	return is;
}
int main()
{
	int day=0;
	Date d1(2017,1,1);
	Date d2(2016,1,1);
	Date d3(d1);
	/*day=d2-d1;*/
	/*d1=d1+100;
	d2=d2-100;*/
	/*++d1;*/
	/*--d1;*/
	cout<<day<<endl;
	cout<<"d1:"<<d1<<endl;
	cout<<"d2:"<<d2<<endl;
	cout<<"d3:"<<d3<<endl;
	

	system("pause");
	
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值