C/C++實(shí)現(xiàn)日期計(jì)算器的示例代碼
問題介紹:
今天突然看到一個(gè)問題看起來蠻有趣的,跟大家分享一下. 給定任意日期對該日期進(jìn)行加減天數(shù),最后得出加減后出現(xiàn)的日期.以及給兩個(gè)日期你可以得出他們兩個(gè)之間相隔多少天.(需要考慮閏年,每個(gè)月天數(shù)不同,我們需要寫一個(gè)我們直接可以使用的日期加減器)因?yàn)闀r(shí)間比較倉促,我也沒有寫界面,只有其中幾個(gè)主要的函數(shù)的架構(gòu)思想以及簡單的調(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ò)了會出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é):
日期類對我們掌握面向?qū)ο筮@里還是一個(gè)蠻重要的知識,你至少要能很熟練很正確地自己快速寫出這個(gè)整個(gè)框架,然后一個(gè)一個(gè)實(shí)現(xiàn)函數(shù),我只能說很重要,很重要,很重要大家一定要掌握.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語言
本文標(biāo)題:C/C++實(shí)現(xiàn)日期計(jì)算器的示例代碼
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1192.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


