Date.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
//构造函数
Date(int year = 1900, int month = 1, int day = 1)
//:_year(year) //初始化列表
//,_month(month)
//,_day(day)
{
if ((month > 0) && (month < 13) && (day > 0) && (day < (GetMonthDays(year, month) + 1)))
{
_year = year;
_month = month;
_day = day;
//cout << _year << "--" << _month << "--" << _day << endl;
}
else
{
//printf("非法日期\n");
cout << "非法日期" << endl;
return;
}
}
//拷贝构造函数
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//析构函数
//~Date()
//{
// cout << "~Data" << endl;
//}
//运算符重载
Date& 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);
bool operator< (const Date& d);
//一般的实现一个等于和小于(或大于)就可以将其他的复用出来。
//但是为了后面日期与日期相减时,相差天数太大会出现递归层数太多而使程序崩溃,
//最好大于和小于都做好处理。
Date operator+ (int day);
Date operator- (int day);
Date& operator++ (); //前置
Date operator++ (int); //后置
Date& operator-- (); //前置
Date operator-- (int); //后置
Date& operator+= (int day);
Date& operator-= (int day);
int operator- (const Date d);
/////////////////////////////////////
//其他函数
void Show();
bool IsWhatYear(const int year);
int GetMonthDays(int year, int month);
private:
int _year;
int _month;
int _day;
};
Date.cpp
#include"Date.h"
///////////////////////以下函数也可以在类内定义//////////////////////////////////////
///////////////////////////////类外定义/////////////////////////////////////////////
//判断是不是闰年,是闰年返回1,不是闰年返回0
void Date:: Show()
{
cout << _year << "—" << _month << "—" << _day << endl;
}
bool Date:: IsWhatYear(const int year)
{
if ((0 == year % 4 && year % 100 != 0) || (0 == year % 400))
return 1;
return 0;
}
//计算这个月多少天
int Date::GetMonthDays(int year, int month)
{
static int MonthArr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
MonthArr[2] = 28;
if ((2 == month) && IsWhatYear(year))
return MonthArr[2] + 1;
return MonthArr[month];
}
//赋值(=)操作符重载
Date& Date:: operator= (const Date& d)
{
if (this != &d) //当自己给自己赋值的时侯就不需要浪费时间了
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
//等于(==)重载 相等返回0,不相等返回1
bool Date:: operator== (const Date& d)
{
if ((_year == d._year) && (_month == d._month) && (_day == d._day))
{
return true;
}
return false;
}
//不等于(!=)重载
bool Date:: operator!= (const Date& d)
{
return !(*this == d);
}
//大于(>)重载
bool Date:: operator>= (const Date& d)
{
if ((_year > d._year)
|| ((_year == d._year) && (_month > d._month))
|| ((_year == d._year) && (_month == d._month) && (_day > d._day)))
{
return true;
}
return false;
}
//小于(<)重载
bool Date:: operator< (const Date& d)
{
return (_year < d._year)
|| ((_year == d._year) && (_month < d._month))
|| ((_year == d._year) && (_month == d._month) && (_day < d._day));
}
//小于等于(<=)重载
bool Date:: operator<= (const Date& d)
{
return !(*this > d);
}
//大于等于(>=)重载
bool Date:: operator> (const Date& d)
{
return (*this > d) && (*this == d);
}
//+重载,日期加一个天数
Date Date:: operator+ (int day)
{
if (day < 0) //天数小于零,则相当于减去这个天数
return *this - (-day);
Date ret(*this);
ret._day += day;
while (ret._day > GetMonthDays(ret._year, ret._month))
{
ret._day -= GetMonthDays(ret._year, ret._month);
++ret._month;
if (13 == ret._month)
{
++ret._year;
ret._month = 1;
}
}
return ret;
}
//-重载,日期减去一个天数
Date Date:: operator- (int day)
{
if (day < 0) //如果天数小于零,则相当于加上这个天数
return *this + (-day);
Date ret(*this);
ret._day -= day;
while (ret._day <= 0)
{
--ret._month;
ret._day += GetMonthDays(ret._year, ret._month);
if (0 == ret._day)
{
ret._month = 12;
ret._day += GetMonthDays(ret._year, ret._month);
}
if (0 == ret._month)
{
--ret._year;
ret._month = 12;
ret._day += GetMonthDays(ret._year, ret._month);
}
}
return ret;
}
//重载前置++
Date& Date:: operator++()
{
return *this = *this + 1;
}
//重载后置++
Date Date:: operator++(int)
{
Date ret(*this);
*this = *this + 1;
return ret;
}
//重载前置--
Date& Date:: operator--()
{
return *this = *this - 1;
}
//重载后置--
Date Date:: operator--(int)
{
Date ret(*this);
*this = *this - 1;
return ret;
}
//重载+=
Date& Date:: operator+=(int day)
{
if (day < 0)
return *this = *this - (-day);
return *this = *this + day;
}
//重载-=
Date& Date::operator-=(int day)
{
if (day < 0)
return *this = *this + (-day);
return *this = *this - day;
}
//重载- ,日期间去一个日期
//让大的日期--到小的日期,就可以得知两个日期之间的天数 或者
//让小的日期++到大的日期,就可以得知两个日期之间的天数
int Date:: operator-( const Date d)
{
int day = 0;
Date max(*this);
Date min(d);
//Date tmp;
int flag = 1;
if (*this < min)
{
//tmp = max;
//max = min;
//min = tmp;
max = d;
min = *this;
flag = -1;
}
while (max != min)
{
++day;
++min;
//--max;
}
return day * flag; //*this大,相差天数为正;d大相差的天数为负
}
test.cpp
#include"Date.h"
//void test1()
//{
// Date d(2018, 3, 30);
// Date d1;
// d1 = d - (-100);
// d1.Show();
//}
//void test2()
//{
// Date d(2019, 2, 28);
// --d;
// //d--;
// ++d;
// d++;
// d.Show();
//}
//void test3()
//{
// Date d(2018, 3, 30);
// //d += 100;
// //d += -100;
// //d -= 100;
// //d -= -100;
//
// d.Show();
//}
void test4()
{
int i = 0;
Date d1(2018, 3, 30);
Date d2(1996, 7, 25);
i = d1 - d2;
printf("%d\n", i);
}
int main()
{
//Date d(2018, 3);
//cout << d.GetMonthDays(2016, 1) << endl;
//test1();
//test2();
//test3();
test4();
system("pause");
return 0;
}