> 作者简介:დ旧言~,目前大二,现在学习Java,c,c++,Python等
> 座右铭:松树千年终是朽,槿花一日自为荣。
> 目标:能手撕日期类。
> 毒鸡汤:枯木逢春犹再发,人无两度再少年。
> 望小伙伴们点赞👍收藏✨加关注哟💕💕
🌟前言
前面我们已经学习了c++类和对象上和下,应该有人会有疑惑,为什么先不讲c++类和对象下,因为本篇博客是为了总结我们刚学习的知识,把我们学的知识用起来。那我们学习日期类有啥子用捏?咱们看一个图解:
这个日期类可以实现日期加减,那咱们上路咯。
⭐主体
咱们从三大板块学习,建立日期类(Date.h),日期类函数的实现(Date.cpp),主函数的实现(Test.cpp)。和数据结构的顺序表玩法差不多,咱们看看总体框架:
🌙建立日期类(Date.h)
💦①:为了使用c++库中内容,我们打开库中的命名空间 using namespace std(记得包含头文件)
💦②:定义一个类,类有公有和私有,公有我们实现一些函数,私有我们定义成员变量。
💦③:在公有的函数中,我们有打印日期,判断闰平年,构造函数,赋值运算重载。
- 打印日期的函数还是比较简单的
- 判断闰平年(闰年二月29天,平年二月28天)
- 构造函数(初始化日期)
- 赋值运算重载(实现日期加减)
咱们看看代码:
//包含头文件 #include<iostream> #include<assert.h> using namespace std; //定义类 class Date { public: //打印 void Print(); //判断闰年(29)平年(28) int GetMonthDay(int year, int month); //判断日期是否合法(构造函数) Date(int year = 1, int month = 1, int day = 1); //日期的加减 (赋值运算重载) bool operator==(const Date& d2); bool operator!=(const Date& d2); bool operator>(const Date& d2); bool operator<(const Date& d2); bool operator>=(const Date& d2); bool operator<=(const Date& d2); 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; };
赋值运算重载我不细讲,里面的参数还返回值到(Data.cpp)中细解。
🌙日期类函数的实现(Date.cpp)
💤初始化(构造函数)
这个函数的实现主要是为了在主函数(Test.cpp)中初始化日期,还有就是判断日期初始化是否正确。
💦①:因为我们在cpp文件中,要使用构造函数,需要Date::Date(int year, int month, int day)
💦②:之后就开始赋值。
💦③:判断日期是否合理。
咱们看看代码:
//判断日期是否合法(构造函数)(初始化) Date::Date(int year, int month, int day) { //赋值 _year = year; _month = month; _day = day; //判断日期是否合法 if (_year < 1 || _month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month)) { Print(); cout << "日期非法" << endl; } }
这里调用 GetMonthDay(_year, _month),后面会细讲,只要记住这里是判断初始化的日期的天是否正确。
💤判断闰平年
这个函数主要是为了判断日期是否合理,并且判断闰平年中二月份的天数问题。
💦①:int Date::GetMonthDay(int year, int month)中的Date::不能忘
💦②:断言(判断日期是否合理)
💦③:判断闰平年(闰年年份可被4整除但不能被100整除,或者能被400整除)
咱们看看代码:
//判断闰年(29)平年(28) int Date::GetMonthDay(int year, int month) { //断言(判断年月给的是否正确) assert(year >= 1 && month >= 1 && month <= 12); //定义一年中月的天数 int arr[13] = { 0,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; } return arr[month]; }
💤打印日期
这个函数要是不会就脱出去打板子。
咱们看看代码:
//打印日期 void Date::Print() { cout << _year << "-" << _month << "-" << _day << endl; }
💤判断日期大小(重点)
看图解:
我们就直接把全部代码上:
//相等 bool Date::operator==(const Date& d2) { return _year == d2._year && _month == d2._month && _day == d2._day; } //不相等( 拷贝bool Date::operator == (const Date& d2) ) bool Date::operator!=(const Date& d2) { return !(*this == d2); } //大于 bool Date::operator>(const Date& d2) { //判断 if (_year > d2._year) { return true; } else if (_year == d2._year && _month > d2._month) { return true; } else if (_year == d2._year && _month == d2._month && _day > d2._day) { return true; } //其它情况为false return false; } //小于( 拷贝bool Date::operator > (const Date& d2) ) bool Date::operator<(const Date& d2) { return !(*this >= d2); } //大于等于( 拷贝bool Date::operator > (const Date& d2) ) bool Date::operator>=(const Date& d2) { return *this > d2 || *this == d2; } //小于等于( 拷贝bool Date::operator > (const Date& d2) ) bool Date::operator<=(const Date& d2) { return !(*this > d2); }
再次看图解:
💤日期的加减(重点)
我们就直接把全部代码上:
//加天数 Date& Date::operator+=(int day) { //如果加的天数为负数 if (day < 0) { return *this = *this - (-day); } _day += day; //判断是否年月要加 while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); ++_month; if (_month == 13) { _year++; _month = 1; } } return *this; } //加天数但原来的*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) { --_year; _month = 12; } _day += GetMonthDay(_year, _month); } return *this; } //减天数但原来的*this不变 Date Date::operator-(int day) { Date tmp(*this); tmp -= day; return tmp; }
上面四个赋值运算重载,相互复用,这就不再多说了。
💤日期的加加减减(重点)
这里需要知道,后置加加(先赋值,再加加),前置加加(先加加,后赋值)
上代码:
// ++d1 Date& Date::operator++() { *this += 1; return *this; } // d1++ Date Date::operator++(int) { Date tmp(*this); *this += 1; return tmp; } // --d1 Date& Date::operator--() { *this -= 1; return *this; } // d1-- Date Date::operator--(int) { Date tmp(*this); *this -= 1; return tmp; } // d1 - d2 int Date::operator-(const Date& d) { // 假设左大右小 int flag = 1; Date max = *this; Date min = d; // 假设错了,左小右大 if (*this < d) { max = d; min = *this; flag = -1; } int n = 0; while (min != max) { ++min; ++n; } return n * flag; }
🌙主函数的实现(Test.cpp)
这里就没啥好说的,只要记住使用类需要这样调用:Date d1(2023, 10, 27);d1.Print()
上代码:
//包含头文件 #include"Date.h" void TestDate1() { Date d1(2023, 10, 27); d1.Print(); Date d2(2023, 10, 27); d2.Print(); bool ret1 = (d1 == d2); cout << ret1 << endl; bool ret2 = (d1 != d2); cout << ret2 << endl; bool ret3 = (d1 > d2); cout << ret3 << endl; bool ret4 = (d1 < d2); cout << ret4 << endl; bool ret5 = (d1 >= d2); cout << ret5 << endl; bool ret6 = (d1 <= d2); cout << ret6 << endl; } int main() { TestDate1(); return 0; }
🌠建立日期类(Date.h)全部代码
//包含头文件 #include<iostream> #include<assert.h> using namespace std; //定义类 class Date { public: //打印 void Print(); //判断闰年(29)平年(28) int GetMonthDay(int year, int month); //判断日期是否合法(构造函数) Date(int year = 1, int month = 1, int day = 1); //日期的加减 (赋值运算重载) bool operator==(const Date& d2); bool operator!=(const Date& d2); bool operator>(const Date& d2); bool operator<(const Date& d2); bool operator>=(const Date& d2); bool operator<=(const Date& d2); 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; };
🌠日期类函数的实现(Date.cpp)全部代码
//包含头文件 #include"Date.h"; //打印日期 void Date::Print() { cout << _year << "-" << _month << "-" << _day << endl; } //判断日期是否合法(构造函数)(初始化) Date::Date(int year, int month, int day) { //赋值 _year = year; _month = month; _day = day; //判断日期是否合法 if (_year < 1 || _month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month)) { Print(); cout << "日期非法" << endl; } } //判断闰年(29)平年(28) int Date::GetMonthDay(int year, int month) { //断言(判断年月给的是否正确) assert(year >= 1 && month >= 1 && month <= 12); //定义一年中月的天数 int arr[13] = { 0,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; } return arr[month]; } //相等 bool Date::operator==(const Date& d2) { return _year == d2._year && _month == d2._month && _day == d2._day; } //不相等( 拷贝bool Date::operator == (const Date& d2) ) bool Date::operator!=(const Date& d2) { return !(*this == d2); } //大于 bool Date::operator>(const Date& d2) { //判断 if (_year > d2._year) { return true; } else if (_year == d2._year && _month > d2._month) { return true; } else if (_year == d2._year && _month == d2._month && _day > d2._day) { return true; } //其它情况为false return false; } //小于( 拷贝bool Date::operator > (const Date& d2) ) bool Date::operator<(const Date& d2) { return !(*this >= d2); } //大于等于( 拷贝bool Date::operator > (const Date& d2) ) bool Date::operator>=(const Date& d2) { return *this > d2 || *this == d2; } //小于等于( 拷贝bool Date::operator > (const Date& d2) ) bool Date::operator<=(const Date& d2) { return !(*this > d2); } //加天数 Date& Date::operator+=(int day) { //如果加的天数为负数 if (day < 0) { return *this = *this - (-day); } _day += day; //判断是否年月要加 while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); ++_month; if (_month == 13) { _year++; _month = 1; } } return *this; } //加天数但原来的*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) { --_year; _month = 12; } _day += GetMonthDay(_year, _month); } return *this; } //减天数但原来的*this不变 Date Date::operator-(int day) { Date tmp(*this); tmp -= day; return tmp; } // ++d1 Date& Date::operator++() { *this += 1; return *this; } // d1++ Date Date::operator++(int) { Date tmp(*this); *this += 1; return tmp; } // --d1 Date& Date::operator--() { *this -= 1; return *this; } // d1-- Date Date::operator--(int) { Date tmp(*this); *this -= 1; return tmp; } // d1 - d2 int Date::operator-(const Date& d) { // 假设左大右小 int flag = 1; Date max = *this; Date min = d; // 假设错了,左小右大 if (*this < d) { max = d; min = *this; flag = -1; } int n = 0; while (min != max) { ++min; ++n; } return n * flag; }
🌠主函数的实现(Test.cpp)全部代码
//包含头文件 #include"Date.h" void TestDate1() { Date d1(2023, 10, 27); d1.Print(); Date d2(2023, 10, 27); d2.Print(); bool ret1 = (d1 == d2); cout << ret1 << endl; bool ret2 = (d1 != d2); cout << ret2 << endl; bool ret3 = (d1 > d2); cout << ret3 << endl; bool ret4 = (d1 < d2); cout << ret4 << endl; bool ret5 = (d1 >= d2); cout << ret5 << endl; bool ret6 = (d1 <= d2); cout << ret6 << endl; } int main() { TestDate1(); return 0; }
🌟结束语
今天内容就到这里啦,时间过得很快,大家沉下心来好好学习,会有一定的收获的,大家多多坚持,嘻嘻,成功路上注定孤独,因为坚持的人不多。那请大家举起自己的小说手给博主一键三连,有你们的支持是我最大的动力💞💞💞,回见。