c++日期类的实现

来源:互联网 发布:筛子目的算法 编辑:程序博客网 时间:2024/05/18 20:07




  1. #pragma once  
  2. #include <iostream>  
  3. #include <assert.h>  
  4. using namespace std;  
  5.   
  6. class Date  
  7. {  
  8. public:  
  9.     Date(int y = 1900, int m = 1, int d = 1);  
  10.     Date(const Date &d);  //复制构造函数看成构造函数的重载, 只有参数不同   
  11.     //  比较运算符重载   
  12.     bool operator>(const Date& d); //前面的日期在后面的日期之后  敲的不多吧,感觉像在默写  
  13.     bool operator<(const Date& d);  
  14.     bool operator>=(const Date& d);  
  15.     bool operator<=(const Date& d);  
  16.     bool operator==(const Date& d); //类中被调用函数在调用函数之后可以找到这个函数吗?  
  17.     bool operator!=(const Date& d);  
  18.       
  19.     //赋值运算符重载  
  20.     Date &operator=(const Date & d);   //返回该类型的引用是为了支持连等  
  21.   
  22.     Date & operator++();//前置++  
  23.     Date operator++(int); //后置++  
  24.     Date & operator--(); // 前置--  
  25.     Date operator--(int); //后置--  
  26.     int operator-(const Date &d);//计算两个日期之间差几天  
  27.     Date operator-(int day); //计算几天之前的日期  
  28.     Date operator+(int day); //计算几天之后的日期  
  29.     Date operator+=(int day); //计算几天之后的日期  
  30.     int operator-(const Date &d); //计算两个日期之间差几天  
  31.     Date &operator-=(int day);//计算几天之前的日期  
  32.   
  33. private:  
  34.     //内部函数  
  35.     bool IsVaild();  //检查传入参数的合法性  
  36.     bool IsLeapYear();  
  37.     int GetMonthDay(); //  
  38. private:  
  39.     int _year;  
  40.     int _month;  
  41.     int _day;  

  1. };  



  1. #include "date.h"  
  2.   
  3. Date::Date(int y = 1900, int m = 1, int d = 1)  
  4. {  
  5.     _year = y;  
  6.     _month = m;  
  7.     _day = d;  
  8.     assert(IsVaild());  
  9. }  
  10.   
  11. Date::Date(const Date &d)  //复制构造函数看成构造函数的重载, 只有参数不同   
  12. {  
  13.     _year = d._year;  
  14.     _month = d._month;  
  15.     _day = d._day;  
  16. }  
  17.   
  18. //  比较运算符重载  
  19. bool Date::operator>(const Date& d)  //前面的日期在后面的日期之后  敲的不多吧,感觉像在默写  
  20. {  
  21.     if (_year > d._year)  
  22.         return true;  
  23.     else if (_year < d._year)  
  24.         return false;  
  25.     else  
  26.     {  
  27.         if (_month > d._month)  
  28.             return true;  
  29.         else if (_month < d._month)  
  30.             return false;  
  31.         else  
  32.             return _day > d._day;  
  33.     }  
  34. }  
  35. bool Date::operator<(const Date& d)  
  36. {  
  37.     return !(*this > d || *this == d);//比较的是*this   
  38. }  
  39. bool Date::operator>=(const Date& d)  
  40. {  
  41.     return ((*this > d) || (*this == d)); //调用的时候是date对象 编译器处理成this指针  return 后面到底用不用加()  
  42. }  
  43. bool Date::operator<=(const Date& d)  
  44. {  
  45.     return ((*this < d) || (*this == d));  
  46. }  
  47. bool Date::operator==(const Date& d) //类中被调用函数在调用函数之后可以找到这个函数吗?  
  48. {  
  49.     return (_year == d._year && _month == d._month && _day == d._day);  
  50. }  
  51. bool Date::operator!=(const Date& d) //类中被调用函数在调用函数之后可以找到这个函数吗?  
  52. {  
  53.     return !(_year == d._year && _month == d._month && _day == d._day);  
  54. }  
  55.   
  56.     //赋值运算符重载  
  57. Date &Date::operator=(const Date & d)   //返回该类型的引用是为了支持连等  
  58. {  
  59.     if (this != &d)  // 此处为比较地址  
  60.     {  
  61.         _year = d._year;  
  62.         _month = d._month;  
  63.         _day = d._day;  
  64.     }  
  65.     return *this;  
  66. }  
  67.   
  68. Date & Date::operator++()//前置++  
  69. {  
  70.     _day += 1;  
  71.     if (_day > GetMonthDay())  
  72.     {  
  73.         if (_month == 12)  
  74.         {  
  75.             _month = 1;  
  76.             _year += 1;  
  77.         }  
  78.         else  
  79.         {  
  80.             _month++;  
  81.         }  
  82.         _day = 1;  
  83.     }  
  84.     return *this;  
  85. }  
  86.   
  87. Date Date::operator++(int//后置++  
  88. {  
  89.     Date tmp(*this);  //Date 类型  
  90.     operator++();  
  91.     return tmp;  
  92. }  
  93.   
  94. Date & Date::operator--() // 前置--  
  95. {  
  96.     _day -= 1;  
  97.     if (_day < 1)  
  98.     {  
  99.         --_month;  
  100.         if (_month < 1)  
  101.         {  
  102.             --_year;  
  103.             _month = 12;  
  104.         }  
  105.         _day = GetMonthDay();  
  106.     }  
  107.     return *this;   //为什么要返回 是要支持连加  
  108. }  
  109.   
  110. Date Date::operator--(int//后置--  
  111. {  
  112.     Date tmp(*this);  
  113.     operator--();  
  114.     return tmp;  
  115. }  
  116.   
  117. Date Date::operator-(int day) //计算几天之前的日期  
  118. {  
  119.     Date tmp(*this);  
  120.     tmp -= day;  
  121.     return tmp;  
  122. }  
  123.   
  124. Date Date::operator+(int day) //计算几天之后的日期  
  125. {  
  126.     Date tmp(*this);  
  127.     tmp += day;  
  128.     return tmp;  
  129. }  
  130.   
  131. Date &Date::operator-=(int day) //计算几天之前的日期  
  132. {  
  133.     _day -= day;  
  134.     while (_day < 1)  
  135.     {  
  136.         --_month;  
  137.         if (_month < 1)  
  138.         {  
  139.             --_year;  
  140.             _month = 12;  
  141.         }  
  142.         _day += GetMonthDay();  
  143.     }  
  144.     return *this;  
  145. }  
  146.   
  147. Date Date::operator+=(int day) //计算几天之后的日期  
  148. {  
  149.     _day += day;  
  150.     while (_day > GetMonthDay())  
  151.     {  
  152.         _day -= GetMonthDay();  
  153.         _month++;  
  154.         if (_month > 12)  
  155.         {  
  156.             _month = 1;  
  157.             _year += 1;  
  158.         }  
  159.     }  
  160.     return *this;  
  161. }  
  162.   
  163. int Date::operator-(const Date &d)//计算两个日期之间差几天  
  164. {  
  165.     Date later = (*this) > d ? (*this) : d;  
  166.     Date formar = (*this) < d ? (*this) : d;  
  167.     int count = 0;  
  168.     while (formar != later)  
  169.     {  
  170.         ++formar;  
  171.         ++count;  
  172.     }  
  173.     return count;  
  174. }  
  175.   
  176.     //内部函数  
  177. bool Date::IsVaild()  //检查传入参数的合法性  
  178. {  
  179.     if (_year > 0  
  180.         && _month > 0 && _month <13  
  181.         && _day > 0 && _day <= GetMonthDay())  
  182.         return true;  
  183.     else  
  184.         return false;  
  185. }  
  186.   
  187. bool Date::IsLeapYear()  
  188. {  
  189.     if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0)  
  190.         return true;  
  191.     else  
  192.         return false;  
  193. }  
  194.   
  195. int Date::GetMonthDay() //  
  196. {  
  197.     static const int dayTable[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  
  198.     if (_month == 2 && IsLeapYear())  
  199.     {  
  200.         return dayTable[2] + 1;  
  201.     }  
  202.     else  
  203.         return dayTable[_month];  
  204. }  



test.c

[cpp] view plain copy
  1. #include "date.h"  
  2.   
  3. int main()  
  4. {  
  5.     Date d1(2016, 12,1);  
  6.     //d1-=10;  //2015 12 27  
  7.     Date d2(2016, 12, 23);  
  8.     //cout <<(d1.operator>=(d2)) << endl;  // d1.operator>(d2)  d1>d2;  operator>(d1,d2)由于隐式传参不行  
  9.     //d2 += 10; // 2017 1 2 想好你要的结果,就能快一些  
  10.     int distance = d2 - d1; // 22  
  11.     return 0;  
  12. }  


原创粉丝点击