最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實現(xiàn)日期類(Date類)的方法

 更新時間:2017年01月11日 08:48:56   投稿:jingxian  
下面小編就為大家?guī)硪黄狢++實現(xiàn)日期類(Date類)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

如下所示:

#include<iostream>
using namespace std;
class Date
{
public:
  Date(int year = 1900, int month = 1, int day = 1)  //構(gòu)造
  :_year(year)
  , _month(month)
  , _day(day)
  {
    if (!isInvalidDate(_year, _month, _day))
    {
      _year = 1900;
      _month = 1;
      _day = 1;
    }
  }
  
  Date operator+(int count)
  { 
    Date tmp(*this);
    tmp._day += count;
    ToCorrect(tmp);
    return tmp;
  }
  Date operator-(int count)
  {
    Date tmp(*this);
    tmp._day -= count;
    ToCorrect(tmp);
    return tmp;
  }
  
  Date& operator++()  //前置++
  {
    (*this)++;
    return *this;
  }
  Date operator++(int)  //后置++
  {
    Date tmp(*this);
    (*this)+=1;
    return tmp;
  }
  Date& operator--()
  {
    (*this)--;
    return *this;
  }
  Date operator--(int)
  {
    Date tmp(*this);
    (*this)--;
    return tmp;
  }
  int operator-(const Date &d)
  {
    int flag = 1;
    Date max = *this;
    Date min = d;
    if (*this<d)
    {
      max = d;
      min = *this;
      flag = -1;
    }
    int count=0;
    while (min != max)
    {
      ++min;
      count++;
    }
    return count*flag;
  }
  Date& operator+=(int day)
  {
    *this = *this + day;
    return *this;
  }
  bool operator!=(const Date &d)
  {
    return !(*this == d);
  }
  bool operator<(const Date &d)
  {
    return !((*this>d)||(*this==d));
  }
  bool operator>=(const Date &d)
  {
    return !(*this<d);
  }
  bool operator>(const Date &d)
  {
    return (_year > d._year
      || (_year == d._year && _month > d._month)
      || (_year == d._year && _month == d._month && _day > d._day));
  }
  bool operator==(const Date &d)
  {
    return ((_year == d._year) && (_month == d._month) && (_day == d._day));
  }
  
public:  
  bool IsLeapYear(int year)
  {
    if(year % 400 || (year % 4 && year % 100))
      return true;
    return false;
  }
  int YearsOfMonth(int year, int month)
  {
    int day;
    int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    day = days[month];
    if (month == 2)
      day += IsLeapYear(year);
    return day;
  }
  
  Date ToCorrect(Date &d)
  {
    if (d._day>YearsOfMonth(d._year, d._month))
    {
      while (d._day > YearsOfMonth(d._year, d._month))
      {
         d._day -= YearsOfMonth(d._year,d._month);

        if (d._month == 12)
        {
          d._year++;
          d._month = 1;
        }
        else
        {
          ++d._month;
        }
      }
    }
    else
    {
      while (d._day <= 0)
      {
        if (d._month == 1)
        {
          d._year--;
          d._month = 12;
        }
        else
        {
          --d._month;
        }
        d._day += YearsOfMonth(d._year, d._month);
      }
    }
    return d;
  }
  

  bool isInvalidDate(int year, int month, int day)
  {
    if ((year < 1) || (month<0 || month>12) || (day<0 || day>YearsOfMonth(year,month)))
      return false;
    return true;
  }
  void Display()
  {
    cout << _year << "-" << _month << "-" << _day << endl;
  }
  friend istream& operator>>(istream& is, Date &d);
  friend ostream& operator<<(ostream& os, const Date &d);
private:
  int _year;
  int _month;
  int _day;
};
istream& operator>>(istream& is, Date& d)
{
  cout << "請輸入一個日期" << endl;
  is >> d._year >> d._month >> d._day;
  return is;
}

