日期类

来源:互联网 发布:中国人才流失严重知乎 编辑:程序博客网 时间:2024/05/02 01:14

Date类的编写要求如下,编写构造函数以及运算符的重载,以及测试基础代码

class Date

{                                      
public: 
Date(int year = 1900, int month = 1, int day = 1) 
:_year(year) 
,_month(month) 
,_day(day) 

// 如何检查一个日期是否合法 
int days = GetMonthDays(year, month); 
if (days == -1 || day < 1 || day > days) 

cout<<"日期不合法"<<endl; 
Display(); 
exit(-1); 



//d1 > d2 
bool operator>(const Date& d); 
//operator>= 
//operator< 
//operator<= 
//operator== 
//operator!= 

//d1 + 100 
Date operator+(int day); 
Date operator+=(int day); 
Date operator-(int day); 
Date operator-=(int day); 
Date operator++(); 
Date operator++(int); 
Date operator--(); 
Date operator--(int); 
int operator-(const Date& d); 

bool IsLeapYear(int year); 
// if /switch/array 
int GetMonthDays(int year, int month); 
private: 
int _year; 
int _month; 
int _day; 

};                    


代码如下:

[plain] view plain copy
  1. #define _CRT_SECURE_NO_WARNINGS 1  
  2.   
  3. #include<iostream>  
  4. using namespace std;  
  5. class Date  
  6. {  
  7. public:  
  8.     Date(int year = 1900, int month = 1, int day = 1)  
  9.         :_year(year)  
  10.         , _month(month)  
  11.         , _day(day)  
  12.     {  
  13.         // 如何检查一个日期是否合法   
  14.         int days = GetMonthDays(year, month);  
  15.         if (days == -1 || day < 1 || day > days)  
  16.         {  
  17.             cout << "日期不合法" << endl;  
  18.             this->Display();  
  19.             //exit(-1);  
  20.         }  
  21.     }  
  22.     void Display()const  
  23.     {  
  24.         cout << _year << "-" << _month << "-" << _day << endl;  
  25.     }  
  26.     //d1 > d2   
  27.     bool operator>(const Date& d)                //>  
  28.     {  
  29.         if (this->_year > d._year)  
  30.         {  
  31.             return true;  
  32.         }  
  33.         else if (this->_year == d._year)  
  34.         {  
  35.             if (this->_month > d._month)  
  36.             {  
  37.                 return true;  
  38.             }  
  39.             else if (this->_month == d._month)  
  40.             {  
  41.                 if (this->_day > d._day)  
  42.                 {  
  43.                     return true;  
  44.                 }  
  45.             }  
  46.         }  
  47.         return false;  
  48.     }  
  49.       
  50.     static bool IsLeapYear(int year)  
  51.     {  
  52.         if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))  
  53.         {  
  54.             return true;  
  55.         }  
  56.         return false;  
  57.     }  
  58.     inline static int GetMonthDays(int year, int month)  
  59.     {  
  60.         if (year < 1900 || month<1 || month>12)  
  61.         {  
  62.             return -1;  
  63.         }  
  64.         static int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  
  65.         int days = arr[month];  
  66.         if ((month == 2) && (IsLeapYear(year)))  
  67.         {  
  68.             days += 1;  
  69.         }  
  70.         return days;  
  71.     }  
  72.     //operator>=   
  73.     bool operator >=(const Date& d)              //>=  
  74.     {  
  75.         return (*this>d)||(*this==d);  
  76.     }  
  77.     //operator<   
  78.     bool operator<(const Date& d)                //<  
  79.     {  
  80.         return !(*this>= d);  
  81.     }  
  82.     //operator<=   
  83.     bool operator<=(const Date& d)              //<=  
  84.     {  
  85.         return (*this < d || *this == d);  
  86.     }  
  87.     //operator==   
  88.     bool operator==(const Date& d)              //==  
  89.     {  
  90.         return _year == d._year  
  91.             && _month == d._month  
  92.             && _day == d._day;  
  93.     }  
  94.     //operator!=   
  95.     bool operator!=(const Date& d)              //!=  
  96.     {  
  97.         return !(*this == d);  
  98.     }  
  99.   
  100.     bool IsInvalid()  
  101.     {  
  102.         if (_year < 1900 || _month<1 || _month>12 || _day<1 || _day>GetMonthDays(_year, _month))  
  103.         {  
  104.             return true;  
  105.         }  
  106.         else  
  107.         {  
  108.             return false;  
  109.         }  
  110.     }  
  111.     //d1 + 100   
  112.     Date operator+(int day)                        //+  
  113.     {  
  114.         if (day < 0)  
  115.         {  
  116.             return *this - (-day);  
  117.         }  
  118.         Date tmp(*this);  
  119.         tmp._day += day;  
  120.         while (tmp.IsInvalid())  
  121.         {  
  122.             tmp._day -= GetMonthDays(tmp._year, tmp._month);  
  123.             tmp._month++;  
  124.             if (tmp._month == 13)  
  125.             {  
  126.                 tmp._year++;  
  127.                 tmp._month = 1;  
  128.             }  
  129.         }  
  130.         return tmp;  
  131.     }  
  132.     Date operator+=(int day)                     //+=  
  133.     {  
  134.         if (day < 0)  
  135.         {  
  136.             return *this -= (-day);  
  137.         }  
  138.         _day += day;  
  139.         while (IsInvalid())  
  140.         {  
  141.             _day -= GetMonthDays(_year, _month);  
  142.             _month++;  
  143.             if (_month == 13)  
  144.             {  
  145.                 _year++;  
  146.                 _month = 1;  
  147.             }  
  148.         }  
  149.         return *this;  
  150.     }  
  151.     Date operator-(int day)                //-  
  152.     {  
  153.         if (day < 0)  
  154.         {  
  155.             return *this + (-day);  
  156.         }  
  157.         Date tmp(*this);  
  158.         tmp._day -= day;  
  159.         while (tmp.IsInvalid()==true)  
  160.         {  
  161.             if (tmp._month == 1)  
  162.             {  
  163.                 tmp._year--;  
  164.                 tmp._month = 12;  
  165.             }  
  166.             else  
  167.             {  
  168.                 tmp._month--;  
  169.             }  
  170.             tmp._day += tmp.GetMonthDays(tmp._year, tmp._month);  
  171.               
  172.         }  
  173.         return tmp;  
  174.     }  
  175.     Date operator-=(int day)             //-=  
  176.     {  
  177.         if (day < 0)  
  178.         {  
  179.             return *this += (-day);  
  180.         }  
  181.         _day -= day;  
  182.         while (IsInvalid() == true)  
  183.         {  
  184.             if (_month == 1)  
  185.             {  
  186.                 _year--;  
  187.                 _month = 12;  
  188.             }  
  189.             else  
  190.             {  
  191.                 _month--;  
  192.             }  
  193.             _day += GetMonthDays(_year, _month);  
  194.   
  195.         }  
  196.         return *this;  
  197.     }  
  198.   
  199.     Date operator++()          //前置++  
  200.     {  
  201.         *this+=1;  
  202.         return *this;  
  203.     }  
  204.     Date operator++(int)          //后置++  
  205.     {  
  206.         Date tmp(*this);  
  207.         *this+=1;  
  208.         return tmp;  
  209.     }  
  210.     Date operator--()               //前置--  
  211.     {  
  212.         *this -= 1;  
  213.         return *this;  
  214.     }  
  215.     Date operator--(int)             //后置--  
  216.     {  
  217.         Date tmp(*this);  
  218.         *this -= 1;  
  219.         return tmp;  
  220.     }  
  221.     int operator-(const Date& d)        //一个日期减去一个日期,相差多少天  
  222.     {                                   //将两个日期分别与初始定义的1900-1-1相减,得到两个天数再相减  
  223.         int Leapyearday = 366;  
  224.         int Noleapyearday = 365;  
  225.         Date chushi;  
  226.         Date tmp1(*this);  
  227.         int day1 = tmp1._day ;  
  228.         while (tmp1._month >chushi._month)  
  229.         {  
  230.             tmp1._month--;  
  231.             day1 += tmp1.GetMonthDays(tmp1._year, tmp1._month);  
  232.         }  
  233.         while (tmp1._year >chushi._year)  
  234.         {  
  235.             tmp1._year--;  
  236.             if (tmp1.IsLeapYear(tmp1._year))  
  237.             {  
  238.                 day1 += Leapyearday;  
  239.             }  
  240.             else  
  241.             {  
  242.                 day1 += Noleapyearday;  
  243.             }  
  244.         }  
  245.         Date tmp2(d);  
  246.         int day2 = tmp2._day;  
  247.         while (tmp2._month >chushi._month)  
  248.         {  
  249.             tmp2._month--;  
  250.             day2 += tmp2.GetMonthDays(tmp2._year, tmp2._month);  
  251.         }  
  252.         while (tmp2._year > chushi._year)  
  253.         {  
  254.             tmp2._year--;  
  255.             if (tmp2.IsLeapYear(tmp2._year))  
  256.             {  
  257.                 day2 += Leapyearday;  
  258.             }  
  259.             else  
  260.             {  
  261.                 day2 += Noleapyearday;  
  262.             }  
  263.         }  
  264.   
  265.         int day = day1 - day2;  
  266.         return day;  
  267.     }  
  268. private:  
  269.     int _year;  
  270.     int _month;  
  271.     int _day;  
  272. };  

