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

C/C++實(shí)現(xiàn)日期計(jì)算器的示例代碼

 更新時(shí)間:2017年09月08日 11:00:09   作者:Dawn_sf  
本篇文章主要介紹了C/C++實(shí)現(xiàn)日期計(jì)算器的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

問題介紹:

今天突然看到一個(gè)問題看起來蠻有趣的,跟大家分享一下. 給定任意日期對(duì)該日期進(jìn)行加減天數(shù),最后得出加減后出現(xiàn)的日期.以及給兩個(gè)日期你可以得出他們兩個(gè)之間相隔多少天.(需要考慮閏年,每個(gè)月天數(shù)不同,我們需要寫一個(gè)我們直接可以使用的日期加減器)因?yàn)闀r(shí)間比較倉促,我也沒有寫界面,只有其中幾個(gè)主要的函數(shù)的架構(gòu)思想以及簡(jiǎn)單的調(diào)試就發(fā)出來了.

代碼實(shí)現(xiàn):

#include<iostream> 
#include<Windows.h> 
#include<assert.h> 
using namespace std; 
 
class Date 
{ 
 
public: 
 
  Date(int year = 1997,int month = 1,int day = 1) 
  :years(year) 
  , months(month) 
  , days(day) 
  { 
    assert(IScorrect()); 
  } 
 
  Date& operator=(const Date& d) 
  { 
    if (this != &d) 
    { 
      years = d.years; 
      months = d.months; 
      days = d.days; 
    } 
    return *this; 
  } 
 
  Date& operator + (int day) 
  { 
    while (day > 365) 
    { 
      if (ISleapyear() && day > 366) 
      { 
        years++; 
        day = day - 366; 
      } 
      else 
      { 
        years++; 
        day = day - 365; 
      } 
    } 
    while (day >= Getmonthsday()) 
    {   
      //注意這里的次序問題,一定先減 再加 最后再判斷. 如果順序錯(cuò)了會(huì)出BUG的. 
      day = day - Getmonthsday();  
      months++; 
      if (months > 12) 
      { 
        years++; 
        months = 1; 
      } 
    } 
 
    while (day > 0) 
    {   
      DateAdvance(); 
      day = day - 1; 
      days++; 
    } 
    return *this; 
  } 
 
  Date& operator - (int day) //先減去一年,然后在使用加的重載,所以你只需要寫一個(gè)無懈可擊的加算法就夠了. 
  { 
    while (day > 365) 
    { 
      if (ISleapyear() && day > 366) 
      {       
        day = day - 366; 
        years--; 
      } 
      else 
      { 
        day = day - 365; 
        years--; 
      } 
    } 
    if (ISleapyear()) 
    { 
      day = 366 - day; 
      years--; 
    } 
    else 
    { 
      day = 365 - day; 
      years--; 
    } 
    operator+(day); 
    return *this; 
  } 
 
  void DateAdvance() //用于出現(xiàn)可以進(jìn)化的情況 
  { 
    if (days > Getmonthsday()) 
    { 
      months++; 
      days = 1; 
    } 
    if (months > 12) 
    { 
      years++; 
      months = 1; 
    } 
  } 
   
  int operator - (Date D) 
  { 
    int count = 0; 
    if (*this > D) 
    { 
      while (*this != D) 
      { 
        D.operator+(1); 
        count++; 
      } 
    } 
    else 
    { 
      while (*this != D) 
      { 
        operator+(1); 
        count++; 
      } 
    } 
    return count; 
  } 
 
  bool ISleapyear() 
  { 
    if ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) 
    { 
      return true; 
    } 
    return false; 
  } 
  int Getmonthsday() 
  { 
    int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 
    if (ISleapyear() && months == 2) 
    { 
      return 29; 
    } 
    return monthDays[months]; 
  } 
 
  void print() 
  { 
    cout << "目前的時(shí)間為"; 
    cout << years << "." << months << "." <<days<< endl; 
  } 
 
  bool IScorrect() 
  { 
    if (years > 0 && ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) && days < 367)//閏年 
    { 
      if (months >0 && months < 13) 
      { 
        if (days > 0 && days <= Getmonthsday()) 
        { 
          return true; 
        } 
      } 
    } 
    else if (years >0 && days < 366) //非閏年 
    { 
      if (months >0 && months < 13) 
      { 
        if (days > 0 && days <= Getmonthsday()) 
        { 
          return true; 
        } 
      } 
    } 
    return false; 
  } 
 
  Date operator += (int day) 
  { 
    *this = *this + 100; 
    return *this; 
  } 
  Date operator -= (int day) 
  { 
    return *this = *this - day; 
  } 
  inline Date& operator++() 
  { 
    *this += 1; 
    return *this; 
  } 
  inline Date operator++(int) 
  { 
    Date tmp(*this); 
    *this = *this + 1; 
    return tmp; 
  } 
 
  bool operator == (const Date& d) 
  { 
    return (years == d.years&& months == d.months&&days == d.days); 
  } 
 
  bool operator != (const Date& d) 
  { 
    return !(*this == d); 
  } 
 
  bool operator >(const Date& d) 
  { 
    if (years > d.years || 
      (years == d.years&&months > d.months) 
      || (years == d.years&&months == d.months && days > d.days)) 
    { 
      return true; 
    } 
    return false; 
  } 
 
  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) && (*this < d); 
  } 
 
