前面介绍了运算符重载相关规则和方法,今天用运算重载函数实现对日期类的操作。
目录
前面准备
实现功能:
-运算符
Date类和int 相减
Date类和Date类相减
+运算符
+=,-=运算符
==,!=运算符
>,>=运算符
<,<=运算符
++,--运算符
功能测试
完整代码
Date.h
fun.cpp
test.cpp
前面准备
日期的运算需要考虑到每个月天数不一样,平年闰年2月天数也不一样,因此需要先写两个函数
1.判断year是否为闰年
2.返回当前年月的天数
代码如下
bool leap(int year)
{
//不是百年4年一闰
if (year % 100 != 0 && year % 4 == 0)
return true;
if (year % 100 == 0 && year % 400 == 0)
return true;
return false;
}
//每月多少天
int MonthDay(int year, int month)
{
int arr[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && !leap(year))
return arr[month]-1;
else
return arr[month];
}
实现功能:
-(分为Date类和Date类相减,Date类和int 相减),+,+=,-=,==,!=,<,<=,>,>=,++,--。
-运算符
这里实现Date类和Date类相减,Date类和int 相减
Date类和int 相减
Date类和int相减,也就是要知道当前日期下几天前的日期,结果是要日期,因此函数的返回类型就是Date,另外-运算符是一个双目运算符,因此运算符重载函数的形参列表只能写一个形参(成员函数有一个默认的this指针),代码如下:
Date operator-(int day)
{
day -= Day;
Month--;
while (day > MonthDay(Year, Day))
{
day -= MonthDay(Year, Month);
Month--;
if (Month < 1)
{
Year--;
Month = 12;
}
}
Day = MonthDay(Year, Month) - day;
return *this;
}
Date类和Date类相减
这里我用的是分别算出date1和date2到1年1月1日的天数,然后相减得到两个日期相差的天数,结果是一个天数,那么函数类型就是int,先写日期到1年1月1日天数的函数,代码如下:
int number1()
{
int year = 1,month = 1;
int thisday = 0;
while (year < Year)
{
thisday += (leap(year)) ? 366 : 365;
year++;
}
while (month < Month)
{
thisday += MonthDay(Year, month);
month++;
}
thisday += Day;
return thisday;
}
接下来是Date类和Date相减的函数
int operator-(Date date)
{
return this->number1()-date.number1();
}
+运算符
这里实现的是,Date类和int相加的结果,也就是,在当前日期下,几天后是几月几号,从这就可以看出运算后的结果是一个Date类,因此函数的类型也就是Date.在函数体刚开始我判断了day是否小于0,小于0也就是几天前是几月几号,直接调用-运算符重载函数就可以。
Date operator+(int day)
{
if (day < 0)
{
return operator-(day);
}
day += Day;
while (day > MonthDay(Year, Month))
{
day -= MonthDay(Year, Month);
Month++;
if (Month > 12)
{
Year++;
Month = 1;
}
}
Day = day;
return *this;
}
+=,-=运算符
+=运算符可以直接调用+运算符重载函数实现
Date operator+=(int day)
{
*this = *this + day;
return *this;
}
同理-=也是同样的道理
Date operator-=(int day)
{
*this = *this - day;
return *this;
}
==,!=运算符
==是判断左右两边的表达式是否相等,Date要相等,就得年相等,月相等,日相等,同时满足,才返回true,代码如下,
bool operator==(Date date)
{
if(Year == date.Year&&Month==date.Month&&Day==date.Day)return true;
return false;
}
!=就容易判断了,==的反面就是!= ;
bool operator!=(Date date)
{
if (*this == date)return false;
return true;
}
>,>=运算符
在我们的认知里,先出现的日期会大,因此比较的时候数值越小,日期越大,逐一比较就行,具体代码如下:
bool operator>(Date date)
{
if (Year < date.Year)
return true;
else
{
if (Month < date.Month)
return true;
else
{
if (Day < date.Day)
return true;
else false;
}
}
}
>=,就是既满足大于也满足相等,满足其一即可,具体代码如下
bool operator>=(Date date)
{
if (*this > date||*this == date)
return true;
else
return false;
}
<,<=运算符
与前面同理,在写<的时候,可以借助前面的>=的函数,>=的反面就是<,具体代码如下:
bool operator<(Date date)
{
if (*this >= date )return false;
return true;
}
<=的反面是>,因此可以借助>,来实现<=的重载
bool operator<=(Date date)
{
if (*this > date)return false;
else return true;
}
++,--运算符
自增,自减运算符就是对自身进行加1,减1的操作,返回自身,需要注意的就是进位,例如:若是Month加到13,那么应该,Year++;Month=1;具体代码如下:
Date operator++()
{
Day++;
if (Day > MonthDay(Year, Month))
{
Month++;
if (Month > 12)
{
Year++;
Month = 1;
}
Day = 1;
}
return *this;
}
Date operator--()
{
if (Day == 1)
{
Month--;
if (Month == 0)
{
Year--;
Month = 12;
}
Day = MonthDay(Year, Month);
return *this;
}
}
功能测试
int main()
{
cout << "相减:---------------------" << endl;
Date date2(2024, 1, 9);
cout << "date2:";
date2.DatePrint();
Date date3 = date2 - 679;
cout << "date2-679=" ;
date3.DatePrint();
cout <<endl<< "相差:------------------------------------" << endl;
cout << "date4:";
Date date4(2023, 9, 9);
date4.DatePrint();
cout << "date5:";
Date date5(2009, 4, 5);
date5.DatePrint();
cout << "date5-date4="<< date5 - date4 << endl;
cout << endl<<"相加:----------------------------------" << endl;
Date date(2024, 1, 9);
cout << "date:";
date.DatePrint();
Date date1 = date + 2000;
cout << "date+2000=";
date1.DatePrint();
cout <<endl<< "+= ---------------------------" << endl;
Date date6(2024, 1, 9);
cout << "date6:";
date6.DatePrint();
date6 += 138;
cout << "date6 += 138:";
date6.DatePrint();
cout << endl<<"-=-----------------------------" << endl;
Date date7(2024, 1, 9);
cout << "date7:";
date7.DatePrint();
date7 -= 138;
cout << "date7-=138:";
date7.DatePrint();
cout << endl<<"==------------------------------" << endl;
Date date8(2024, 1, 9);
cout << "date8:";
date8.DatePrint();
Date date9(2024, 1, 9);
cout << "date9:";
date9.DatePrint();
cout << "date8==date9:" << (date8 == date9) << endl;
cout << endl<<"!=------------------------------------" << endl;
Date date10(2024, 1, 9);
cout << "date10:";
date10.DatePrint();
Date date11(2024, 1, 10);
cout << "date11:";
date11.DatePrint();
cout << "date10!=date11:" << (date10 != date11) << endl;
cout << endl<<">----------------------------------------" << endl;
Date date12(2024, 1, 9);
cout << "date12:";
date12.DatePrint();
Date date13(2024, 1, 10);
cout << "date13:";
date13.DatePrint();
cout << "date10>date11:" << (date10 > date11) << endl;
cout <<endl<< "++------------------------------" << endl;
Date date14(2024, 1, 31);
cout << "date14:";
date14.DatePrint();
++date14;
cout << "++date14:";
date14.DatePrint();
cout <<endl<< "-- --------------------------" << endl;
Date date15(2024, 1, 1);
cout << "date15:";
date15.DatePrint();
--date15;
cout << "--date15:";
date15.DatePrint();
return 0;
}
结果:
完整代码
Date.h
#include<iostream>
#include<stdbool.h>
using namespace std;
bool leap(int year);
int MonthDay(int year, int month);
//闰年
class Date
{
private:
int Year;
int Month;
int Day;
public:
//构造函数
Date();
Date(int year,int month,int day);
void DatePrint();
//加法
Date operator+(int day);
//减法
Date operator-(int day);
//当前日期到1年1月1日的天数
int number1();
//相差
int operator-(Date date);
//判断相等
bool operator==(Date date);
//判断不相等
bool operator!=(Date date);
//实现-=
Date operator-=(int day);
//实现+=
Date operator+=(int day);
//判断>
bool operator>(Date date);
//判断<
bool operator<(Date date);
//判断>=
bool operator<=(Date date);
//判断>=
bool operator>=(Date date);
//++
Date operator++();
//--
Date operator--();
};
fun.cpp
#include"Date.h"
bool leap(int year)
{
//不是百年4年一闰
if (year % 100 != 0 && year % 4 == 0)
return true;
if (year % 100 == 0 && year % 400 == 0)
return true;
return false;
}
//每月多少天
int MonthDay(int year, int month)
{
int arr[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && !leap(year))
return arr[month]-1;
else
return arr[month];
}
Date::Date(int year, int month, int day)
{
Year = year;
Month = month;
Day = day;
}
void Date::DatePrint()
{
cout << Year << "/" << Month << "/" << Day << endl;
}
Date::Date()
{
Year = 1;
Month = 1;
Day = 1;
}
Date Date::operator-(int day)
{
day -= Day;
Month--;
while (day > MonthDay(Year, Day))
{
day -= MonthDay(Year, Month);
Month--;
if (Month < 1)
{
Year--;
Month = 12;
}
}
Day = MonthDay(Year, Month) - day;
return *this;
}
Date Date::operator+(int day)
{
if (day < 0)
{
return operator-(day);
}
day += Day;
while (day > MonthDay(Year, Month))
{
day -= MonthDay(Year, Month);
Month++;
if (Month > 12)
{
Year++;
Month = 1;
}
}
Day = day;
return *this;
}
int Date:: number1()
{
int year = 1,month = 1;
int thisday = 0;
while (year < Year)
{
thisday += (leap(year)) ? 366 : 365;
year++;
}
while (month < Month)
{
thisday += MonthDay(Year, month);
month++;
}
thisday += Day;
return thisday;
}
int Date::operator-(Date date)
{
return this->number1()-date.number1();
}
bool Date::operator==(Date date)
{
if(Year == date.Year&&Month==date.Month&&Day==date.Day)return true;
return false;
}
bool Date::operator!=(Date date)
{
if (*this == date)return false;
return true;
}
Date Date::operator-=(int day)
{
*this = *this - day;
return *this;
}
Date Date::operator+=(int day)
{
*this = *this + day;
return *this;
}
bool Date::operator>(Date date)
{
if (Year < date.Year)
return true;
else
{
if (Month < date.Month)
return true;
else
{
if (Day < date.Day)
return true;
else false;
}
}
}
bool Date::operator>=(Date date)
{
if (*this > date||*this == date)
return true;
else
return false;
}
bool Date::operator<(Date date)
{
if (*this >= date )return false;
return true;
}
bool Date::operator<=(Date date)
{
if (*this > date)return false;
else return true;
}
Date Date::operator++()
{
Day++;
if (Day > MonthDay(Year, Month))
{
Month++;
if (Month > 12)
{
Year++;
Month = 1;
}
Day = 1;
}
return *this;
}
Date Date::operator--()
{
if (Day == 1)
{
Month--;
if (Month == 0)
{
Year--;
Month = 12;
}
Day = MonthDay(Year, Month);
return *this;
}
}
test.cpp
#include"Date.h"
int main()
{
cout << "相减:---------------------" << endl;
Date date2(2024, 1, 9);
cout << "date2:";
date2.DatePrint();
Date date3 = date2 - 679;
cout << "date2-679=" ;
date3.DatePrint();
cout <<endl<< "相差:------------------------------------" << endl;
cout << "date4:";
Date date4(2023, 9, 9);
date4.DatePrint();
cout << "date5:";
Date date5(2009, 4, 5);
date5.DatePrint();
cout << "date5-date4="<< date5 - date4 << endl;
cout << endl<<"相加:----------------------------------" << endl;
Date date(2024, 1, 9);
cout << "date:";
date.DatePrint();
Date date1 = date + 2000;
cout << "date+2000=";
date1.DatePrint();
cout <<endl<< "+= ---------------------------" << endl;
Date date6(2024, 1, 9);
cout << "date6:";
date6.DatePrint();
date6 += 138;
cout << "date6 += 138:";
date6.DatePrint();
cout << endl<<"-=-----------------------------" << endl;
Date date7(2024, 1, 9);
cout << "date7:";
date7.DatePrint();
date7 -= 138;
cout << "date7-=138:";
date7.DatePrint();
cout << endl<<"==------------------------------" << endl;
Date date8(2024, 1, 9);
cout << "date8:";
date8.DatePrint();
Date date9(2024, 1, 9);
cout << "date9:";
date9.DatePrint();
cout << "date8==date9:" << (date8 == date9) << endl;
cout << endl<<"!=------------------------------------" << endl;
Date date10(2024, 1, 9);
cout << "date10:";
date10.DatePrint();
Date date11(2024, 1, 10);
cout << "date11:";
date11.DatePrint();
cout << "date10!=date11:" << (date10 != date11) << endl;
cout << endl<<">----------------------------------------" << endl;
Date date12(2024, 1, 9);
cout << "date12:";
date12.DatePrint();
Date date13(2024, 1, 10);
cout << "date13:";
date13.DatePrint();
cout << "date10>date11:" << (date10 > date11) << endl;
cout <<endl<< "++------------------------------" << endl;
Date date14(2024, 1, 31);
cout << "date14:";
date14.DatePrint();
++date14;
cout << "++date14:";
date14.DatePrint();
cout <<endl<< "-- --------------------------" << endl;
Date date15(2024, 1, 1);
cout << "date15:";
date15.DatePrint();
--date15;
cout << "--date15:";
date15.DatePrint();
return 0;
}