C++日期类计算器怎么实现

其他教程   发布日期:2025年04月15日   浏览次数:115

这篇文章主要介绍“C++日期类计算器怎么实现”,在日常操作中,相信很多人在C++日期类计算器怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++日期类计算器怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    1.获取某年某月的天数

    1. int GetMonthDay(int year, int month)
    2. {
    3. static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    4. if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    5. {
    6. return 29;
    7. }
    8. else
    9. {
    10. return monthDayArray[month];
    11. }
    12. }

    2.构造函数

    1. Date(int year = 1, int month = 1, int day = 1)
    2. {
    3. _year = year;
    4. _month = month;
    5. _day = day;
    6. //检查日期是否合法
    7. if (!((year >= 1)
    8. && (month >= 1 && month <= 12)
    9. && (day >= 1 && day <= GetMonthDay(year, month))))
    10. {
    11. cout << "非法日期" << endl;
    12. }
    13. }

    3.拷贝构造函数

    1. // 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了
    2. Date(const Date& d)
    3. {
    4. _year = d._year;
    5. _month = d._month;
    6. _day = d._day;
    7. }

    4.赋值运算符重载

    1. //d1 = d2
    2. //注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用
    3. Date& operator=(const Date& d)
    4. {
    5. //为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1
    6. if (this != &d)
    7. {
    8. _year = d._year;
    9. _month = d._month;
    10. _day = d._day;
    11. }
    12. return *this;
    13. }

    5.析构函数

    1. ~Date()//可不写
    2. {
    3. ;
    4. }

    日期类因为没有申请资源,所以无需写析构函数,编译器默认生成的析构函数就可以。

    6.日期+=天数

    1. //d1 += 100
    2. //天满了进月 月满了进年
    3. Date& operator+=(int day)
    4. {
    5. //避免 d1 += -1000的情形
    6. if (day < 0)
    7. {
    8. return *this -= -day;
    9. }
    10. _day += day;
    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. }

    7.日期+天数

    1. //d1 + 100
    2. Date operator+(int day) const
    3. {
    4. Date ret(*this);
    5. ret += day;//ret.operator+=(day)
    6. return ret;
    7. }

    8.日期-天数

    1. //d1 - 100
    2. Date operator-(int day) const
    3. {
    4. Date ret(*this);
    5. ret -= day;
    6. return ret;
    7. }

    9.日期-=天数

    1. //d1 -= 100
    2. Date& operator-=(int day)
    3. {
    4. //避免 d1 -= -1000
    5. if (day < 0)
    6. {
    7. return *this += -day;
    8. }
    9. _day -= day;
    10. while (_day <= 0)
    11. {
    12. --_month;
    13. if (_month == 0)
    14. {
    15. --_year;
    16. _month = 12;
    17. }
    18. _day += GetMonthDay(_year, _month);
    19. }
    20. return *this;
    21. }

    10.前置++的运算符重载

    1. //前置++
    2. Date& operator++()
    3. {
    4. //会调用 operator+=(int day)
    5. *this += 1;
    6. return *this;
    7. }

    11.后置++的运算符重载

    1. //后置++ ―多一个int参数主要是为了和前置++进行区分 构成函数重载
    2. Date operator++(int)
    3. {
    4. Date tmp(*this);
    5. *this += 1;
    6. return tmp;
    7. }

    12.前置--的运算符重载

    1. //前置--
    2. Date& operator--()
    3. {
    4. //复用运算符重载-=
    5. *this -= 1;
    6. return *this;
    7. }

    13.后置--的运算符重载

    1. //后置--
    2. Date operator--(int)
    3. {
    4. Date tmp = *this;
    5. *this -= 1;
    6. return tmp;
    7. }

    14.>的运算符重载

    1. //d1 > d2
    2. bool operator>(const Date& d) const
    3. {
    4. if (_year > d._year)
    5. {
    6. return true;
    7. }
    8. else if (_year == d._year && _month > d._month)
    9. {
    10. return true;
    11. }
    12. else if (_year == d._year && _month == d._month && _day > d._day)
    13. {
    14. return true;
    15. }
    16. return false;
    17. }

    15.<的运算符重载

    1. //d1 < d2
    2. bool operator<(const Date& d) const
    3. {
    4. return !(*this >= d);
    5. }

    16.==的运算符重载

    1. //d1 == d2
    2. bool operator==(const Date& d) const
    3. { return _year == d._year
    4. && _month == d._month
    5. && _day == d._day;
    6. }

    17.>=的运算符重载

    1. //d1 >= d2
    2. bool operator>=(const Date& d) const
    3. {
    4. return *this > d || *this == d;
    5. }

    18.<=的运算符重载

    1. //d1 <= d2
    2. bool operator<=(const Date& d) const
    3. {
    4. return !(*this > d);
    5. }

    19.!=的运算符重载

    1. //d1 != d2
    2. bool operator!=(const Date& d) const
    3. {
    4. return !(*this == d);
    5. }

    20.<<的运算符重载

    1. //内联函数和静态成员一样 调用处展开 不进符号表
    2. inline ostream& operator<<(ostream& out, const Date& d)
    3. {
    4. out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    5. return out;
    6. }

    21.>>的运算符重载

    1. //cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
    2. inline istream& operator>>(istream& in, Date& d)
    3. {
    4. in >> d._year >> d._month >> d._day;
    5. return in;
    6. }

    22.日期-日期

    1. //日期-日期
    2. int operator-(const Date& d) const
    3. {
    4. Date max = *this;
    5. Date min = d;
    6. int flag = 1;
    7. if (*this < d)
    8. //总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const
    9. //if (d > *this)
    10. {
    11. max = d;
    12. min = *this;
    13. flag = -1;
    14. }
    15. int n = 0;
    16. while (min != max)
    17. {
    18. ++n;
    19. //复用++ ++到和d1日期相等 就是相差多少天
    20. ++min;
    21. }
    22. return n * flag;
    23. }

    Date.h

    1. #pragma once
    2. #include <iostream>
    3. using namespace std;
    4. class Date
    5. {
    6. //友元声明(类的任意位置)声明友元时可以不用加inline
    7. friend ostream& operator<<(ostream& out, const Date& d);
    8. friend istream& operator>>(istream& in, Date& d);
    9. public:
    10. int GetMonthDay(int year, int month)
    11. {
    12. static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    13. if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    14. {
    15. return 29;
    16. }
    17. else
    18. {
    19. return monthDayArray[month];
    20. }
    21. }
    22. Date(int year = 1, int month = 1, int day = 1)
    23. {
    24. _year = year;
    25. _month = month;
    26. _day = day;
    27. //检查日期是否合法
    28. if (!((year >= 1)
    29. && (month >= 1 && month <= 12)
    30. && (day >= 1 && day <= GetMonthDay(year, month))))
    31. {
    32. cout << "非法日期" << endl;
    33. }
    34. }
    35. // 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了
    36. Date(const Date& d)
    37. {
    38. _year = d._year;
    39. _month = d._month;
    40. _day = d._day;
    41. }
    42. //d1 == d2
    43. bool operator==(const Date& d) const;
    44. //d1 > d2
    45. bool operator>(const Date& d) const;
    46. //d1 >= d2
    47. bool operator>=(const Date& d) const;
    48. //d1 <= d2
    49. bool operator<=(const Date& d) const;
    50. //d1 < d2
    51. bool operator<(const Date& d) const;
    52. //d1 != d2
    53. bool operator!=(const Date& d) const;
    54. //d1 += 100
    55. Date& operator+=(int day);
    56. //d1 + 100
    57. Date operator+(int day) const;
    58. //d1 = d2 注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用
    59. Date& operator=(const Date& d)
    60. {
    61. //为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1
    62. if (this != &d)
    63. {
    64. _year = d._year;
    65. _month = d._month;
    66. _day = d._day;
    67. }
    68. return *this;
    69. }
    70. //d1 -= 100
    71. Date& operator-=(int day);
    72. //d1 - 100
    73. Date operator-(int day) const;
    74. //++的操作数只有一个 不传参
    75. //前置++
    76. Date& operator++();
    77. //编译器为了区分前置++和后置++ 规定在后置的函数上加了一个参数
    78. //后置++
    79. Date operator++(int);
    80. //允许成员函数加const 此时this指针的类型为:const Date* const this
    81. void Print() const
    82. {
    83. cout << _year << "/" << _month << "/" << _day << endl;
    84. }
    85. //前置--
    86. Date& operator--();
    87. //后置--
    88. Date operator--(int);
    89. //日期-日期
    90. int operator-(const Date& d) const;
    91. //流插入
    92. //d1 << cout编译器会转化成d1.operator<<(cout) this指针抢了左操作数d1的位置
    93. //<<和>>的重载一般不写成成员函数 因为this默认抢了第一个参数的位置 Date类对象就是左操作数 不符合使用习惯和可读性
    94. /*void operator<<(ostream& out)
    95. {
    96. out << _year << "年" << _month << "月" << _day << "日" << endl;
    97. }*/
    98. //取地址重载
    99. Date* operator&()
    100. {
    101. return this;
    102. }
    103. //const成员取地址重载
    104. const Date* operator&() const
    105. {
    106. return this;
    107. }
    108. //取地址重载和const成员取地址重载不实现 编译器会默认生成
    109. private:
    110. int _year;
    111. int _month;
    112. int _day;
    113. };
    114. //结论:对于自定义类型,尽量用前置,减少拷贝,提高效率
    115. //全局函数调用:cout << d1转化成operator<<(cout,d1)
    116. //全局函数的定义和全局变量不能放在.h文件中 因为函数的定义在Date.cpp和test.cpp都会展开 函数地址进入符号表 链接器链接两个.cpp文件时相同的函数地址会报错
    117. //解决方法:1.改成静态 2.声明和定义分离
    118. //static修饰函数只在当前文件可见 不会进入符号表
    119. //static void operator<<(ostream& out,const Date& d)
    120. //{
    121. // out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    122. //}
    123. //ostream& operator<<(ostream& out, const Date& d);
    124. //内联函数和静态成员一样 调用处展开 不进符号表
    125. inline ostream& operator<<(ostream& out, const Date& d)
    126. {
    127. out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    128. return out;
    129. }
    130. //cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
    131. inline istream& operator>>(istream& in, Date& d)
    132. {
    133. in >> d._year >> d._month >> d._day;
    134. return in;
    135. }

    Date.cpp

    1. #include"Date.h"
    2. //d1 == d2
    3. bool Date::operator==(const Date& d) const
    4. { return _year == d._year
    5. && _month == d._month
    6. && _day == d._day;
    7. }
    8. //d1 > d2
    9. bool Date::operator>(const Date& d) const
    10. {
    11. if (_year > d._year)
    12. {
    13. return true;
    14. }
    15. else if (_year == d._year && _month > d._month)
    16. {
    17. return true;
    18. }
    19. else if (_year == d._year && _month == d._month && _day > d._day)
    20. {
    21. return true;
    22. }
    23. return false;
    24. }
    25. //d1 >= d2
    26. bool Date::operator>=(const Date& d) const
    27. {
    28. return *this > d || *this == d;
    29. }
    30. //d1 <= d2
    31. bool Date::operator<=(const Date& d) const
    32. {
    33. return !(*this > d);
    34. }
    35. //d1 < d2
    36. bool Date::operator<(const Date& d) const
    37. {
    38. return !(*this >= d);
    39. }
    40. //d1 != d2
    41. bool Date::operator!=(const Date& d) const
    42. {
    43. return !(*this == d);
    44. }
    45. //d1 += 100
    46. //天满了进月 月满了进年
    47. Date& Date::operator+=(int day)
    48. {
    49. //避免 d1 += -1000的情形
    50. if (day < 0)
    51. {
    52. return *this -= -day;
    53. }
    54. _day += day;
    55. while (_day > GetMonthDay(_year, _month))
    56. {
    57. _day -= GetMonthDay(_year, _month);
    58. _month++;
    59. if (_month == 13)
    60. {
    61. ++_year;
    62. _month = 1;
    63. }
    64. }
    65. return *this;
    66. }
    67. //d1 + 100
    68. Date Date::operator+(int day) const
    69. {
    70. Date ret(*this);
    71. ret += day;//ret.operator+=(day)
    72. return ret;
    73. }
    74. //d1 -= 100
    75. Date& Date::operator-=(int day)
    76. {
    77. //避免 d1 -= -1000
    78. if (day < 0)
    79. {
    80. return *this += -day;
    81. }
    82. _day -= day;
    83. while (_day <= 0)
    84. {
    85. --_month;
    86. if (_month == 0)
    87. {
    88. --_year;
    89. _month = 12;
    90. }
    91. _day += GetMonthDay(_year, _month);
    92. }
    93. return *this;
    94. }
    95. //d1 - 100
    96. Date Date::operator-(int day) const
    97. {
    98. Date ret(*this);
    99. ret -= day;
    100. return ret;
    101. }
    102. //前置++
    103. Date& Date::operator++()
    104. {
    105. //会调用 operator+=(int day)
    106. *this += 1;
    107. return *this;
    108. }
    109. //后置++ ―多一个int参数主要是为了和前置++进行区分 构成函数重载
    110. Date Date::operator++(int)
    111. {
    112. Date tmp(*this);
    113. *this += 1;
    114. return tmp;
    115. }
    116. //前置--
    117. Date& Date::operator--()
    118. {
    119. //复用运算符重载-=
    120. *this -= 1;
    121. return *this;
    122. }
    123. //后置--
    124. Date Date::operator--(int)
    125. {
    126. Date tmp = *this;
    127. *this -= 1;
    128. return tmp;
    129. }
    130. //日期-日期
    131. int Date::operator-(const Date& d) const
    132. {
    133. Date max = *this;
    134. Date min = d;
    135. int flag = 1;
    136. if (*this < d)
    137. //总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const
    138. //if (d > *this)
    139. {
    140. max = d;
    141. min = *this;
    142. flag = -1;
    143. }
    144. int n = 0;
    145. while (min != max)
    146. {
    147. ++n;
    148. //复用++ ++到和d1日期相等 就是相差多少天
    149. ++min;
    150. }
    151. return n * flag;
    152. }
    153. //为了支持链式流插入 cout<< d1 <<d2 返回cout类对象
    154. //ostream& operator<<(ostream& out,const Date& d)
    155. //{
    156. // out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    157. // return out;
    158. //}

    以上就是C++日期类计算器怎么实现的详细内容,更多关于C++日期类计算器怎么实现的资料请关注九品源码其它相关文章!