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

C++實現(xiàn)日期類的方法詳解

 更新時間:2024年01月08日 09:51:09   作者:小王又困了  
這篇文章主要給大家介紹了C++實現(xiàn)日期類的方法,文中通過代碼示例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

一、日期類的實現(xiàn)

1.1日期類功能

頭文件中是我們要實現(xiàn)日期類功能的函數(shù)聲明。這里我們要注意拷貝函數(shù),只能在函數(shù)聲明時寫缺省值,防止我們在聲明和定義是給的缺省值不一樣。

#include <iostream>
#include <assert.h>
using namespace std;
 
class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1);
 
    void Print();
    int GetMonthDay(int year, int month);
 
    bool operator==(const Date& y);
    bool operator!=(const Date& y);
    bool operator>(const Date& y);
    bool operator<(const Date& y);
    bool operator>=(const Date& y);
    bool operator<=(const Date& y);
 
    int operator-(const Date& d);
    Date& operator+=(int day);
    Date operator+(int day);
    Date& operator-=(int day);
    Date operator-(int day);
 
    Date& operator++();
    Date operator++(int);
 
    Date& operator--();
    Date operator--(int);
private:
    int _year;
    int _month;
    int _day;
};

1.2拷貝日期

有時輸入的日期可能是非法的,例如:月份大于12,日期大于31,還有閏2月天數(shù)等。所以我們要對輸入的日期進行判斷,因為每個月的天數(shù)不同,所以要用到GetMonthDay函數(shù)。

Date::Date(int year, int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
 
    if (_year < 1 || 
        _month < 1 || _month>12 || 
        _day < 1 || _day > GetMonthDay(_year, _month))
	{
        //assert(false);
        Print();
        cout << "日期非法" << endl;
	}
}
 
int Date::GetMonthDay(int year, int month)
{
    assert(year >= 1 && month >= 1 && month <= 12);
 
    int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };
 
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
        return 29;
 
    return monthArray[month];
}

小Tips:我們先判斷是否是二月,在判斷是否是閏年可以提高效率。 

1.3重載關(guān)系運算符

關(guān)系運算符有:<、>、==、<=、>=、!=,我們在寫實現(xiàn)這些功能的代碼時,會發(fā)現(xiàn)它們邏輯相同,會復(fù)制粘貼,但這樣的代碼看著十分冗余,我們可以通過復(fù)用來實現(xiàn),讓代碼更簡單。

重載==

bool Date::operator==(const Date& y)
{
    return _year == y._year
        && _month == y._month
        && _day == y._day;
}

重載!=

bool Date::operator!=(const Date& y)
{
    return !(*this == y);
}

重載<

bool Date::operator<(const Date& d)
{
    if (_year < d._year)
    {
        return true;
    }
    else if (_year == d._year && _month < d._month)
    {
        return true;
    }
    else if (_year == d._year && _month < d._month && _day < d._day)
    {
        return true;
    }
    else
    {
        return false;
    }
}

重載<=

bool Date::operator<=(const Date& y)
{
    return *this < y || *this == y;
}

重載>

bool Date::operator>(const Date& y)
{
    return !(*this <= y);
}

重載>=

bool Date::operator>=(const Date& y)
{
    return !(*this < y);
}

1.4重載+、+=

我們想知道50天之后的日期,就可以通過重載+、+=來實現(xiàn)。在加天數(shù)的時候,由于每個月的天數(shù)不一樣,所以進位就不同,我們要得到每個月份的天數(shù)。

獲得月份的天數(shù)

int Date::GetMonthDay(int year, int month)
{
    assert(year >= 1 && month >= 1 && month <= 12);
 
    int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };
 
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
        return 29;
 
    return monthArray[month];
}

小Tips:把month == 2放在前面判斷可以提高效率,如果不是二月就不需要判斷是否是閏年,如果是二月在判斷。

重載+=

