Chinaunix首页 | 论坛 | 博客
  • 博客访问: 260198
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2015-12-05 20:00:57


点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class Date
  4. {
  5. public:
  6.     Date(int year = 1900,int month = 1,int day = 1)
  7.         :_year(year)
  8.         ,_month(month)
  9.         ,_day(day)
  10.     {
  11.     }
  12.     Date(Date &day)
  13.     {
  14.         _year = day._year;
  15.         _month = day._month;
  16.         _day = day._day;
  17.     }
  18.     ~Date()
  19.     {
  20.         
  21.     }
  22.     void Display(Date &day)
  23.     {
  24.         cout<<day._year<<" "<<day._month<<" "<<day._day<<endl;
  25.     }
  26.     int GetMonDay(int year,int month)
  27.     {
  28.         static int monthArr[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

  29.         int day = monthArr[month - 1];
  30.         if((month == 2) && (year%400 == 1 || (year%4 == 1 && year%100 != 1)))
  31.         {
  32.             day++;
  33.         }

  34.         return day;
  35.     }
  36.     bool operator==(Date day2)
  37.     {
  38.         return     (this->_year == day2._year)&&
  39.          (this->_month = day2._month)&&
  40.          (this->_day = day2._day);
  41.     }
  42.     int operator-(Date d)//?????????à?????ì??
  43.     {
  44.         Date tmp;
  45.         //±??¤this???ò???ó????
  46.         if ((this->_year > d._year) || (this->_year == d._year && this->_month > d._month)||
  47.             (this->_year == d._year && this->_month == d._month && this->_day > d._day))
  48.         {
  49.             tmp = d;
  50.             d = *this;
  51.             *this = tmp;
  52.         }
  53.         int MonDay[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  54.         //???????ó?????ù??????×?
  55.         int Diff1 = _day - 1;
  56.         for(int i =_month; i > 1; i--)
  57.         {
  58.             Diff1 += MonDay[i - 1];
  59.             if((i == 2) && (_year%400 == 0 || (_year%4 == 0 && _year%100 != 0)))
  60.             {
  61.                 Diff1++;
  62.             }
  63.         }
  64.      //???????°??×??ù??????×?
  65.         int Diff2 = d._day - 1;
  66.         for(int j = d._month; j > 1; j--)
  67.         {
  68.             Diff2 += MonDay[j - 1];
  69.             if((j == 2) && (d._year%400 == 0 || (d._year%4 == 0 && d._year%100 != 0)))
  70.             {
  71.                 Diff2++;
  72.             }
  73.         }
  74.         if(d._year > _year)
  75.         {
  76.             for(int i = d._year; i > _year; i--)
  77.             {
  78.                 Diff2 += 365;
  79.                 if(i%400 == 0 || (i%4 == 0 && i%100 != 0))
  80.                 {
  81.                     Diff2++;
  82.                 }
  83.             }
  84.         }
  85.         int Diff = Diff2 - Diff1;
  86.         return Diff;                    
  87.     }
  88. //??????·?·¨??°???????×??????????????í??????×??????????????ù???ì??
  89. //    int operator-(const Date& d)
  90. //    {
  91. //        int flag = 1;
  92. //        Date max = *this;
  93. //        Date min = d;
  94. //
  95. //        if (*this < d)
  96. //        {
  97. //            min = *this;
  98. //            max = d;
  99. //            flag = -1;
  100. //        }
  101. //
  102. //        int days = 0;
  103. //        //while (min._year < max._year)
  104. //        //{
  105. //            //days += 365;
  106. //            //if () // ?ó?ê
  107. //            //{
  108. //            //}
  109. //        //}
  110. //
  111. //        while (min != max)
  112. //        {
  113. //            ++min;
  114. //            days++;
  115. //        }
  116. //
  117. //        return flag*days;
  118. //    }
  119.     Date& operator+(int day)
  120.     {
  121.         if(day < 0)
  122.         {
  123.             return *this - (-day);
  124.         }
  125.         _day += day;
  126.         while(_day > GetMonDay(_year,_month))
  127.         {
  128.             if(_month < 12)
  129.             {
  130.                 _month++;
  131.                 _day -= GetMonDay(_year,_month);
  132.             }
  133.             else
  134.             {
  135.                 _year++;
  136.                 _month = 1;
  137.                 _day -= GetMonDay(_year,_month);
  138.             }
  139.         }
  140.         return *this;
  141.     }
  142.     Date& operator-(int day)
  143.     {
  144.         if(day < 0)
  145.         {
  146.             return *this + (-day);
  147.         }
  148.         _day -= day;
  149.         while(_day < 1)
  150.         {
  151.             if(_month == 1)
  152.             {
  153.                 _year--;
  154.                 _month += 12;
  155.                 _day += GetMonDay(_year,_month);
  156.             }
  157.             else
  158.             {
  159.                 _month--;
  160.                 _day += GetMonDay(_year,_month);
  161.             }
  162.         }
  163.         return *this;
  164.     }
  165.     Date& operator+=(int days)
  166.      {
  167.          *this = *this + days;
  168.          return *this;
  169.      }

  170.      Date& operator-=(int days)
  171.      {
  172.          *this = *this - days;
  173.          return *this;
  174.      }

  175.     Date& operator++()
  176.     {
  177.         if(_day == GetMonDay(_year,_month))
  178.         {
  179.             if(_month == 12)
  180.             {
  181.                 _year++;
  182.                 _month = 1;
  183.                 _day = 1;
  184.             }
  185.             _month++;
  186.             _day = GetMonDay(_year,_month);
  187.         }
  188.         _day++;
  189.         return *this;
  190.     }
  191.     Date operator++(int)
  192.     {
  193.         Date *tmp = this;
  194.         if(_day == GetMonDay(_year,_month))
  195.         {
  196.             if(_month == 12)
  197.             {
  198.                 _year++;
  199.                 _month = 1;
  200.                 _day = 1;
  201.             }
  202.             _month++;
  203.             _day = GetMonDay(_year,_month);
  204.         }
  205.         _day++;
  206.         return *tmp;
  207.     }

  208.     Date& operator--()
  209.     {
  210.         if(_day = 1)
  211.         {
  212.             if(_month = 1 )
  213.             {
  214.                 _year--;
  215.                 _month = 12;
  216.                 _day = GetMonDay(_year,_month);
  217.                 return *this;
  218.             }
  219.             _month--;
  220.             _day = GetMonDay(_year,_month);
  221.             return *this;
  222.         }
  223.         _day--;
  224.         return *this;
  225.     }
  226.     Date operator--(int)
  227.     {
  228.         Date *tmp = this;
  229.         if(_day = 1)
  230.         {
  231.             if(_month = 1 )
  232.             {
  233.                 _year--;
  234.                 _month = 12;
  235.                 _day = GetMonDay(_year,_month);
  236.                 return *this;
  237.             }
  238.             _month--;
  239.             _day = GetMonDay(_year,_month);
  240.             return *this;
  241.         }
  242.         _day--;
  243.         return *tmp;
  244.     }
  245.     istream& operator>>(istream& is, Date& d)
  246.     {
  247.         cout<<"?????????????????ê????:";
  248.         is>>d._year>>d._month>>d._day;

  249.         return is;
  250.     }

  251.     ostream& operator<<(ostream& os, const Date& d)
  252.     {
  253.         cout<<d._year<<"-"<<d._month<<"-"<<d._day<<endl;
  254.         return os;
  255.     }
  256. private:
  257.     int _year;
  258.     int _month;
  259.     int _day;
  260. };

阅读(1042) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~