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

C++實(shí)現(xiàn)日期類(Date)

 更新時間:2018年09月10日 08:35:16   作者:WangJ_F_  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)日期類的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++實(shí)現(xiàn)日期類的具體代碼,供大家參考,具體內(nèi)容如下

#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
 //構(gòu)造函數(shù)
 Date(int year = 1900, int month = 1, int day = 1)
 :_year(year)
 , _month(month)
 , _day(day)
 {
 if (!IsInvalidDate(_year, _month, _day))
 {
  _year = 1900;
  _month = 1;
  _day = 1;
 }
 }
 //拷貝函數(shù)
 Date(const Date& d)
 : _year(d._year)
 , _month(d._month)
 , _day(d._day)
 {}
 
 //析構(gòu)函數(shù)
 ~Date()
 {}
 
 //判斷是不是閏年
 bool IsLeapYear(int year)
 {
  if ((year % 400 == 0) ||
  ((year % 4 == 0) && (year % 100 != 0)) )
  {
  return true;
  }
  return false;
 }
 //判斷是不是合法日期
 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;
 }
 //判斷當(dāng)前月份多少天
 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 && IsLeapYear(year))
  {
  day += 1;
  }
  return day;
 }
 //修正日期
 Date ToCorrect(Date &d)
 {
  while (d._day > YearsOfMonth(d._year, d._month) || d._day <= 0)
  {
  if(d._day <= 0)
  {
   d._day += YearsOfMonth(d._year,( d._month - 1));
   if (d._month == 1)
   {
   d._month = 12;
   d._year--;
   }
   else
   {
   d._month--;
   }
  }
  else
  {
   d._day -= YearsOfMonth(d._year, d._month);
   if (d._month == 12)
   {
   d._year++;
   d._month = 1;
   }
   else
   {
   d._month++;
   }
  }
  }
  return d;
 }
 // 當(dāng)前日期days天后是什么日期? 
 Date operator+(int days)
 {
  Date tmp(*this);
  tmp._day += days;
  ToCorrect(tmp);
  return tmp;
 }
 
 // 當(dāng)前日期days天前是什么日期? 
 Date operator-(int days)
 {
  Date tmp(*this);
  tmp._day -= days;
  ToCorrect(tmp);
  return tmp;
 }
 
 // 日期比大小 
 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 ||
  (_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));
 }
 bool operator!=(const Date& d)
 {
 return !(*this == d);
 }
 bool operator>=(const Date &d)
 {
 return !(*this<d);
 }
 bool operator<=(const Date &d)
 {
 return !(*this>d);
 }
 
 // 重載取地址符號 
 Date* operator&()
 {
 
 }
 
 // 前置++ 
 Date& operator++()
 {
 (*this)++;
 return *this;
 }
 
 // 后置++ 
 Date operator++(int)//通過返回值和傳參區(qū)別前置和后置++
 {
 Date tmp(*this);
 (*this) = (*this) + 1;
 return tmp;
 }
 
 // 前置-- 
 Date& operator--()
 {
 (*this)--;
 return *this;
 }
 
 // 后置-- 
 Date operator--(int)
 {
 Date tmp(*this);
 (*this)--;
 return tmp;
 }
 void Display()
 {
 cout << _year << "-" << _month << "-" << _day << endl;
 }
private:
 int _year;
 int _month;
 int _day;
};
 
int main()
{
 Date d(2018, 9, 9);
 d.Display();
 Date d1 = d + 50;
 d1.Display();
 d1 =d1 - 50;
 d1.Display();
 
 cout << "------"<<endl;
 cout << "前置++" << endl;
 d1.Display();
 (++d1).Display();
 d1.Display();
 cout << "后置++" << endl;
 d1.Display();
 (d1++).Display();
 d1.Display();
 cout << "------" << endl;
 
 
 system("pause");
 return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言實(shí)現(xiàn)簡單回聲服務(wù)器

    C語言實(shí)現(xiàn)簡單回聲服務(wù)器

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單回聲服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Linux 軟件看門狗 watchdog使用介紹

    Linux 軟件看門狗 watchdog使用介紹

    這篇文章主要介紹了Linux 軟件看門狗 watchdog使用介紹,需要的朋友可以參考下
    2016-10-10
  • 一文詳解C++模板和泛型編程

    一文詳解C++模板和泛型編程

    這篇文章主要為為大家為大家詳細(xì)的介紹了C++模板和泛型編程使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Visual Studio Code 2020安裝教程及CPP環(huán)境配置(教程圖解)

    Visual Studio Code 2020安裝教程及CPP環(huán)境配置(教程圖解)

    這篇文章主要介紹了Visual Studio Code 2020安裝教程、CPP環(huán)境配置,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Linux下用C++實(shí)現(xiàn)俄羅斯方塊

    Linux下用C++實(shí)現(xiàn)俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了Linux下用C++實(shí)現(xiàn)俄羅斯方塊的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 利用C語言實(shí)現(xiàn)順序表的實(shí)例操作

    利用C語言實(shí)現(xiàn)順序表的實(shí)例操作

    順序表是線性表中的一種重要的數(shù)據(jù)結(jié)構(gòu),也是最基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu),所以他不僅是學(xué)習(xí)中的重點(diǎn),也是應(yīng)用開發(fā)非常常用的一種數(shù)據(jù)結(jié)構(gòu)。這篇文章介紹如何利用C語言實(shí)現(xiàn)順序表。
    2016-08-08
  • visual studio 建立dll類型工程、控制臺程序

    visual studio 建立dll類型工程、控制臺程序

    這篇文章主要介紹了visual studio 建立dll、控制臺類型工程的相關(guān)知識,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05
  • 詳解原碼、反碼與補(bǔ)碼存儲與大小

    詳解原碼、反碼與補(bǔ)碼存儲與大小

    這篇文章主要介紹了詳解原碼、反碼與補(bǔ)碼存儲與大小的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • C語言小程序 楊輝三角示例代碼

    C語言小程序 楊輝三角示例代碼

    輸入要顯示的楊輝三角的行數(shù),會打印出金字塔型的楊輝三角,不過行數(shù)太多的話,效果不太好,可以再調(diào)整一下格式控制
    2013-07-07
  • VC定制個性化的MessageBox解決方法

    VC定制個性化的MessageBox解決方法

    這篇文章主要介紹了VC定制個性化的MessageBox解決方法,有助于進(jìn)一步的了解windows應(yīng)用程序的消息機(jī)制及運(yùn)行原理,需要的朋友可以參考下
    2014-07-07

最新評論