private: 
  int years; 
  int months; 
  int days; 
}; 
 
void Test() 
{ 
  Date d1(2012, 4, 5); 
  Date d2(2013, 4, 5); 
  d1.print(); 
  /*d1 = d1 - 400;*/ 
  d1.print(); 
  cout << d1 - d2 << endl; 
  d1.print(); 
  system("pause"); 
} 

總結(jié):

日期類對(duì)我們掌握面向?qū)ο筮@里還是一個(gè)蠻重要的知識(shí),你至少要能很熟練很正確地自己快速寫出這個(gè)整個(gè)框架,然后一個(gè)一個(gè)實(shí)現(xiàn)函數(shù),我只能說很重要,很重要,很重要大家一定要掌握.

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

相關(guān)文章

  • Mygui中文換行問題解決方案

    Mygui中文換行問題解決方案

    相信大家解決了中文輸入后一定會(huì)遇到如何解決中文輸入的問題,中文輸入換行問題是很多gui框架都存在的一個(gè)問題,需要的朋友可以了解下
    2012-11-11
  • C語言實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn)

    C語言實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 淺談C++的淺拷貝出現(xiàn)的錯(cuò)誤

    淺談C++的淺拷貝出現(xiàn)的錯(cuò)誤

    下面小編就為大家?guī)硪黄獪\談C++的淺拷貝出現(xiàn)的錯(cuò)誤。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • 一起來了解c語言的str函數(shù)

    一起來了解c語言的str函數(shù)

    這篇文章主要為大家詳細(xì)介紹了c語言的str函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • C++實(shí)現(xiàn)圖片轉(zhuǎn)base64的示例代碼

    C++實(shí)現(xiàn)圖片轉(zhuǎn)base64的示例代碼

    Base64就是一種 基于64個(gè)可打印字符來表示二進(jìn)制數(shù)據(jù)的表示方法,本文主要為大家詳細(xì)介紹了如何使用C++實(shí)現(xiàn)圖片轉(zhuǎn)base64,需要的可以參考下
    2024-04-04
  • 基于C語言實(shí)現(xiàn)掃雷小游戲

    基于C語言實(shí)現(xiàn)掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了基于C語言實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C語言實(shí)現(xiàn)手寫紅黑樹的示例代碼

    C語言實(shí)現(xiàn)手寫紅黑樹的示例代碼

    紅黑樹在表意上就是一棵每個(gè)節(jié)點(diǎn)帶有顏色的二叉搜索樹,并通過對(duì)節(jié)點(diǎn)顏色的控制,使該二叉搜索樹達(dá)到盡量平衡的狀態(tài)。本文主將用C語言實(shí)現(xiàn)手寫紅黑樹,需要的可以參考一下
    2022-09-09
  • 詳解C++編程中表達(dá)式的語義與計(jì)算順序

    詳解C++編程中表達(dá)式的語義與計(jì)算順序

    這篇文章主要介紹了C++編程中表達(dá)式的語義與計(jì)算順序,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-01-01
  • Opencv基于文字檢測(cè)去圖片水印的實(shí)現(xiàn)示例

    Opencv基于文字檢測(cè)去圖片水印的實(shí)現(xiàn)示例

    去水印是個(gè)麻煩事,本文就來介紹一種方法Opencv基于文字檢測(cè)去圖片水印的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • QT5實(shí)現(xiàn)UDP通信的示例代碼

    QT5實(shí)現(xiàn)UDP通信的示例代碼

    本文主要介紹了QT5實(shí)現(xiàn)UDP通信的示例代碼,主要使用QUdpSocket類用于實(shí)現(xiàn)UDP通信,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12

最新評(píng)論