Date类的实现

1)Date.cpp

#include"Date.h"
#include<stdlib.h.>

void Test()
{
	 Date d1;
	 Date d2(1993,2,20);
	 Date d3(d2);
	 Date d4;
	 d4=d3;	 cout<<"d1:"<<d1<<"d2:"<<d2<<"d3:"<<d3<<"d4:"<<d4;
	 cout<<"d1+15:"<<d1+15;
	 cout<<"d2+329:"<<d2+329;
	 cout<<"d1-17:"<<d1-17;
	 cout<<"d2-555:"<<d2-555;

	 cout<<"d1>d2:"<<(d1>d2)<<endl;
	 cout<<"d2<d4:"<<(d2<d4)<<endl;
	 cout<<"d4==d3:"<<(d4==d3)<<endl;
	 cout<<"d2!=d1:"<<(d2!=d1)<<endl;
}

void Funtest()
{
  	  Date d5(2017,2,20);
	  Date d6(2017,2,20);
	  Date d7(2017,4,1);
	  Date d8(2017,4,30);
	
	 cout<<"d8-d7:"<<d8-d7<<endl;

	 cout<<"d5<=d7:"<<(d5<=d7)<<endl;
	 cout<<"d5>=d6:"<<(d5>=d6)<<endl;
	 cout<<"d5==d7:"<<(d5==d7)<<endl;

	 cout<<"d7:"<<(d7)<<endl;
	 cout<<"d8:"<<(d8)<<endl;

	 cout<<"(d7++):"<<(d7++)<<endl;
	 cout<<"++d7:"<<(++d7)<<endl;
	 cout<<"d8--:"<<(--d8)<<endl;
	 cout<<"--d6:"<<(--d6)<<endl;
	 cout<<"d7:"<<(d7)<<endl;
	 cout<<"d8:"<<(d8)<<endl;
	 cout<<"d8-d7:"<<d8-d7<<endl;
}

int main()
{
	Test();
	Funtest();
	system("pause");
	return 0;
}

2)Date.h

#include<stdio.h>
#include<assert.h>
#include<iostream>
using namespace std;
class Date
{
private:
	int _year;
	int _month;
	int _day;

	 bool IsLeapYear(int year)
	 {
	   return((year%4==0&&year%100!=0)||(year%400==0)) ;
	 }
	 
	 int GetMonthDay(int year,int month)
	 {
	   assert(month>0&&month<13);
	   static int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	   if((month==2)&&(IsLeapYear(year)))
	   {
		  days[month]+=1;
	   }
	   return days[month];
	 }

public:
  Date(int year=2017,int month=1,int day=1)
	  :_year(year),_month(month),_day(day)
  {	 
	  if((_year>0)&&(_month>0&&_month<13)&&(_day<=GetMonthDay(_year,_month)))
	  {
	       _year=year;
	       _month=month;
	       _day=day;
	  }
  }

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

 Date operator-(int days)  //求当前日期day天前的日期
  {
	  Date temp(*this);
	  temp._day-=days;
	  if(days<0)
	  {
		 return *this-days;
	  }
	  while(temp._day<=0)
	  {
	      if(temp._month==1)
		  {
			  temp._year-=1;
			  temp._month=12;
		  }
		  else
		  {
			  temp._month-=1;
		  }
		 temp._day+=GetMonthDay(temp._year,temp._month);
	  }
	   return temp;
  }

 Date operator+(int days)  //求当前日期day天后的日期
  {
	  Date temp(*this);
	  temp._day+=days;
	  if(days<0)
	  {
		 return *this-(0-days);
	  }
	int  dayln=GetMonthDay(temp._year,temp._month);
	while(temp._day>dayln)	  
	{
	    temp._day-=dayln;
	    if(temp._month==12)
		  {
			  temp._year+=1;
			  temp._month=1;
		  }
		  else
		  {
			  temp._month+=1;
		  }
	  }
	   return temp;
  }

 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 ) < d )
		return false;
	else
		return true;
  }

bool operator<=(const Date& d)
  {
	   if((*this)>d)
		 return false;
          else
		 return true;	   
  }

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

bool operator<(const Date& d)
  {
	    if(!(*this==d))
		   return true;
	   else
		   return false;
  }

 int operator-(const Date& d) //求两个日期之间差多少天
 {
	 Date min(*this);
	 Date max(d);
	 int count=0;
	 if( max < min)
	 {
		 min=max;
		 max=*this;
	 }
	while(min < max)
	{
		 ++min;
	        ++count;
	}
	return count;
 }

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

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

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

  ~Date()
  {
	 if(NULL!=_day||NULL!=_year||NULL!=_month)
	 {
		 _day=NULL;
	     _year=NULL;
		 _month=NULL;
	 }
  }

  friend ostream& operator<<(ostream & _cout,const Date &date)
  {
	_cout << date._year<< "-" << date._month<<" -" << date._day << endl;
		 return _cout;
  }

  
  friend istream& operator>>(istream & _cin,Date &date)
  {		      _cin>>date._year>>date._month>>date._day;
		 return _cin;
  }
};

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ashley zhao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值