Date& Date::operator+=(int day)
{
    //天數(shù)為負數(shù)時,復(fù)用-=
    if (day < 0)
    {
        return *this -= (-day);
    }
 
    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
        _day -= GetMonthDay(_year, _month);
        _month++;
        if (_month == 13)
        {
            _year++;
            _month = 1;
        }
    }
    return *this;
}

重載+

Date Date::operator+(int day)
{
    if(x < 0)
    {
        return *this - (-day);
    }
 
    Date tmp(*this);
    tmp._day = _day + x;
    while (tmp._day > GetDay(tmp._year, tmp._month))
    {
        tmp._day = tmp._day - GetDay(tmp._year, tmp._month);
        tmp._month++;
        if (tmp._month == 13)
        {
            tmp._year++;
            tmp._month = 1;
        }
    }
    return tmp;
}

小Tips:重載+是原來的日期不會改變,所以我們拷貝構(gòu)造了一個和*this相同的對象,我們對tmp對象修改,不會改變*this。重載+=是在原來的日期上直接修改,所以我們直接對*this指向的日期進行修改,然后返回就可以。

為什么重載+=用引用返回?

在重載+=中,*this就是d1,它的作用域是函數(shù)結(jié)束后才銷毀,由于傳值返回會拷貝一份返回值,所以為了減少返回時的拷貝,所以使用引用返回。在重載+中,tmp出了operator+函數(shù)就被銷毀,所以只能使用傳值返回。

++=之間的復(fù)用

  • +復(fù)用+=
Date Date::operator+(int day)
{
    Date tmp(*this);
    tmp += day;
    return tmp;
}

1.5重載 -、-=

有時我們想知道以前的日期,日期-天數(shù)可以知道多少天以前的日期,日期-日期可以知道兩個日期直接隔了多少天。兩個operator-函數(shù)構(gòu)成了函數(shù)重載。

重載-=

Date& operator-=(int x)
{
    //天數(shù)天數(shù)小于0,復(fù)用+=
    if (x < 0)
    {
        return *this += -x;
    }
    _day -= x;
    while (_day <= 0)
    {
        _month--;
        if (_month == 0)
        {
            _month = 12;
            _year--;
	    }
        _day += GetDay(_year, _month);
    }
 
    return *this;
}

重載日期-天數(shù)

Date Date::operator-(int x) 
{
    Date tmp(*this);
 
    return tmp -= x;
}

重載日期-日期

日期-日期,計算的結(jié)果是兩個日期之間的天數(shù),所以返回值是int,要知道兩個日期之間相隔的天數(shù),可以設(shè)置一個計數(shù)器,讓小日期一直加到大日期,就可以知道兩個日期之間相隔的天數(shù)。

int operator-(const Date& d) 
{
    Date max = *this;//存放大日期
    Date min = d;//存放小日期
    int flag = 1;
    if (*this < d)
    {
        max = d;
        min = *this;
        flag = -1;
    }
    int n = 0;
    while (max != min)
    {
        --max;
        ++n;
    }
 
    return n * flag;
}

1.6重載++、--

重載前置++

前置++要返回++之后的值

Date& Date::operator++()
{
    *this += 1;//復(fù)用+=
    return *this;
}

重載前置++

后置++要返回++之前的值

Date Date::operator++(int)//編譯器會把有int的視為后置++
{
    Date tmp(*this);
    *this += 1;//復(fù)用+=
    return tmp;
}

小Tips:這兩個operator++函數(shù)構(gòu)成了函數(shù)重載,那調(diào)用的時候怎么區(qū)分前置++和后置++呢?后置重載的時候多增加一個int類型的參數(shù),使用后置++時,調(diào)用運算符重載函數(shù)時不用傳遞參數(shù),編譯器自動傳遞。

重載前置 -- 

前置--要返回--之后的值

Date& operator--()
{
    *this -= 1;//復(fù)用-=
    return *this;
}

重載后置 -- 

后置--要返回--之前的值

Date operator--(int)
{
    Date tmp(*this);
    *this -= 1;//復(fù)用了-=
    return tmp;
}

結(jié)語: 

