欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • VC基于ADO技術(shù)訪問數(shù)據(jù)庫的方法

    VC基于ADO技術(shù)訪問數(shù)據(jù)庫的方法

    這篇文章主要介紹了VC基于ADO技術(shù)訪問數(shù)據(jù)庫的方法,較為詳細(xì)的分析了VC使用ADO操作數(shù)據(jù)庫的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • C語言的動態(tài)內(nèi)存管理你了解嗎

    C語言的動態(tài)內(nèi)存管理你了解嗎

    這篇文章主要為大家詳細(xì)介紹了C語言的動態(tài)內(nèi)存管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 詳解C++ 拷貝構(gòu)造函數(shù)

    詳解C++ 拷貝構(gòu)造函數(shù)

    這篇文章主要介紹了C++ 拷貝構(gòu)造函數(shù)的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C++數(shù)據(jù)結(jié)構(gòu)之堆詳解

    C++數(shù)據(jù)結(jié)構(gòu)之堆詳解

    本文詳細(xì)講解了C++數(shù)據(jù)結(jié)構(gòu)之堆,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C語言實現(xiàn)BMP圖像處理(直方圖均衡化)

    C語言實現(xiàn)BMP圖像處理(直方圖均衡化)

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)BMP圖像直方圖均衡化處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Linux中利用c語言刪除某個目錄下的文件

    Linux中利用c語言刪除某個目錄下的文件

    這篇文章主要給大家介紹了Linux中利用c語言刪除某個目錄下文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C語言各種符號的使用介紹上篇

    C語言各種符號的使用介紹上篇

    C 語言的基本符號就有 20 多個,每個符號可能同時具有多重含義,而且這些符號之間相互組合又使得 C 語言中的符號變得更加復(fù)雜起來
    2022-08-08
  • 新手socket編程入門詳解指南

    新手socket編程入門詳解指南

    本文,將一步一步引導(dǎo)初學(xué)者來學(xué)習(xí)socket,所有編程思路都結(jié)合在socket API里面,以及提供socket的疑問和基礎(chǔ)知識點,同時在最后給出多個例程,下面可以和小編一起學(xué)習(xí)
    2019-05-05
  • C++實現(xiàn)LeetCode(157.用Read4來讀取N個字符)

    C++實現(xiàn)LeetCode(157.用Read4來讀取N個字符)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(157.用Read4來讀取N個字符),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • win32 api實現(xiàn)2048游戲示例

    win32 api實現(xiàn)2048游戲示例

    這篇文章主要介紹了win32 api實現(xiàn)2048游戲示例,需要的朋友可以參考下
    2014-05-05

最新評論