ostream& operator<<(ostream& os, const Date &d)
{
  cout << d._year << "-" <<d. _month << "-" << d._day << endl;
  return os;
}
int main()
{
  /*Date d1(2016,8,18);
  //d1.Display();

  //d1 = d1++;
  cout << d1 << endl;*/

  //Date d1(2015, 12, 3);
  //(d1++).Display(); //d1.operator++(&d1, 0);
  //(++d1).Display(); //d1.operator++(&d1);

  Date d1(2015, 12, 3);
  Date d2(2015, 11, 1);
  cout << (d1 - d2) << endl;

  //Date d1(2015, 12, 3);
  //Date ret = d1 + 40; //operator+
  //ret.Display();


  /*Date d1(2015, 12, 3);
  Date ret = d1 + 40;
  d1 = ret;
  ret = d1 - 40;
  ret.Display();*/
  
  /*Date ret;
  Date d2(2015, 1, 1);
  ret = d2 - 1;
  ret.Display();*/
  return 0;
}

以上這篇C++實現(xiàn)日期類(Date類)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言文件讀寫操作介紹與簡單示例

    C語言文件讀寫操作介紹與簡單示例

    這篇文章主要給大家介紹了關(guān)于C語言文件讀寫操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C語言實現(xiàn)消消樂游戲的代碼分享

    C語言實現(xiàn)消消樂游戲的代碼分享

    本章我們將編寫十字消除游戲,用戶點擊空白方塊,沿其上下左右方向?qū)ふ业谝粋€彩色方塊,如果有兩個或兩個以上顏色一致,就將其消除,感興趣的可以了解一下
    2023-02-02
  • C語言深入講解函數(shù)的使用

    C語言深入講解函數(shù)的使用

    各位小伙伴們,今天YU同學(xué)給大家?guī)淼氖桥c函數(shù)相關(guān)的知識,本篇將會帶著大家初步認(rèn)識和調(diào)用函數(shù)來解決一些簡單的問題
    2022-04-04
  • C語言異常處理機制案例講解

    C語言異常處理機制案例講解

    這篇文章主要介紹了C語言異常處理機制案例講解,本文講解了異常處理機制所用的函數(shù)和具體的代碼實現(xiàn)等,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 關(guān)于win32 gettimeofday替代方案

    關(guān)于win32 gettimeofday替代方案

    下面小編就為大家?guī)硪黄P(guān)于win32 gettimeofday替代方案。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • C語言指針超詳細(xì)講解下篇

    C語言指針超詳細(xì)講解下篇

    指針提供了對地址操作的一種方法,因此,使用指針可使得?C?語言能夠更高效地實現(xiàn)對計算機底層硬件的操作。另外,通過指針可以更便捷地操作數(shù)組。在一定意義上可以說,指針是?C?語言的精髓
    2022-04-04
  • c與c++之間的相互調(diào)用及函數(shù)區(qū)別示例詳解

    c與c++之間的相互調(diào)用及函數(shù)區(qū)別示例詳解

    這篇文章主要為大家介紹了c與c++相互調(diào)用的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • C語言動態(tài)順序表實例代碼

    C語言動態(tài)順序表實例代碼

    大家好,本篇文章主要講的是C語言動態(tài)順序表實例代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • opencv求解區(qū)域的內(nèi)接矩形

    opencv求解區(qū)域的內(nèi)接矩形

    這篇文章主要為大家詳細(xì)介紹了opencv求解區(qū)域的內(nèi)接矩形,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C語言實現(xiàn)繪制可愛的橘子鐘表

    C語言實現(xiàn)繪制可愛的橘子鐘表

    這篇文章主要為大家詳細(xì)介紹了如何利用C語言實現(xiàn)繪制可愛的橘子鐘表,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的可以了解一下
    2022-12-12

最新評論

塘沽区| 全南县| 凤山市| 兰坪| 永福县| 翁牛特旗| 武山县| 辰溪县| 灯塔市| 石棉县| 错那县| 滨海县| 美姑县| 北京市| 客服| 丰原市| 比如县| 乌兰察布市| 平舆县| 西昌市| 合山市| 堆龙德庆县| 沁阳市| 九江县| 汝南县| 乌兰浩特市| 赤城县| 思茅市| 白银市| 沈丘县| 江永县| 莱阳市| 天祝| 弥渡县| 垦利县| 庆安县| 海林市| 收藏| 宜章县| 金乡县| 宁化县|