👑个人主页:啊Q闻
🎇收录专栏:《C++》
🎉我自会去见我的山
💡感谢阅读,欢迎关注,点赞,收藏,评论💡
前言
实现日期的相加减,这个代码有助于我们进一步理解运算符重载的相关知识,也是对C++基础知识的一个运用。
代码思路
1.实现日期的哪些运算?
实现日期类的运算,我们要实现那些运算,是要根据实际情况判断哪些运算是有实际意义的,如日期加减天数是有意义,日期减日期可以求两个日期间的间隔天数是有意义的,但是日期相加是没有意义的,所以,我们要写的运算有:
2.如何实现日期加减天数?
日期减天数类似于上面。
3.利用复用可以大大减少代码量 ,问题在于实现谁复用谁?
减法类似上面。
4.前置加加和后置加加如何区分?
为了区分,构成重载,给后置++强行增加一个int形参,这里不需要写形参名,这个参数仅仅是为了区分前置和后置。
5.如何实现日期减日期?
需要注意的是:
下面代码中的*this<d中的< , min!=max中的!=,它是在实现日期类的比较,日期类的比较需要我们自己实现。
代码实现
Date.h中:
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
//初始化
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//打印
void Print();
//判断二月是否为闰二月要反复调用,所以我们将其定为内联函数
int GetMonthDay(int year, int month)const
{
assert(month > 0 && month < 13);
static int MonthArray[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
if ((month == 2) && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
{
return 29;
}
else
{
return MonthArray[month];
}
}
//运算的实现
Date operator+(int day)const;
Date operator-(int day)const;
Date& operator+=(int day);
Date& operator-=(int day);
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
int operator-(const Date& d) const;
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator==(const Date& d) const;
bool operator!=(const Date& d) const;
private:
int _year;
int _month;
int _day;
};
.cpp中:
#include"Date.h"
void Date:: Print()
{
cout << _year << "-" << _month << "-" << _day;
}
//独立实现加
Date Date::operator+(int day)const
{
Date tmp = *this;
tmp._day += day;
while(tmp._day > GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);
tmp._month++;
if (tmp._month == 13)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
//独立实现
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this += (-day);
return *this;
}
_day+= day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
//复用加等
Date Date::operator+(int day)const
{
Date tmp = *this;
tmp +=day;
return tmp;
}
//复用+
Date& Date::operator+=(int day)
{
*this += day;
return *this;
}
Date Date::operator-(int day)const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date& Date:: operator-=(int day)
{
if (day < 0)
{
*this += (-day);
return *this;
}
_day -= day;
while (_day <=0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date& Date:: operator++()
{
*this += 1;
return *this;
}
Date Date:: operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date&Date:: operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp;
*this -= 1;
return tmp;
}
bool Date::operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return true;
}
else if (_month == d._month)
{
return _day < d._day;
}
}
return false;
}
// d1 <= d2
bool Date::operator<=(const Date& d) const
{
return *this < d || *this == d;
}
bool Date::operator>(const Date& d) const
{
return !(*this <= d);
}
bool Date::operator>=(const Date& d) const
{
return !(*this < d);
}
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
int Date::operator-(const Date& d)const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
希望我的博客能对你有帮助,铁子们可以自己去实现实现。