用c++实现日期类函数的重载

来源:互联网 发布:好帮手软件简介 编辑:程序博客网 时间:2024/06/08 18:39
  1. #include<iostream>  
  2. #include<windows.h>  
  3.   
  4. using namespace std;  
  5.   
  6. class Date  
  7. {  
  8. public:  
  9.     Date(int year, int month, int day) //构造函数  
  10.         :_year(year)  
  11.         ,_month(month)  
  12.         ,_day(day)  
  13.     {}  
  14.     Date(Date & d)   //拷贝构造  
  15.         :_year(d._year)  
  16.         , _month(d._month)  
  17.         , _day(d._day)  
  18.     {}  
  19.     Date & operator = (const Date &d) //赋值运算符的重载  
  20.     {//将实例d的所有成员变量值全部赋值给this,这里不存在指针问题,就不用考虑内存问题  
  21.         _year = d._year;  
  22.         _month = d._month;  
  23.         _day = d._day;  
  24.         return *this;  
  25.     }  
  26.     bool operator == (const Date& d)//重载 ==  
  27.     {  
  28.         return this->_year == d._year  
  29.             && this->_month == d._month  
  30.             && this->_day == d._day;  
  31.     }  
  32.     bool operator <(const Date& d) //重载 <  
  33.     {  
  34.         if (_year < d._year)//判断年  
  35.         {  
  36.             return true;  
  37.         }  
  38.         else if (_year == d._year)//年相等,就判断月  
  39.         {  
  40.             if (_month<d._month)  
  41.             {  
  42.                 return true;  
  43.             }  
  44.             else if (_month == d._month)//月相等,判断天  
  45.             {  
  46.                 if (_day<d._day)  
  47.                 {  
  48.                     return true;  
  49.                 }  
  50.             }  
  51.         }  
  52.         return false;  
  53.     }  
  54.     bool operator <=(const Date& d)//重载<=(复用函数<和==)  
  55.     {  
  56.         return (*this<d) || (*this == d);//当<或==满足一个的时候为真  
  57.     }  
  58.     bool operator >(const Date& d)//重载<=(复用函数<=)  
  59.     {  
  60.         return !(*this <= d);  
  61.     }  
  62.     bool operator >=(const Date& d)  
  63.     {  
  64.         return !(*this < d);  
  65.     }  
  66.     Date operator+ (int day)//重载+  
  67.     {  
  68.         int ret = GetMonthDay(_year, _month);//调用函数获取当年当月天数  
  69.         Date tmp = *this;//用this创建一个临时对象  
  70.         while ((_day + day) > ret)//循环条件:当总天数大于一个月的天数时  
  71.         {  
  72.             if ((tmp._month + 1) > 12)//当月份大于12时  
  73.             {  
  74.                 tmp._year++;//年份加一  
  75.                 tmp._month = 0;//月份置零  
  76.             }  
  77.             else//否则,月份加一  
  78.             {  
  79.                 tmp._month++;  
  80.             }  
  81.             day -= ret;//总天数减去当月的天数  
  82.             ret = GetMonthDay(_year, tmp._month);//获取下一月的天数  
  83.         }  
  84.         tmp._day += day;  
  85.         return tmp;  
  86.     }  
  87.     Date& operator+= (int day)//重载+=(复用+)  
  88.     {  
  89.         *this = *this + day;  
  90.         return *this;  
  91.     }  
  92.   
  93.     Date operator- (int day)//重载-  
  94.     {  
  95.         int ret = GetMonthDay(_year, _month);  
  96.         Date tmp = *this;  
  97.         while (day > tmp._day)//当需要减的天>当月的天数时进行循环  
  98.         {  
  99.             while (day > ret)  
  100.             {  
  101.                 if (tmp._month > 1)  
  102.                 {  
  103.                     tmp._month--;  
  104.                 }  
  105.                 else  
  106.                 {  
  107.                     tmp._year--;  
  108.                     tmp._month = 11;  
  109.                 }  
  110.                 day -= ret;  
  111.                 ret = GetMonthDay(tmp._year, tmp._month);  
  112.             }  
  113.             day -= ret;  
  114.         }  
  115.         tmp._day -= day;  
  116.         return tmp;  
  117.     }  
  118.     Date& operator-= (int day)//重载-=(复用-)  
  119.     {  
  120.         *this = *this - day;  
  121.         return *this;  
  122.     }  
  123.   
  124.     Date operator++()  
  125.     {  
  126.         return *this += 1;  
  127.     }  
  128.     Date operator++(int)  
  129.     {  
  130.         return *this + 1;  
  131.     }  
  132.   
  133.     Date operator--()  
  134.     {  
  135.         return *this -= 1;  
  136.     }  
  137.     Date operator--(int)  
  138.     {  
  139.         return *this - 1;  
  140.     }  
  141.   
  142.     int operator-(const Date& d)//日期间日期  
  143.     {  
  144.         Date tmp = *this;  
  145.         int day = 0;  
  146.         int ret = 0;  
  147.         if (tmp > d)  
  148.         {  
  149.             while (tmp._year > d._year)  
  150.             {  
  151.                 if (tmp._month > 2)  
  152.                 {  
  153.                     if (IsLeapYear(tmp._year))  
  154.                     {  
  155.                         day += 366;  
  156.                         tmp._year--;  
  157.                     }  
  158.                 }  
  159.                 else  
  160.                 {  
  161.                     day += 365;  
  162.                     tmp._year--;  
  163.                 }  
  164.             }  
  165.             while (tmp._month > d._month)  
  166.             {  
  167.                 ret = GetMonthDay(tmp._year, tmp._month);  
  168.                 day += ret;  
  169.             }  
  170.             if (tmp._day > d._day)  
  171.             {  
  172.                 day += tmp._day - d._day;  
  173.             }  
  174.             else  
  175.             {  
  176.                 day -= d._day - tmp._day;  
  177.             }  
  178.   
  179.         }  
  180.         return day;  
  181.     }  
  182.     void Display()  
  183.     {  
  184.         cout << _year << "\t" << _month << "\t" << _day << endl;  
  185.     }  
  186. protected:  
  187.     bool IsLeapYear(int year)//判断是否为闰年  
  188.     {  
  189.         if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)  
  190.         {  
  191.             return true;  
  192.         }  
  193.         return false;  
  194.     }  
  195.     int GetMonthDay(int year, int month)//获得当年当月天数  
  196.     {  
  197.         int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  
  198.   
  199.         int day = monthArray[month];  
  200.   
  201.         if (month == 2 && IsLeapYear(year))  
  202.         {  
  203.             day += 1;  
  204.         }  
  205.         return day;  
  206.     }  
  207. private:  
  208.     int _year;  
  209.     int _month;  
  210.     int _day;  
  211. };  
  1. #include<iostream>  
  2. #include<windows.h>  
  3.   
  4. using namespace std;  
  5.   
  6. class Date  
  7. {  
  8. public:  
  9.     Date(int year, int month, int day) //构造函数  
  10.         :_year(year)  
  11.         ,_month(month)  
  12.         ,_day(day)  
  13.     {}  
  14.     Date(Date & d)   //拷贝构造  
  15.         :_year(d._year)  
  16.         , _month(d._month)  
  17.         , _day(d._day)  
  18.     {}  
  19.     Date & operator = (const Date &d) //赋值运算符的重载  
  20.     {//将实例d的所有成员变量值全部赋值给this,这里不存在指针问题,就不用考虑内存问题  
  21.         _year = d._year;  
  22.         _month = d._month;  
  23.         _day = d._day;  
  24.         return *this;  
  25.     }  
  26.     bool operator == (const Date& d)//重载 ==  
  27.     {  
  28.         return this->_year == d._year  
  29.             && this->_month == d._month  
  30.             && this->_day == d._day;  
  31.     }  
  32.     bool operator <(const Date& d) //重载 <  
  33.     {  
  34.         if (_year < d._year)//判断年  
  35.         {  
  36.             return true;  
  37.         }  
  38.         else if (_year == d._year)//年相等,就判断月  
  39.         {  
  40.             if (_month<d._month)  
  41.             {  
  42.                 return true;  
  43.             }  
  44.             else if (_month == d._month)//月相等,判断天  
  45.             {  
  46.                 if (_day<d._day)  
  47.                 {  
  48.                     return true;  
  49.                 }  
  50.             }  
  51.         }  
  52.         return false;  
  53.     }  
  54.     bool operator <=(const Date& d)//重载<=(复用函数<和==)  
  55.     {  
  56.         return (*this<d) || (*this == d);//当<或==满足一个的时候为真  
  57.     }  
  58.     bool operator >(const Date& d)//重载<=(复用函数<=)  
  59.     {  
  60.         return !(*this <= d);  
  61.     }  
  62.     bool operator >=(const Date& d)  
  63.     {  
  64.         return !(*this < d);  
  65.     }  
  66.     Date operator+ (int day)//重载+  
  67.     {  
  68.         int ret = GetMonthDay(_year, _month);//调用函数获取当年当月天数  
  69.         Date tmp = *this;//用this创建一个临时对象  
  70.         while ((_day + day) > ret)//循环条件:当总天数大于一个月的天数时  
  71.         {  
  72.             if ((tmp._month + 1) > 12)//当月份大于12时  
  73.             {  
  74.                 tmp._year++;//年份加一  
  75.                 tmp._month = 0;//月份置零  
  76.             }  
  77.             else//否则,月份加一  
  78.             {  
  79.                 tmp._month++;  
  80.             }  
  81.             day -= ret;//总天数减去当月的天数  
  82.             ret = GetMonthDay(_year, tmp._month);//获取下一月的天数  
  83.         }  
  84.         tmp._day += day;  
  85.         return tmp;  
  86.     }  
  87.     Date& operator+= (int day)//重载+=(复用+)  
  88.     {  
  89.         *this = *this + day;  
  90.         return *this;  
  91.     }  
  92.   
  93.     Date operator- (int day)//重载-  
  94.     {  
  95.         int ret = GetMonthDay(_year, _month);  
  96.         Date tmp = *this;  
  97.         while (day > tmp._day)//当需要减的天>当月的天数时进行循环  
  98.         {  
  99.             while (day > ret)  
  100.             {  
  101.                 if (tmp._month > 1)  
  102.                 {  
  103.                     tmp._month--;  
  104.                 }  
  105.                 else  
  106.                 {  
  107.                     tmp._year--;  
  108.                     tmp._month = 11;  
  109.                 }  
  110.                 day -= ret;  
  111.                 ret = GetMonthDay(tmp._year, tmp._month);  
  112.             }  
  113.             day -= ret;  
  114.         }  
  115.         tmp._day -= day;  
  116.         return tmp;  
  117.     }  
  118.     Date& operator-= (int day)//重载-=(复用-)  
  119.     {  
  120.         *this = *this - day;  
  121.         return *this;  
  122.     }  
  123.   
  124.     Date operator++()  
  125.     {  
  126.         return *this += 1;  
  127.     }  
  128.     Date operator++(int)  
  129.     {  
  130.         return *this + 1;  
  131.     }  
  132.   
  133.     Date operator--()  
  134.     {  
  135.         return *this -= 1;  
  136.     }  
  137.     Date operator--(int)  
  138.     {  
  139.         return *this - 1;  
  140.     }  
  141.   
  142.     int operator-(const Date& d)//日期间日期  
  143.     {  
  144.         Date tmp = *this;  
  145.         int day = 0;  
  146.         int ret = 0;  
  147.         if (tmp > d)  
  148.         {  
  149.             while (tmp._year > d._year)  
  150.             {  
  151.                 if (tmp._month > 2)  
  152.                 {  
  153.                     if (IsLeapYear(tmp._year))  
  154.                     {  
  155.                         day += 366;  
  156.                         tmp._year--;  
  157.                     }  
  158.                 }  
  159.                 else  
  160.                 {  
  161.                     day += 365;  
  162.                     tmp._year--;  
  163.                 }  
  164.             }  
  165.             while (tmp._month > d._month)  
  166.             {  
  167.                 ret = GetMonthDay(tmp._year, tmp._month);  
  168.                 day += ret;  
  169.             }  
  170.             if (tmp._day > d._day)  
  171.             {  
  172.                 day += tmp._day - d._day;  
  173.             }  
  174.             else  
  175.             {  
  176.                 day -= d._day - tmp._day;  
  177.             }  
  178.   
  179.         }  
  180.         return day;  
  181.     }  
  182.     void Display()  
  183.     {  
  184.         cout << _year << "\t" << _month << "\t" << _day << endl;  
  185.     }  
  186. protected:  
  187.     bool IsLeapYear(int year)//判断是否为闰年  
  188.     {  
  189.         if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)  
  190.         {  
  191.             return true;  
  192.         }  
  193.         return false;  
  194.     }  
  195.     int GetMonthDay(int year, int month)//获得当年当月天数  
  196.     {  
  197.         int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  
  198.   
  199.         int day = monthArray[month];  
  200.   
  201.         if (month == 2 && IsLeapYear(year))  
  202.         {  
  203.             day += 1;  
  204.         }  
  205.         return day;  
  206.     }  
  207. private:  
  208.     int _year;  
  209.     int _month;  
  210.     int _day;  
  211. };