• 日期类的实现


    目录

    Date.h

    Test.cpp

    测试代码Test.cpp


    日期类的实现

    代码分享

    Date.h

    1. #pragma once
    2. #include
    3. using namespace std;
    4. #include
    5. class Date
    6. {
    7. //友元函数声明
    8. friend ostream& operator<<(ostream& out, Date& d);
    9. friend istream& operator>>(istream& in, Date& d);
    10. public:
    11. // 全缺省的构造函数
    12. Date(int year = 1, int month = 1, int day = 1);
    13. // 拷贝构造函数
    14. Date (const Date& d);
    15. // 赋值运算符重载
    16. // d2 = d3; -> d2.operator=(&d2, d3);
    17. Date& operator=(const Date& d);
    18. // 析构函数
    19. ~Date();
    20. // 直接定义类里面,他默认是inline
    21. // 频繁调用
    22. //得到月的天数
    23. int GetMonthDay(int year, int month)
    24. {
    25. assert(month>0&& month<13);
    26. static int MonthDay[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    27. if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))//千万不能少括号
    28. {
    29. return 29;
    30. }
    31. return MonthDay[month];
    32. }
    33. bool CheckDate();
    34. bool operator == (const Date& d)const;
    35. bool operator != (const Date& d)const;
    36. bool operator <(const Date& d)const;
    37. bool operator > (const Date& d)const;
    38. bool operator <=(const Date& d)const;
    39. bool operator >= (const Date& d)const;
    40. Date& operator += (int day);
    41. Date operator + (int day)const;
    42. Date& operator ++ ();
    43. // 为了区分,构成重载,给后置++,强行增加了一个int形参
    44. // 这里不需要写形参名,因为接收值是多少不重要,也不需要用
    45. // 这个参数仅仅是为了跟前置++构成重载区分
    46. Date operator ++ (int);
    47. Date& operator -= (int day);
    48. Date operator - (int day)const;
    49. //日期减日期
    50. int operator -(const Date& d)const;
    51. Date& operator -- ();
    52. Date operator -- (int);
    53. // 流插入
    54. // 不建议,因为Date* this占据了一个参数位置,使用d<
    55. //void operator<<(ostream& out);
    56. void Printf()const
    57. {
    58. cout << _year << "年" << _month << "月" << _day << "日" << endl;
    59. }
    60. private:
    61. int _year = 1;
    62. int _month = 1;
    63. int _day = 1;
    64. };

    Test.cpp

    1. #include"Date.h"
    2. bool Date::CheckDate()
    3. {
    4. if (_month < 1 || _month>12
    5. || _day<1 || _day>GetMonthDay(_year, _month))
    6. {
    7. return false;
    8. }
    9. else
    10. {
    11. return true;
    12. }
    13. }
    14. // 全缺省的构造函数
    15. Date::Date(int year , int month , int day )
    16. {
    17. _year = year;
    18. _month = month;
    19. _day = day;
    20. if (!CheckDate())
    21. {
    22. cout << "日期非法" << endl;
    23. }
    24. }
    25. //Date d2(d1);
    26. //Date d2 = d1;
    27. // 拷贝构造函数
    28. Date::Date(const Date& d)
    29. {
    30. _year = d._year;
    31. _month = d._month;
    32. _day = d._day;
    33. }
    34. // 赋值运算符重载
    35. // d2 = d3; -> d2.operator=(&d2, d3);
    36. Date& Date::operator=(const Date& d)
    37. {
    38. _year = d._year;
    39. _month = d._month;
    40. _day = d._day;
    41. return *this;
    42. }
    43. // 析构函数
    44. Date::~Date()
    45. {
    46. _year = 1;
    47. _month = 1;
    48. _day = 1;
    49. }
    50. bool Date::operator == (const Date& d)const
    51. {
    52. return _year == d._year
    53. && _month == d._month
    54. && _day == d._day;
    55. }
    56. bool Date::operator != (const Date& d)const
    57. {
    58. return !(*this == d);
    59. }
    60. bool Date::operator <(const Date& d)const
    61. {
    62. if(this->_year
    63. {
    64. return true;
    65. }
    66. else
    67. {
    68. if (this->_year == d._year && this->_month < d._month)
    69. {
    70. return true;
    71. }
    72. if (this->_year == d._year && this->_month == d._month && this->_day < d._day)
    73. {
    74. return true;
    75. }
    76. }
    77. return false;
    78. }
    79. bool Date::operator > (const Date& d)const
    80. {
    81. return (!(*this < d)) && *this != d;//易错,或者!(*this<=d),但是要在<=函数后面
    82. }
    83. bool Date::operator <=(const Date& d)const
    84. {
    85. return *this < d || *this == d;
    86. }
    87. bool Date::operator >= (const Date& d)const
    88. {
    89. return *this > d || *this == d;
    90. }
    91. Date& Date::operator += (int day)
    92. {
    93. if (day<0)
    94. {
    95. return *this -= -day;
    96. }
    97. _day += day;
    98. while (_day > GetMonthDay(_year,_month))
    99. {
    100. _day -= GetMonthDay(_year, _month);
    101. ++_month;
    102. if (_month == 13)
    103. {
    104. ++_year;
    105. _month = 1;
    106. }
    107. }
    108. return *this;
    109. }
    110. Date Date::operator + (int day)const
    111. {
    112. Date tmp=*this;
    113. tmp += day;
    114. return tmp;
    115. }
    116. Date& Date::operator ++ ()
    117. {
    118. *this += 1;
    119. return *this;
    120. }
    121. Date Date::operator ++ (int)
    122. {
    123. Date tmp = *this;
    124. *this += 1;
    125. return tmp;
    126. }
    127. Date& Date::operator -= (int day)
    128. {
    129. if(day<0)
    130. {
    131. return *this += -day;
    132. }
    133. _day -= day;
    134. while (_day < 1)
    135. {
    136. --_month;
    137. if (_month == 0)
    138. {
    139. --_year;
    140. _month = 12;
    141. }
    142. _day+= GetMonthDay(_year, _month);
    143. }
    144. return *this;
    145. }
    146. Date Date::operator - (int day)const
    147. {
    148. Date tmp = *this;
    149. tmp -= day;
    150. return tmp;
    151. }
    152. //日期减日期
    153. int Date::operator -(const Date& d)const
    154. {
    155. int flag = 1;
    156. int count = 0;
    157. //假设this大,d小
    158. Date max = (*this);
    159. Date min = d;
    160. if (max < min)
    161. {
    162. max = d;
    163. min = (*this);
    164. flag = -1;
    165. }
    166. while (max>min)
    167. {
    168. ++min;//一般用前置
    169. ++count;
    170. }
    171. return count * flag;
    172. }
    173. Date& Date::operator -- ()
    174. {
    175. *this -= 1;
    176. return *this;
    177. }
    178. Date Date::operator -- (int)
    179. {
    180. Date tmp = *this;
    181. *this -= 1;
    182. return tmp;
    183. }
    184. ostream& operator<<(ostream& out,Date& d)
    185. {
    186. out << d._year << "年" <"月" << d._day << "日" << endl;
    187. return out;
    188. }
    189. istream& operator>>(istream& in, Date& d)
    190. {
    191. cout << "请输入年,月,日" << endl;
    192. in >> d._year >> d._month >> d._day;
    193. if (!d.CheckDate())
    194. {
    195. cout << "日期非法" << endl;
    196. }
    197. return in;
    198. }

    测试代码Test.cpp

    1. #include"Date.h"
    2. using namespace std;
    3. void Test1()
    4. {
    5. Date d1(2022,6,21);
    6. Date d2 = d1;
    7. Date d3(2022,6,1);
    8. if (d3 < d1)
    9. {
    10. cout << "d3 << endl;
    11. }
    12. }
    13. void Test2()
    14. {
    15. Date d1(2024, 4, 21);
    16. Date d2 = d1;
    17. d2 += 3000;
    18. d2.Printf();
    19. Date d3 = d1 + 300;
    20. d3.Printf();
    21. Date d4= d3++;
    22. d4.Printf();
    23. ++d3;
    24. d3.Printf();
    25. }
    26. void Test3()
    27. {
    28. Date d1(2024,4,21);
    29. Date d2 = d1 - 5000;
    30. d1 -= 20;
    31. d1.Printf();
    32. d2.Printf();
    33. }
    34. void Test4()
    35. {
    36. Date d1(2024,4,21);
    37. Date d2(2002,2,14);
    38. int day = d2 - d1;
    39. cout << day << "Ìì" << endl;
    40. }
    41. void Test5()
    42. {
    43. Date d1(2024,4,21);
    44. Date d2=d1--;
    45. d2.Printf();
    46. d1.Printf();
    47. --d1;
    48. d1.Printf();
    49. }
    50. void Test6()
    51. {
    52. Date d1(2024, 4, 14);
    53. Date d2 = d1 + 30000;
    54. // operator<<(cout, d1)
    55. cout << d1;
    56. cout << d2;
    57. cin >> d1 ;
    58. d2 = d1 + 30000;
    59. cout << d1 << d2;
    60. }
    61. int main()
    62. {
    63. /*Test6();*/
    64. Date d1(2024, 4, 21);
    65. Date d2(2024, 4, 21);
    66. if (d1 == d2)
    67. {
    68. cout << "d1 == d2" << endl;
    69. }
    70. return 0;
    71. }

    这个博客如果对你有帮助,给博主一个免费的点赞就是最大的帮助

    欢迎各位点赞,收藏和关注哦

    如果有疑问或有不同见解,欢迎在评论区留言哦

    后续我会一直分享双一流211西北大学软件(C,数据结构,C++,Linux,MySQL)的学习干货以及重要代码的分享

  • 相关阅读:
    QXlsx 使用
    vulnhub——The Planets: Earth
    使用poi-tl循环导出word报表
    【c++】智能指针
    python自动化测试——unittest二次开发之根据不同的粒度实现多线程执行测试用例(一)
    抖音很火的情侣公众号天气推送
    python性能分析
    oracle查询数据库内全部的表名、列明、注释、数据类型、长度、精度等
    如何基于 Apache Doris 构建新一代日志分析平台
    【数据结构】二叉树的层序遍历(四)
  • 原文地址:https://blog.csdn.net/m0_73751295/article/details/138046905