🌈个人主页:羽晨同学
💫个人格言:“成为自己未来的主人~”
日期类的实现
首先我们先来看看日期类实现的代码
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
class Date
{
public:
Date(int year = 2024, int month = 5, int day = 17);
void Print();
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int monthDayArray[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && (year % 4 == 0 && year % 100 != 0))
{
return 29;
}
else
{
return monthDayArray[month];
}
}
bool CheckDate();
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-=(int day);
Date operator-(int day);
int operator-(const Date& d);
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
bool Date::CheckDate()
{
if (_month < 1 || _month>12
|| _day<1 || _day>GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
cout << "日期非法" << endl;
}
}
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool Date::operator<(const Date& d)
{
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 true;
}
bool Date::operator<=(const Date& d)
{
return *this < d || *this == d;
}
bool Date::operator>=(const Date& d)
{
return !(*this < d);
}
bool Date::operator==(const Date& d)
{
return _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);
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_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)
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
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);
*this -= 1;
return tmp;
}
int Date::operator-(const Date& d)
{
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;
}
#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
//class Date
//{
//public:
// Date(int year = 2024, int month = 5, int day = 17)
// {
// _year = year;
// _month = month;
// _day = day;
// }
// //前置++
// Date& operator++()
// {
// _day += 1;
// return *this;
// }
// //后置++
// Date operator++(int)
// {
// Date tmp(*this);
// _day += 1;
// return tmp;
// }
// void Print()
// {
// cout << _year << "-" << _month << "-" << _day << endl;
// }
//private:
// int _year;
// int _month;
// int _day;
//};
//int main()
//{
// Date d1(2024, 1, 1);
// Date d2(2024, 1, 1);
// Date d3=d1++;
// Date d4=++d2;
// d3.Print();
// d4.Print();
//
// return 0;
//}
void TestDate1()
{
Date d1(2024, 4, 14);
Date d2 = d1 + 30000;
d1.Print();
d2.Print();
//int n = (d2 - d1);
//cout << n;
}
void TestDate2()
{
Date d1(2024, 4, 14);
Date d2(2034, 4, 14);
int n = d1 - d2;
cout << n << endl;
}
void TestDate3()
{
Date d1(2024, 1, 1);
Date d2(2024, 2, 4);
//cout<<(d1 > d2)<<endl;
d1 < d2;
}
int main()
{
//TestDate1();
//TestDate2();
TestDate3();
return 0;
}
上面是日期类的全部实现代码,下面我们讲解一下这些功能背后的实现逻辑是什么?
首先,我们先来到,那几个判断大小功能的代码部分
bool Date::operator<(const Date& d)
{
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 true;
}
bool Date::operator<=(const Date& d)
{
return *this < d || *this == d;
}
bool Date::operator>=(const Date& d)
{
return !(*this < d);
}
bool Date::operator==(const Date& d)
{
return _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);
}
大家可以看到,在实现日期类的判断功能的时候,我们切切实实手敲的只有等于和大于这两个部分,其余的功能都是这两个功能的组合,并结合其他的功能诞生出了更多的功能。
在这其中,基础的实现逻辑,其实就是大于的话,我们先比较年,然后比较月和日,从反面来进行判断会减少情况的判断量。
等于的话是年月日分别相等。
接下来我们来看看这几个功能
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_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)
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
在这几个功能当中,其实实现逻辑十分相似,我们可以总共分为两类,一个是-和-=,一个是+和+=,这两个部分
在这其中更为重要的是-=和+=的实现逻辑,因为要涉及到是否需要进位的操作,
在这前面,我们用数组,在数组当中放入了全年每个月的天数,用来进行比较。
然后后面的+和-,就运用了+=和-=的操作逻辑。
而-=和+=由于是直接对变量进行操作,所以我们可以用&来进行,可以提高效率。
下面的这个部分是前置++和后置++,前置--和后置--,这几个操作,我们在上一篇文章中有详细讲解,大家可以看看。
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);
*this -= 1;
return tmp;
}
最后一部分是两个日期相减所得到的天数。
int Date::operator-(const Date& d)
{
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;
}
在这个当中,需要关注的重点是flag的设立,大家可以体会一下其中的妙处。
好了,我们本期的文章就到这里。
下一篇我们再见。