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

問題介紹:

創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計,港北網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:港北等地區(qū)。港北做網(wǎng)站價格咨詢:13518219792

今天突然看到一個問題看起來蠻有趣的,跟大家分享一下. 給定任意日期對該日期進行加減天數(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)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享名稱:C/C++實現(xiàn)日期計算器的示例代碼
分享地址:http://muchs.cn/article44/iehiee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、網(wǎng)站內(nèi)鏈關(guān)鍵詞優(yōu)化、企業(yè)建站、動態(tài)網(wǎng)站企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)