MyDate.h
#ifndef __MY_DATE_H__
#define __MY_DATE_H__
#include
#include
using namespace std;
class my_date
{
friend ostream& my_date::operator<<(ostream& os, const my_date& q);
public:
my_date(int x = 1970, int y = 1, int z = 1)
:year(x)
, month(y)
, day(z)
{}
my_date operator+(int m);
my_date& operator+=(int m);
// void add(int m);//加天数
my_date operator-(int m);
my_date& operator-=(int m);
// void del(int m);//减天数
int operator-(my_date& q);
//int diff(my_date& q);//日期差
//void display();//显示
private:
int year, month, day;
};
my_date my_date::operator+(int m)
{
my_date tmp_date(*this);
int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, temp, f;//d为12个月每个月天数的数组
f = 0;
while (m>0)
{
if (tmp_date.year % 100 == 0)
d[1] = (tmp_date.year % 400 == 0 ? 29 : 28);//如果是闰年,2月天数为29
else
d[1] = (tmp_date.year % 4 == 0 ? 29 : 28);//如果是闰年,2月天数为29
temp = d[tmp_date.month - 1] - tmp_date.day;
if (temp >= m)//如果所加天数未超过当月剩余天数
{
tmp_date.day = tmp_date.day + m;
break;
}
m -= temp;
tmp_date.day = 0;
tmp_date.month++;
for (; tmp_date.month <= 12; tmp_date.month++)//开始月遍历进行日期计算
{
if (d[tmp_date.month - 1] >= m)
{
tmp_date.day += m;
break;
}
else
{
m -= d[tmp_date.month - 1];
}
}
if (tmp_date.month <= 12)//若所加天数在本年12月内
break;
tmp_date.month = 1;//所加天数超过今天剩余天数,月初始化为1月,即从下年一月继续算
tmp_date.year++;//加一年
}
return tmp_date;
}
my_date& my_date::operator+=(int m)
{
int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, temp, f;//d为12个月每个月天数的数组
f = 0;
while (m>0)
{
if (year % 100 == 0)
d[1] = (year % 400 == 0 ? 29 : 28);//如果是闰年,2月天数为29
else
d[1] = (year % 4 == 0 ? 29 : 28);//如果是闰年,2月天数为29
temp = d[month - 1] - day;
if (temp >= m)//如果所加天数未超过当月剩余天数
{
day = day + m;
break;
}
m -= temp;
day = 0;
month++;
for (; month <= 12; month++)//开始月遍历进行日期计算
{
if (d[month - 1] >= m)
{
day += m;
break;
}
else
{
m -= d[month - 1];
}
}
if (month <= 12)//若所加天数在本年12月内
break;
month = 1;//所加天数超过今天剩余天数,月初始化为1月,即从下年一月继续算
year++;//加一年
}
return *this;
}
my_date my_date::operator-(int m)
//void my_date::del(int m)//本函数注释与add基本相同
{
my_date tmp_date(*this);
int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, f;
f = 0;
while (m>0)
{
if (tmp_date.year % 100 == 0)
d[1] = (tmp_date.year % 400 == 0 ? 29 : 28);
else
d[1] = (tmp_date.year % 4 == 0 ? 29 : 28);
if (tmp_date.day >= m)
{
tmp_date.day = tmp_date.day - m;
break;
}
m -= tmp_date.day;
tmp_date.month--;
for (; tmp_date.month >= 1; tmp_date.month--)
{
if (d[tmp_date.month - 1] >= m)
{
tmp_date.day = d[tmp_date.month - 1] - m;
break;
}
else
{
m -= d[tmp_date.month - 1];
}
}
if (tmp_date.month >= 1)
break;
tmp_date.month = 12;
tmp_date.year--;
tmp_date.day = 31;
}
return tmp_date;
}
my_date& my_date::operator-=(int m)
//void my_date::del(int m)//本函数注释与add基本相同
{
int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, f;
f = 0;
while (m>0)
{
if (year % 100 == 0)
d[1] = (year % 400 == 0 ? 29 : 28);
else
d[1] = (year % 4 == 0 ? 29 : 28);
if (day >= m)
{
day = day - m;
break;
}
m -= day;
month--;
for (; month >= 1; month--)
{
if (d[month - 1] >= m)
{
day = d[month - 1] - m;
break;
}
else
{
m -= d[month - 1];
}
}
if (month >= 1)
break;
month = 12;
year--;
day = 31;
}
return *this;
}
int my_date::operator-(my_date& q)
//int my_date::diff(my_date& q)
{
int differece;
int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, m, i;
my_date temp;
my_date *pSmall, *pBig;
if (year == q.year)//如果年相同
{
if (month == q.month)//若月相同
{
differece = q.day - day;
}
else//月不同
{
if (month>q.month)//让pSmall指针指向小日期,pBig指针指向大日期
{
pSmall = &q;
pBig = this;
}
else
{
pSmall = this;
pBig = &q;
}
temp.month = pSmall->month;
temp.day = pSmall->day;
for (i = 1; i <= 366; i++)//每次加一天,看看加多少次,两个日期相同,加的次数即为相差的天数
{//由于年份相同,相差的天数必然不会大于366,实际循环次数不大于366
temp + 1;//使用 运算符重载 +
if (temp.month == pBig->month && temp.day == pBig->day)
{
differece = i;
break;
}
}
differece = (month>q.month ? -differece : differece);
}
}
else//年份不同
{
if (year>q.year)//让pSmall指针指向小日期,pBig指针指向大日期
{
pSmall = &q;
pBig = this;
}
else
{
pSmall = this;
pBig = &q;
}
temp.year = pSmall->year;
temp.month = pSmall->month;
temp.day = pSmall->day;
m = pBig->year - pSmall->year;
for (i = (m - 1) * 365; i <= (m + 1) * 366; i++)//每次加一天,看看加多少次,两个日期相同,加的次数即为相差的天数
{//计算出年份差m后,两个日期的天数差必定在[(m-1)*365,(m+1)*366]范围内,实际循环的次数不大于2*366
//temp.add(1);
temp += 1;
if (temp.month == pBig->month && temp.day == pBig->day)
{
differece = i - (m - 1) * 365 + 1;
break;
}
}
differece = (year>q.year ? -differece : differece);
}
return differece;
}
ostream& operator<<(ostream& os, const my_date& q)
//void my_date::display()
{
os << q.year << "年" << q.month << "月" << q.day << "日";
return os;
}
#endif
My_Date.cpp
#include "My_Date.h"
class date
{
friend ostream& operator<<(ostream& os, const date &d);
friend istream& operator>>(istream& in, date &d);
public:
date(int year = 1900, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (is_invalid_date())//无效的
{
_year = 1900;
_month = 1;
_day = 1;
}
}
public:
bool is_invalid_date()//判断日期是否无效
{
if ((_year < 1900)
|| ((_month < 1) || (_month > 12))
|| ((_day < 1) || (_day > day_in_month())))
{
return true;
}
return false;
}
public:
int day_in_month()
{
int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (is_leap_year())
{
days[2] += 1;
}
return days[_month];
}
public:
bool is_leap_year()
{
if ((_year % 400 == 0) || ((_year % 100) != 0 && _year % 4 == 0))
{
return true;
}
return false;
}
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;
}
public:
void to_correct_date()
{
//2000 11 -3
while (_day <= 0)
{
if (_month == 1)
{
_month = 12;
_year -= 1;
}
else
{
_month -= 1;
}
_day += day_in_month();
}
while (_day > day_in_month())
{
_day -= day_in_month();
if (_month == 12)
{
_month = 1;
_year += 1;
}
else
{
_month += 1;
}
}
}
date operator+(int day)
{
date d(*this);
d._day += day;
//
d.to_correct_date();
return d;
}
date& operator+=(int day)
{
_day += day;
//
this->to_correct_date();
return *this;
}
date operator-(int day)
{
date d(*this);
d._day -= day;
//
d.to_correct_date();
return d;
}
date& operator-=(int day)
{
date d(*this);
_day -= day;
//
this->to_correct_date();
return *this;
}
int operator-(const date &d)
{
int days = 0;
date d1(d);
date d2(*this);
if (d1 > d2)//d2 记录大的日期
{
d1 = (*this);
d2 = d;
}
while (d1 != d2)
{
days++;
d1 += 1;
}
return days;
}
bool operator>(const date& d)
{
if (_year > d._year)
return true;
if (_year == d._year)
{
if (_month > d._month)
return true;
if (_month == d._month)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
bool operator>=(const date& d)
{
return (*this > d) || (*this == d);
}
bool operator!=(const date& d)
{
return !(*this == d);
}
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)
{
return (*this > d);
}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& os, const date& d)
{
os << d._year << "-" << d._month << "-" << d._day;
return os;
}
istream& operator>>(istream& in, date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
int main()
{
date d(2016, 3, 5);
cout << d << endl;
d += 2;
cout << "d+2: " << d << endl;
d += 31;
cout << "d+31: " << d << endl;
d -= 61;
cout << "d-61: " << d << endl;
cout << "==============" << endl;
date d1(2016, 3, 5), d2(2016, 2, 1);
cout << (d2 - d1) << endl;
getchar();
return 0;
}
Test.cpp
#include "My_Date.h"
int main()
{
my_date d(2016, 2, 24);
cout << "原日期date1:" << endl;
//d.display();
cout << d << endl;
// d.add(60);
d += 60;
cout << "date2=date1+60:";
cout << d << endl;
d -= 60;
cout << "date3=date2-60:";
cout << d << endl;
// printf("2016-2-24与2017-2-24日相差%d天\n",d.diff(my_date(2017,2,24)));
cout << "2016-2-24与2017-2-24日相差" << (d - my_date(2017, 2, 24)) << "天" << endl;
return 0;
}