• 《日期类》的模拟实现


    目录

    前言:

    头文件类与函数的定义Date.h

    实现函数的Date.cpp

    测试Test.cpp

    运行结果:


    前言:

    我们在前面的两章初步学习认识了《类与对象》的概念,接下来我们将实现一个日期类,是我们的知识储备更加牢固。

    头文件类与函数的定义Date.h

    1. #pragma once
    2. #define _CRT_SECURE_NO_WARNIGS 1
    3. #include
    4. using namespace std;
    5. class Date
    6. {
    7. public:
    8. friend ostream& operator<<(ostream& out, const Date& d);
    9. int GetMonthDay(int _year, int _month)
    10. {
    11. int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    12. if ((_year % 4 == 0) && (_year % 100 != 0) || (_year % 400 == 0))
    13. {
    14. day[2] = 29;
    15. }
    16. return day[_month];
    17. }
    18. Date(int year = 2000, int month = 1, int day = 1)
    19. {
    20. if (month > 12 || day > GetMonthDay(year, month))
    21. {
    22. cout << "Setting error!!!" << endl;
    23. exit(-1);
    24. }
    25. _year = year;
    26. _month = month;
    27. _day = day;
    28. }
    29. bool operator==(const Date& d);
    30. bool operator!=(const Date& d);
    31. bool operator>(const Date& d);
    32. bool operator>=(const Date& d);
    33. bool operator<(const Date& d);
    34. bool operator<=(const Date& d);
    35. Date& operator+=(int day);
    36. Date operator+(int day);
    37. Date& operator++();
    38. Date operator++(int);
    39. Date& operator-=(int day);
    40. Date operator-(int day);
    41. Date& operator--();
    42. Date operator--(int);
    43. int operator-(const Date& d);
    44. private:
    45. int _year;
    46. int _month;
    47. int _day;
    48. };

    实现函数的Date.cpp

     

    1. #include"Data.h"
    2. bool Date::operator==(const Date& d)
    3. {
    4. return (_year == d._year) && (_month == d._month) && (_day == d._day);
    5. }
    6. bool Date::operator!=(const Date& d)
    7. {
    8. return !(*this == d);
    9. }
    10. bool Date::operator>(const Date& d)
    11. {
    12. if (_year > d._year)
    13. {
    14. return true;
    15. }
    16. else if (_year < d._year)
    17. {
    18. return false;
    19. }
    20. if (_month > d._month)
    21. {
    22. return true;
    23. }
    24. else if (_month < d._month)
    25. {
    26. return false;
    27. }
    28. if (_day > d._day)
    29. {
    30. return true;
    31. }
    32. return false;
    33. }
    34. bool Date::operator>=(const Date& d)
    35. {
    36. return (*this > d) || (*this == d);
    37. }
    38. bool Date::operator<(const Date& d)
    39. {
    40. return !(*this >= d);
    41. }
    42. bool Date::operator<=(const Date& d)
    43. {
    44. return !(*this > d);
    45. }
    46. Date& Date::operator+=(int day)
    47. {
    48. _day += day;
    49. while (_day > GetMonthDay(_year, _month))
    50. {
    51. _day -= GetMonthDay(_year, _month);
    52. _month++;
    53. if (_month == 13)
    54. {
    55. _month = 1;
    56. _year++;
    57. }
    58. }
    59. return *this;
    60. }
    61. Date Date::operator+(int day)
    62. {
    63. Date tmp = *this;
    64. tmp += day;
    65. return tmp;
    66. }
    67. Date& Date::operator++()
    68. {
    69. *this += 1;
    70. return *this;
    71. }
    72. Date Date::operator++(int)
    73. {
    74. Date tmp = *this;
    75. *this += 1;
    76. return tmp;
    77. }
    78. Date& Date::operator-=(int day)
    79. {
    80. _day -= day;
    81. while (_day <= 0)
    82. {
    83. _month--;
    84. if (_month == 0)
    85. {
    86. _month = 12;
    87. _year--;
    88. }
    89. _day += GetMonthDay(_year, _month);
    90. }
    91. return *this;
    92. }
    93. Date Date::operator-(int day)
    94. {
    95. Date tmp = *this;
    96. tmp -= day;
    97. return tmp;
    98. }
    99. Date& Date::operator--()
    100. {
    101. *this -= 1;
    102. return *this;
    103. }
    104. Date Date::operator--(int)
    105. {
    106. Date tmp = *this;
    107. *this -= 1;
    108. return tmp;
    109. }
    110. int Date::operator-(const Date& d)
    111. {
    112. int flag = 1;
    113. Date max = *this;
    114. Date min = d;
    115. if (max < min)
    116. {
    117. max = d;
    118. min = *this;
    119. flag = -1;
    120. }
    121. int count = 0;
    122. while (max != min)
    123. {
    124. ++min;
    125. ++count;
    126. }
    127. return flag * count;
    128. }
    129. ostream& operator<<(ostream& out, const Date& d)
    130. {
    131. out << d._year << "年" << d._month << "月" << d._day << "号";
    132. return out;
    133. }

    测试Test.cpp

    1. #include"Data.h"
    2. int main()
    3. {
    4. Date d1(2024, 3, 10);
    5. cout << d1 << endl;
    6. Date d2(2024, 6, 17);
    7. cout << d2 << endl;
    8. printf("相差天数:");
    9. cout << (d2 - d1) << endl;
    10. return 0;
    11. }

    运行结果:

  • 相关阅读:
    el-select与el-tree的联动,切换勾选保存回显功能(全部代码)
    qlistwidget不显示内容
    ChatGPT-4o体验demo
    hadoop fs 命令详情
    python中的NumPy和Pandas往往都是同时使用,NumPy和Pandas的在数据分析中的联合使用
    K8s 集群部署过程文档
    STM32使用HAL库UART接收不定长数据-1
    游戏工程管理
    【ccf-csp题解】第7次csp认证-第三题-路径解析超详细题解-字符串模拟
    vite — 超快且方便的编译工具
  • 原文地址:https://blog.csdn.net/weixin_72917087/article/details/136602625