今天写了一个日期类,发上来共享一下
//头文件:(date.h)
class Date
{
public:
Date();
Date(int y,int m,int d);
Date(const Date& dt);
Date& add_year(int n);
Date& add_month(int n);
Date& add_day(int n);
bool leapyear(int n);
Date operator+(int n);
Date operator-(int n);
Date& operator++();
Date& operator--();
Date& operator+=(int n);
Date& operator-=(int n);
bool operator==(Date dt) const;
bool operator!=(Date dt) const;
bool operator<(Date dt) const;
bool operator>(Date dt) const;
int operator-(Date dt);
int get_week();
static void set_today(int y,int m,int d);
void print();
private:
int y,m,d;
week w;
static Date default_date;
};
//cpp文件
#include"date.h"
#include<iostream>
using namespace std;
#define CASE break;case
typedef enum wk{sun=0,mon,tue,wed,thu,fri,sat}week;
const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
Date Date::default_date=Date(2008,3,16);
void Date::set_today(int y,int m,int d)
{
default_date.y=y;
default_date.m=m;
default_date.d=d;
}
Date::Date()
{
*this=default_date;
}
Date::Date(int y,int m,int d)
{
this->y=y;
this->m=m;
this->d=d;
}
Date::Date(const Date &dt)
{
y=dt.y;
m=dt.m;
d=dt.d;
}
Date& Date::add_day(int n)
{
d+=n;
int day=m==2&&leapyear(y)?month[m]+1:month[m];
while(d>day)
{
m++;
d-=day;
day=m==2&&leapyear(y)?month[m]+1:month[m];
}
if(m>12)
{
y+=m/12;
m=m%12;
}
return *this;
}
Date& Date::add_month(int n)
{
m+=n;
if(m>12)
{
y+=m/12;
m=m%12;
}
int day=m==2&&leapyear(y)?month[m]+1:month[m];
if(d>day)
{
m++;
d-=day;
}
return *this;
}
Date& Date::add_year(int n)
{
if(leapyear(y)&&!leapyear(y+n)&&m==2&&d==29)
{
m=3;
d=1;
}
y+=n;
return *this;
}
bool Date::operator==(Date dt) const
{
if(y==dt.y&&m==dt.y&&d==dt.d)
return 1;
return 0;
}
bool Date::operator !=(Date dt) const
{
if(*this==dt)
return 0;
return 1;
}
bool Date::operator<(Date dt) const
{
if(y!=dt.y)
return y<dt.y;
if(m!=dt.m)
return m<dt.m;
return d<dt.d;
}
bool Date::operator>(Date dt) const
{
if(y!=dt.y)
return y>dt.y;
if(m!=dt.m)
return m>dt.m;
return d>dt.d;
}
Date Date::operator+(int n)
{
this->add_day(n);
return *this;
}
Date Date::operator-(int n)
{
while(n)
{
int day=(m-1)==2&&leapyear(y)?month[m-1?m-1:12]+1:month[m-1?m-1:12];
if(n>day)
{
d-=day;
n-=day;
m--;
}
else
{
if(n>=d)
{
d=day+d-n;
m--;
}
else
d-=n;
}
if(m<0)
{
m=12;
y--;
}
}
return *this;
}
int Date::operator -(Date dt)
{
int sum=0,day;
while(y>dt.y+1)
{
sum+=leapyear(dt.y)?366:365;
dt.y++;
}
if(y>dt.y&&(m>dt.m||m==dt.m&&d>=dt.d))
{
sum+=leapyear(dt.y)?366:365;
dt.y++;
}
while(m>dt.m)
{
day=dt.m==2&&leapyear(dt.y)?month[dt.m]+1:month[dt.m];
sum+=day;
dt.m++;
}
sum+=d-dt.d;
return sum;
}
Date& Date::operator+=(int n)
{
*this=*this+n;
return *this;
}
Date& Date::operator-=(int n)
{
*this=*this-n;
return *this;
}
Date& Date::operator++()
{
*this+=1;
return *this;
}
Date& Date::operator--()
{
*this-=1;
return *this;
}
bool Date::leapyear(int n)
{
if(n%4)
return 0;
if(n%100==0)
if(n%400)
return 0;
return 1;
}
void Date::print()
{
cout<<y<<"."<<m<<"."<<d<<"\t";
w=(week)(get_week()%7);
switch(w)
{
case 0:
cout<<"sunday";
CASE(1):
cout<<"monday";
CASE(2):
cout<<"tuesday";
CASE(3):
cout<<"wednesday";
CASE(4):
cout<<"thursday";
CASE(5):
cout<<"friday";
CASE(6):
cout<<"saturday";
break;
default:
cout<<"the week is wrong";
}
cout<<endl;
}
int Date::get_week()
{
return *this>default_date?(*this-default_date)%7:7-(default_date-*this)%7;
}