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

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

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

問題介紹:

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

代碼實現(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()) 
    {   
      //注意這里的次序問題,一定先減 再加 最后再判斷. 如果順序錯了會出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) //先減去一年,然后在使用加的重載,所以你只需要寫一個無懈可擊的加算法就夠了. 
  { 
    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)可以進化的情況 
  { 
    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 << "目前的時間為"; 
    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é):

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

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

相關(guān)文章

  • c++詳細講解構(gòu)造函數(shù)的拷貝流程

    c++詳細講解構(gòu)造函數(shù)的拷貝流程

    拷貝構(gòu)造函數(shù)是一種特殊的構(gòu)造函數(shù),它在創(chuàng)建對象時,是使用同一類中之前創(chuàng)建的對象來初始化新創(chuàng)建的對象。拷貝構(gòu)造函數(shù)通常用于:通過使用另一個同類型的對象來初始化新創(chuàng)建的對象。?復(fù)制對象把它作為參數(shù)傳遞給函數(shù)。復(fù)制對象,并從函數(shù)返回這個對象
    2022-05-05
  • 詳解C++中單繼承與多繼承的使用

    詳解C++中單繼承與多繼承的使用

    C++的繼承機制相對其他語言是比較復(fù)雜的一種,不同于java只支持單繼承,C++不僅支持單繼承,也支持多繼承。本文將詳細講解C++中單繼承與多繼承的使用,需要的可以參考一下
    2022-04-04
  • C語言實現(xiàn)括號配對的方法示例

    C語言實現(xiàn)括號配對的方法示例

    本文主要介紹了C語言實現(xiàn)括號配對的方法示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 獲取本地網(wǎng)卡適配器信息具體代碼

    獲取本地網(wǎng)卡適配器信息具體代碼

    這篇文章主要介紹了獲取本地網(wǎng)卡適配器信息具體代碼,有需要的朋友可以參考一下
    2013-12-12
  • 利用C語言實現(xiàn)http服務(wù)器(Linux)

    利用C語言實現(xiàn)http服務(wù)器(Linux)

    本文將利用C語言實現(xiàn)一個輕量級的http服務(wù)器,使用Reactor模式,即主線程只負責(zé)監(jiān)聽文件描述符上是否有事件發(fā)生,有的話立即將該事件通知工作線程,感興趣的可以了解一下
    2022-07-07
  • C語言菜鳥基礎(chǔ)教程之單精度浮點數(shù)與雙精度浮點數(shù)

    C語言菜鳥基礎(chǔ)教程之單精度浮點數(shù)與雙精度浮點數(shù)

    在C語言中,單精度浮點數(shù)(float)和雙精度浮點數(shù)(double)類型都是用來儲存實數(shù)的,雙精度是用記憶較多,有效數(shù)字較多,數(shù)值范圍較大。
    2017-10-10
  • C語言中sizeof和strlen的區(qū)別詳解

    C語言中sizeof和strlen的區(qū)別詳解

    這篇文章主要介紹了C語言中sizeof和strlen的區(qū)別,文中有通過代碼示例和相關(guān)例題給大家介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • Opencv實現(xiàn)對象提取與測量

    Opencv實現(xiàn)對象提取與測量

    這篇文章主要為大家詳細介紹了基于Opencv實現(xiàn)對象提取與測量,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C++庫std::flush的具體使用

    C++庫std::flush的具體使用

    std::flush是C++標準庫中的一個操作符,用于刷新輸出流,本文主要介紹了C++庫std::flush的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • VC使用編譯時間作為版本號標識的方法

    VC使用編譯時間作為版本號標識的方法

    這篇文章主要介紹了VC使用編譯時間作為版本號標識的方法,需要的朋友可以參考下
    2017-03-03

最新評論

乐业县| 乌鲁木齐县| 定日县| 武陟县| 二手房| 海盐县| 高安市| 鄱阳县| 太湖县| 金堂县| 泽库县| 土默特右旗| 金湖县| 龙井市| 鞍山市| 交口县| 三都| 泗阳县| 土默特右旗| 依兰县| 荃湾区| 理塘县| 清流县| 宜兰市| 浦县| 哈密市| 华池县| 深泽县| 安仁县| 南汇区| 武隆县| 延寿县| 灵川县| 洪江市| 赤壁市| 临清市| 延川县| 旌德县| 泸溪县| 莎车县| 万全县|