• 日期类的实现


    > 作者简介:დ旧言~,目前大二,现在学习Java,c,c++,Python等
    > 座右铭:松树千年终是朽,槿花一日自为荣。

    > 目标:能手撕日期类

    > 毒鸡汤:枯木逢春犹再发,人无两度再少年。
    > 望小伙伴们点赞👍收藏✨加关注哟💕💕 

    🌟前言

            前面我们已经学习了c++类和对象上和下,应该有人会有疑惑,为什么先不讲c++类和对象下,因为本篇博客是为了总结我们刚学习的知识,把我们学的知识用起来。那我们学习日期类有啥子用捏?咱们看一个图解:



    这个日期类可以实现日期加减,那咱们上路咯。

    主体  

            咱们从三大板块学习,建立日期类(Date.h),日期类函数的实现(Date.cpp),主函数的实现(Test.cpp)。数据结构的顺序表玩法差不多,咱们看看总体框架:



    🌙建立日期类(Date.h)

    💦①:为了使用c++库中内容,我们打开库中的命名空间 using namespace std(记得包含头文件)

    💦②:定义一个类,类有公有和私有,公有我们实现一些函数,私有我们定义成员变量。

    💦③:在公有的函数中,我们有打印日期,判断闰平年,构造函数,赋值运算重载。

    1. 打印日期的函数还是比较简单的
    2. 判断闰平年(闰年二月29天,平年二月28天)
    3. 构造函数(初始化日期)
    4. 赋值运算重载(实现日期加减)

    咱们看看代码:

    1. //包含头文件
    2. #include
    3. #include
    4. using namespace std;
    5. //定义类
    6. class Date
    7. {
    8. public:
    9. //打印
    10. void Print();
    11. //判断闰年(29)平年(28)
    12. int GetMonthDay(int year, int month);
    13. //判断日期是否合法(构造函数)
    14. Date(int year = 1, int month = 1, int day = 1);
    15. //日期的加减 (赋值运算重载)
    16. bool operator==(const Date& d2);
    17. bool operator!=(const Date& d2);
    18. bool operator>(const Date& d2);
    19. bool operator<(const Date& d2);
    20. bool operator>=(const Date& d2);
    21. bool operator<=(const Date& d2);
    22. Date& operator+=(int day);
    23. Date operator+(int day);
    24. Date& operator-=(int day);
    25. Date operator-(int day);
    26. int operator-(const Date& d);
    27. Date& operator++();
    28. Date operator++(int);
    29. Date& operator--();
    30. Date operator--(int);
    31. //定义成员变量
    32. private:
    33. int _year;
    34. int _month;
    35. int _day;
    36. };

    赋值运算重载我不细讲,里面的参数还返回值到(Data.cpp)中细解。

    🌙日期类函数的实现(Date.cpp)

    💤初始化(构造函数)

            这个函数的实现主要是为了在主函数(Test.cpp)中初始化日期,还有就是判断日期初始化是否正确。

    💦①:因为我们在cpp文件中,要使用构造函数,需要Date::Date(int year, int month, int day)

    💦②:之后就开始赋值。

    💦③:判断日期是否合理。

    咱们看看代码:

    1. //判断日期是否合法(构造函数)(初始化)
    2. Date::Date(int year, int month, int day)
    3. {
    4. //赋值
    5. _year = year;
    6. _month = month;
    7. _day = day;
    8. //判断日期是否合法
    9. if (_year < 1 || _month < 1 || _month > 12 ||
    10. _day < 1 || _day > GetMonthDay(_year, _month))
    11. {
    12. Print();
    13. cout << "日期非法" << endl;
    14. }
    15. }

    这里调用 GetMonthDay(_year, _month),后面会细讲,只要记住这里是判断初始化的日期的天是否正确。

    💤判断闰平年

    这个函数主要是为了判断日期是否合理,并且判断闰平年中二月份的天数问题。

    💦①:int Date::GetMonthDay(int year, int month)中的Date::不能忘

    💦②:断言(判断日期是否合理)

    💦③:判断闰平年(闰年年份可被4整除但不能被100整除,或者能被400整除)

    咱们看看代码:

    1. //判断闰年(29)平年(28)
    2. int Date::GetMonthDay(int year, int month)
    3. {
    4. //断言(判断年月给的是否正确)
    5. assert(year >= 1 && month >= 1 && month <= 12);
    6. //定义一年中月的天数
    7. int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    8. //判断
    9. if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    10. {
    11. return 29;
    12. }
    13. return arr[month];
    14. }

    💤打印日期

    这个函数要是不会就脱出去打板子。

    咱们看看代码:

    1. //打印日期
    2. void Date::Print()
    3. {
    4. cout << _year << "-" << _month << "-" << _day << endl;
    5. }

    💤判断日期大小(重点)

    看图解:



    我们就直接把全部代码上:

    1. //相等
    2. bool Date::operator==(const Date& d2)
    3. {
    4. return _year == d2._year
    5. && _month == d2._month
    6. && _day == d2._day;
    7. }
    8. //不相等( 拷贝bool Date::operator == (const Date& d2) )
    9. bool Date::operator!=(const Date& d2)
    10. {
    11. return !(*this == d2);
    12. }
    13. //大于
    14. bool Date::operator>(const Date& d2)
    15. {
    16. //判断
    17. if (_year > d2._year)
    18. {
    19. return true;
    20. }
    21. else if (_year == d2._year && _month > d2._month)
    22. {
    23. return true;
    24. }
    25. else if (_year == d2._year && _month == d2._month
    26. && _day > d2._day)
    27. {
    28. return true;
    29. }
    30. //其它情况为false
    31. return false;
    32. }
    33. //小于( 拷贝bool Date::operator > (const Date& d2) )
    34. bool Date::operator<(const Date& d2)
    35. {
    36. return !(*this >= d2);
    37. }
    38. //大于等于( 拷贝bool Date::operator > (const Date& d2) )
    39. bool Date::operator>=(const Date& d2)
    40. {
    41. return *this > d2 || *this == d2;
    42. }
    43. //小于等于( 拷贝bool Date::operator > (const Date& d2) )
    44. bool Date::operator<=(const Date& d2)
    45. {
    46. return !(*this > d2);
    47. }

    再次看图解:



    💤日期的加减(重点)

    我们就直接把全部代码上:

    1. //加天数
    2. Date& Date::operator+=(int day)
    3. {
    4. //如果加的天数为负数
    5. if (day < 0)
    6. {
    7. return *this = *this - (-day);
    8. }
    9. _day += day;
    10. //判断是否年月要加
    11. while (_day > GetMonthDay(_year, _month))
    12. {
    13. _day -= GetMonthDay(_year, _month);
    14. ++_month;
    15. if (_month == 13)
    16. {
    17. _year++;
    18. _month = 1;
    19. }
    20. }
    21. return *this;
    22. }
    23. //加天数但原来的*this不变
    24. Date Date::operator+(int day)
    25. {
    26. Date tmp(*this);
    27. tmp += day;
    28. return tmp;
    29. }
    30. //减天数
    31. Date& Date::operator-=(int day)
    32. {
    33. //如果减的天数为负数
    34. if (day < 0)
    35. {
    36. return *this += (-day);
    37. }
    38. _day -= day;
    39. while (_day <= 0)
    40. {
    41. --_month;
    42. if (_month == 0)
    43. {
    44. --_year;
    45. _month = 12;
    46. }
    47. _day += GetMonthDay(_year, _month);
    48. }
    49. return *this;
    50. }
    51. //减天数但原来的*this不变
    52. Date Date::operator-(int day)
    53. {
    54. Date tmp(*this);
    55. tmp -= day;
    56. return tmp;
    57. }

    上面四个赋值运算重载,相互复用,这就不再多说了。

    💤日期的加加减减(重点)

    这里需要知道,后置加加(先赋值,再加加),前置加加(先加加,后赋值)

    上代码:

    1. // ++d1
    2. Date& Date::operator++()
    3. {
    4. *this += 1;
    5. return *this;
    6. }
    7. // d1++
    8. Date Date::operator++(int)
    9. {
    10. Date tmp(*this);
    11. *this += 1;
    12. return tmp;
    13. }
    14. // --d1
    15. Date& Date::operator--()
    16. {
    17. *this -= 1;
    18. return *this;
    19. }
    20. // d1--
    21. Date Date::operator--(int)
    22. {
    23. Date tmp(*this);
    24. *this -= 1;
    25. return tmp;
    26. }
    27. // d1 - d2
    28. int Date::operator-(const Date& d)
    29. {
    30. // 假设左大右小
    31. int flag = 1;
    32. Date max = *this;
    33. Date min = d;
    34. // 假设错了,左小右大
    35. if (*this < d)
    36. {
    37. max = d;
    38. min = *this;
    39. flag = -1;
    40. }
    41. int n = 0;
    42. while (min != max)
    43. {
    44. ++min;
    45. ++n;
    46. }
    47. return n * flag;
    48. }

    🌙主函数的实现(Test.cpp)

    这里就没啥好说的,只要记住使用类需要这样调用:Date d1(2023, 10, 27);d1.Print()

    上代码:

    1. //包含头文件
    2. #include"Date.h"
    3. void TestDate1()
    4. {
    5. Date d1(2023, 10, 27);
    6. d1.Print();
    7. Date d2(2023, 10, 27);
    8. d2.Print();
    9. bool ret1 = (d1 == d2);
    10. cout << ret1 << endl;
    11. bool ret2 = (d1 != d2);
    12. cout << ret2 << endl;
    13. bool ret3 = (d1 > d2);
    14. cout << ret3 << endl;
    15. bool ret4 = (d1 < d2);
    16. cout << ret4 << endl;
    17. bool ret5 = (d1 >= d2);
    18. cout << ret5 << endl;
    19. bool ret6 = (d1 <= d2);
    20. cout << ret6 << endl;
    21. }
    22. int main()
    23. {
    24. TestDate1();
    25. return 0;
    26. }

    🌠建立日期类(Date.h)全部代码

    1. //包含头文件
    2. #include
    3. #include
    4. using namespace std;
    5. //定义类
    6. class Date
    7. {
    8. public:
    9. //打印
    10. void Print();
    11. //判断闰年(29)平年(28)
    12. int GetMonthDay(int year, int month);
    13. //判断日期是否合法(构造函数)
    14. Date(int year = 1, int month = 1, int day = 1);
    15. //日期的加减 (赋值运算重载)
    16. bool operator==(const Date& d2);
    17. bool operator!=(const Date& d2);
    18. bool operator>(const Date& d2);
    19. bool operator<(const Date& d2);
    20. bool operator>=(const Date& d2);
    21. bool operator<=(const Date& d2);
    22. Date& operator+=(int day);
    23. Date operator+(int day);
    24. Date& operator-=(int day);
    25. Date operator-(int day);
    26. int operator-(const Date& d);
    27. Date& operator++();
    28. Date operator++(int);
    29. Date& operator--();
    30. Date operator--(int);
    31. //定义成员变量
    32. private:
    33. int _year;
    34. int _month;
    35. int _day;
    36. };

    🌠日期类函数的实现(Date.cpp)全部代码

    1. //包含头文件
    2. #include"Date.h";
    3. //打印日期
    4. void Date::Print()
    5. {
    6. cout << _year << "-" << _month << "-" << _day << endl;
    7. }
    8. //判断日期是否合法(构造函数)(初始化)
    9. Date::Date(int year, int month, int day)
    10. {
    11. //赋值
    12. _year = year;
    13. _month = month;
    14. _day = day;
    15. //判断日期是否合法
    16. if (_year < 1 || _month < 1 || _month > 12 ||
    17. _day < 1 || _day > GetMonthDay(_year, _month))
    18. {
    19. Print();
    20. cout << "日期非法" << endl;
    21. }
    22. }
    23. //判断闰年(29)平年(28)
    24. int Date::GetMonthDay(int year, int month)
    25. {
    26. //断言(判断年月给的是否正确)
    27. assert(year >= 1 && month >= 1 && month <= 12);
    28. //定义一年中月的天数
    29. int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    30. //判断
    31. if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    32. {
    33. return 29;
    34. }
    35. return arr[month];
    36. }
    37. //相等
    38. bool Date::operator==(const Date& d2)
    39. {
    40. return _year == d2._year
    41. && _month == d2._month
    42. && _day == d2._day;
    43. }
    44. //不相等( 拷贝bool Date::operator == (const Date& d2) )
    45. bool Date::operator!=(const Date& d2)
    46. {
    47. return !(*this == d2);
    48. }
    49. //大于
    50. bool Date::operator>(const Date& d2)
    51. {
    52. //判断
    53. if (_year > d2._year)
    54. {
    55. return true;
    56. }
    57. else if (_year == d2._year && _month > d2._month)
    58. {
    59. return true;
    60. }
    61. else if (_year == d2._year && _month == d2._month
    62. && _day > d2._day)
    63. {
    64. return true;
    65. }
    66. //其它情况为false
    67. return false;
    68. }
    69. //小于( 拷贝bool Date::operator > (const Date& d2) )
    70. bool Date::operator<(const Date& d2)
    71. {
    72. return !(*this >= d2);
    73. }
    74. //大于等于( 拷贝bool Date::operator > (const Date& d2) )
    75. bool Date::operator>=(const Date& d2)
    76. {
    77. return *this > d2 || *this == d2;
    78. }
    79. //小于等于( 拷贝bool Date::operator > (const Date& d2) )
    80. bool Date::operator<=(const Date& d2)
    81. {
    82. return !(*this > d2);
    83. }
    84. //加天数
    85. Date& Date::operator+=(int day)
    86. {
    87. //如果加的天数为负数
    88. if (day < 0)
    89. {
    90. return *this = *this - (-day);
    91. }
    92. _day += day;
    93. //判断是否年月要加
    94. while (_day > GetMonthDay(_year, _month))
    95. {
    96. _day -= GetMonthDay(_year, _month);
    97. ++_month;
    98. if (_month == 13)
    99. {
    100. _year++;
    101. _month = 1;
    102. }
    103. }
    104. return *this;
    105. }
    106. //加天数但原来的*this不变
    107. Date Date::operator+(int day)
    108. {
    109. Date tmp(*this);
    110. tmp += day;
    111. return tmp;
    112. }
    113. //减天数
    114. Date& Date::operator-=(int day)
    115. {
    116. //如果减的天数为负数
    117. if (day < 0)
    118. {
    119. return *this += (-day);
    120. }
    121. _day -= day;
    122. while (_day <= 0)
    123. {
    124. --_month;
    125. if (_month == 0)
    126. {
    127. --_year;
    128. _month = 12;
    129. }
    130. _day += GetMonthDay(_year, _month);
    131. }
    132. return *this;
    133. }
    134. //减天数但原来的*this不变
    135. Date Date::operator-(int day)
    136. {
    137. Date tmp(*this);
    138. tmp -= day;
    139. return tmp;
    140. }
    141. // ++d1
    142. Date& Date::operator++()
    143. {
    144. *this += 1;
    145. return *this;
    146. }
    147. // d1++
    148. Date Date::operator++(int)
    149. {
    150. Date tmp(*this);
    151. *this += 1;
    152. return tmp;
    153. }
    154. // --d1
    155. Date& Date::operator--()
    156. {
    157. *this -= 1;
    158. return *this;
    159. }
    160. // d1--
    161. Date Date::operator--(int)
    162. {
    163. Date tmp(*this);
    164. *this -= 1;
    165. return tmp;
    166. }
    167. // d1 - d2
    168. int Date::operator-(const Date& d)
    169. {
    170. // 假设左大右小
    171. int flag = 1;
    172. Date max = *this;
    173. Date min = d;
    174. // 假设错了,左小右大
    175. if (*this < d)
    176. {
    177. max = d;
    178. min = *this;
    179. flag = -1;
    180. }
    181. int n = 0;
    182. while (min != max)
    183. {
    184. ++min;
    185. ++n;
    186. }
    187. return n * flag;
    188. }

    🌠主函数的实现(Test.cpp)全部代码

    1. //包含头文件
    2. #include"Date.h"
    3. void TestDate1()
    4. {
    5. Date d1(2023, 10, 27);
    6. d1.Print();
    7. Date d2(2023, 10, 27);
    8. d2.Print();
    9. bool ret1 = (d1 == d2);
    10. cout << ret1 << endl;
    11. bool ret2 = (d1 != d2);
    12. cout << ret2 << endl;
    13. bool ret3 = (d1 > d2);
    14. cout << ret3 << endl;
    15. bool ret4 = (d1 < d2);
    16. cout << ret4 << endl;
    17. bool ret5 = (d1 >= d2);
    18. cout << ret5 << endl;
    19. bool ret6 = (d1 <= d2);
    20. cout << ret6 << endl;
    21. }
    22. int main()
    23. {
    24. TestDate1();
    25. return 0;
    26. }

    🌟结束语

           今天内容就到这里啦,时间过得很快,大家沉下心来好好学习,会有一定的收获的,大家多多坚持,嘻嘻,成功路上注定孤独,因为坚持的人不多。那请大家举起自己的小说手给博主一键三连,有你们的支持是我最大的动力💞💞💞,回见。

  • 相关阅读:
    【附源码】计算机毕业设计SSM商品限时秒杀系统
    读写/dev/kmsg设备编程实例
    HTML期末学生大作业-拯救宠物网页作业html+css
    做个小工具显示UE里地形的高度图和权重图
    宝塔面板使用Supervisor进程守护插件,配置守护Mysql的操作教程。
    CREO:CREO软件之装配设计界面的简介、装配图设计流程、案例应用(图文教程)之详细攻略
    scau CSAPP 第二章家庭作业
    回调函数 事件回调 异步事件 异步函数 JS事件流 事件的捕获模式
    PTA: 字符串的模式匹配
    医疗器械行业迎来黄金十年,集团采购系统助力企业把握机遇,实现高质量发展
  • 原文地址:https://blog.csdn.net/AAlykk/article/details/134084062