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