测试用例:

[plain] view plain copy
  1. void test()  
  2. {  
  3.     Date d1;  
  4.     Date d2(1990, 1, 1);  
  5.     d1.Display();  
  6.     d2.Display();  
  7.     cout << d1.operator==(d2)<< endl;  
  8.     cout << d1.operator<=(d2) << endl;  
  9.     cout << d1.operator>=(d2) << endl;  
  10.     cout << d1.operator!=(d2) << endl;  
  11.     cout << d1.operator<(d2) << endl;  
  12.     cout << d1.operator>(d2) << endl;  
  13.   
  14. }  
  15. void test1()  
  16. {  
  17.     Date d1(1991, 7, 1);  
  18.     Date d3 = ++d1;  
  19.     d3.Display();  
  20.     d3 = d1++;  
  21.     d3.Display();  
  22.     d1.Display();  
  23.     d3 = d1 + 1000;  
  24.     d3.Display();  
  25.     d1 += 1000;  
  26.     d1.Display();  
  27.   
  28. }  
  29.   
  30. void test2()  
  31. {  
  32.     Date d1(1991, 7, 1);  
  33.     Date d3 = d1 - 1000;  
  34.     d3.Display();  
  35.     d1.Display();  
  36.     d1 -= 1000;  
  37.     d1.Display();  
  38. }  
  39.   
  40. void test3()  
  41. {  
  42.     Date d1(1991,7,1);  
  43.     d1.Display();  
  44.     Date d3 = d1--;  
  45.     d3.Display();  
  46.     d1.Display();  
  47.     d3 = --d1;  
  48.     d3.Display();  
  49.     d1.Display();  
  50. }  
  51. void test4()  
  52. {  
  53.     Date d1(1991, 1, 1);  
  54.     Date d2(1994, 7, 1);  
  55.     int days = d2 - d1;  
  56.     cout << days << endl;  
  57. }  
原创粉丝点击