到此這篇關(guān)于C++實現(xiàn)日期類的方法詳解的文章就介紹到這了,更多相關(guān)C++實現(xiàn)日期類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++使用Mutex實現(xiàn)讀寫鎖思路

    C++使用Mutex實現(xiàn)讀寫鎖思路

    這篇文章主要介紹了C++使用Mutex實現(xiàn)讀寫鎖思路,詳細介紹了讀寫鎖需要具有的特征,通過實例代碼給大家講解的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-05-05
  • 基于結(jié)構(gòu)體與指針的詳解

    基于結(jié)構(gòu)體與指針的詳解

    本篇文章是對結(jié)構(gòu)體與指針進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言實現(xiàn)電子秒表

    C語言實現(xiàn)電子秒表

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)電子秒表,毫秒級秒表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C語言中操作密碼文件的一些函數(shù)總結(jié)

    C語言中操作密碼文件的一些函數(shù)總結(jié)

    這篇文章主要介紹了C語言中操作密碼文件的一些函數(shù)總結(jié),包括setpwent()函數(shù)和getpwent()函數(shù)以及endpwent()函數(shù),需要的朋友可以參考下
    2015-08-08
  • C++ 實現(xiàn)線程安全的頻率限制器(推薦)

    C++ 實現(xiàn)線程安全的頻率限制器(推薦)

    這篇文章主要介紹了在 C++ 中實現(xiàn)一個線程安全的頻率限制器,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • C語言推箱子游戲?qū)崿F(xiàn)代碼

    C語言推箱子游戲?qū)崿F(xiàn)代碼

    這篇文章主要為大家詳細介紹了C語言推箱子游戲?qū)崿F(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 通俗易懂講解C語言與Java中二叉樹的三種非遞歸遍歷方式

    通俗易懂講解C語言與Java中二叉樹的三種非遞歸遍歷方式

    二叉樹是一種非常重要的數(shù)據(jù)結(jié)構(gòu),很多的數(shù)據(jù)結(jié)構(gòu)都是基于二叉樹的基礎(chǔ)演變過來的。二叉樹的前,中,后3種遍歷方式,因為樹的定義本身就是遞歸定義的,所以采用遞歸的方法來實現(xiàn)是很簡單的
    2021-09-09
  • C++詳細實現(xiàn)完整圖書管理功能

    C++詳細實現(xiàn)完整圖書管理功能

    隨著網(wǎng)絡(luò)技術(shù)的高速發(fā)展,計算機應(yīng)用的普及,利用計算機對圖書館的日常工作進行管理勢在必行,本篇文章涵蓋一個圖書管理系統(tǒng)的全部實現(xiàn)代碼,大家可以查缺補漏,提升水平
    2022-05-05
  • C++解析wav文件方法介紹

    C++解析wav文件方法介紹

    最近將項目改為跨平臺,于是音頻模塊從微軟的XAudio2改用OpenAL庫。之前使用MSDN的代碼,所以現(xiàn)在改為了C++標準的寫法,適用性更廣
    2022-09-09
  • 利用C語言實現(xiàn)經(jīng)典多級時間輪定時器

    利用C語言實現(xiàn)經(jīng)典多級時間輪定時器

    C語言是一門通用計算機編程語言,廣泛應(yīng)用于底層開發(fā),這篇文章主要給大家介紹了關(guān)于利用C語言實現(xiàn)經(jīng)典多級時間輪定時器的相關(guān)資料,需要的朋友可以參考下
    2021-07-07

最新評論

泗阳县| 星子县| 大姚县| 运城市| 灵璧县| 舞钢市| 大田县| 宜丰县| 永清县| 南靖县| 昭平县| 海口市| 六枝特区| 久治县| 泽州县| 黄山市| 柳江县| 奉节县| 莱西市| 丰镇市| 东乡族自治县| 丰宁| 华亭县| 崇信县| 新余市| 湟源县| 揭阳市| 桐城市| 洛南县| 苍溪县| 昌乐县| 威信县| 即墨市| 保靖县| 沙河市| 蓬溪县| 克东县| 商洛市| 隆尧县| 尚志市| 青浦区|