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

基于C++實(shí)現(xiàn)一個(gè)日期計(jì)算器

 更新時(shí)間:2022年10月10日 08:27:04   作者:蔣靈瑜的筆記本  
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)一個(gè)簡單的日期計(jì)算器,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下

一、日期計(jì)算器的功能

實(shí)現(xiàn)日期類的==、!=、+=、+、-=、-、>=、>、<=、<、前置++和--、后置++和--。

二、獲取每個(gè)月的天數(shù)

int GetMonthDay(int year, int month)
{
    //靜態(tài)數(shù)組,每次調(diào)用不用頻繁在棧區(qū)創(chuàng)建數(shù)組
    static int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    //判斷是否閏年
    int day = monthArr[month - 1];
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
    {
        day = 29;
    }
    return day;
}

1、因?yàn)镚etMonthDay這個(gè)函數(shù)需要在日期類中被頻繁調(diào)用,所以將 monthArr存放至靜態(tài)區(qū),減少數(shù)組頻繁開辟、銷毀空間的開銷。

三、Date類中的默認(rèn)成員函數(shù)

1、構(gòu)造函數(shù)

Date(int year = 1, int month = 1, int day = 1)
{
    if (year >= 1 && month >= 1 && day >= 1 && day <= GetMonthDay(year, month))
    {
        _year = year;
        _month = month;
        _day = day;
        //cout << "構(gòu)造成功" << endl;
    }
    else
    {
        cout << "日期不合法" << endl;
    }
}

日期類的構(gòu)造函數(shù)需要對(duì)日期的的合法性進(jìn)行判斷。

2、析構(gòu)函數(shù)

~Date()//可不寫
{
    ;
}

日期類因?yàn)闆]有申請(qǐng)資源(動(dòng)態(tài)開辟空間、文件的打開等),所以無需寫析構(gòu)函數(shù),系統(tǒng)默認(rèn)生成的就可以。

3、拷貝構(gòu)造

Date(const Date& d)//可不寫
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
    //cout << "拷貝構(gòu)造成功" << endl;
}

系統(tǒng)默認(rèn)生成的拷貝構(gòu)造函數(shù)會(huì)對(duì)內(nèi)置類型完成淺拷貝,所以內(nèi)置類型也可以不用寫,用系統(tǒng)默認(rèn)生成的就可以。

4、賦值運(yùn)算符重載

Date& operator=(Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
    //cout << "賦值成功" << endl;
    return *this;
}

也可不寫,使用系統(tǒng)默認(rèn)生成的即可。

拷貝構(gòu)造和賦值運(yùn)算符重載的區(qū)別在于拷貝構(gòu)造用于對(duì)象構(gòu)造時(shí)使用,而賦值運(yùn)算符重載用于已存在對(duì)象賦值時(shí)使用。

四、運(yùn)算符重載

1、+=、+、-=、-

Date& operator+=(int day)
{
    if (day < 0)
        *this -= -day;
    else
    {
        _day += day;
        while (_day > GetMonthDay(_year, _month))
        {
            _day -= GetMonthDay(_year, _month);
            ++_month;
            if (_month > 12)
            {
                _month = 1;
                ++_year;
            }
        }
    }
    return *this;
}
Date operator+(int day)
{
    Date tmp(*this);
    return tmp += day;
}
Date& operator-=(int day)
{
    if (day < 0)
        *this += -day;
    else
    {
        _day -= day;
        while (_day <= 0)
        {
            --_month;
            if (_month <= 0)
            {
                _month = 12;
                --_year;
            }
            _day += GetMonthDay(_year, _month);
        }
    }
    return *this;
}
Date operator-(int day)
{
    Date tmp(*this);
    return tmp -= day;
}
//日期減日期
int operator-(const Date& d)
{
    Date tmpThis = *this, tmpDay = d;
    int count = 0;//用于計(jì)數(shù)
    if (*this >= d)
    {
        while (tmpDay != tmpThis)
        {
            ++tmpDay;
            ++count;
        }
    }
    else
    {
        while (tmpDay != tmpThis)
        {
            ++tmpThis;
            --count;
        }
    }
    return count;
}

1、注意這幾個(gè)運(yùn)算符要防止外部傳入的day是負(fù)數(shù)。例如+=傳入的參數(shù)如果是負(fù)數(shù),則去調(diào)用-=函數(shù)。

2、注意傳值返回和傳引用返回,當(dāng)return對(duì)象出了作用域還存在時(shí),可以用傳引用返回,減少一次拷貝構(gòu)造。

3、實(shí)現(xiàn)完+=、-=后,+、-運(yùn)算符可復(fù)用邏輯。

2、==、!=、>、>=、<、<=

bool operator==(const Date& d)
    {
        if (_year == d._year && _month == d._month && _day == d._day)
        {
            return true;
        }
        return false;
    }
    bool operator>(const Date& d)
    {
        if (_year > d._year)
            return true;
        if (_year == d._year && _month > d._month)
            return true;
        if (_year == d._year && _month == d._month && _day > d._day)
            return true;
        return false;
    }
    bool operator>=(const Date& d)
    {
        return *this > d || *this == d;
    }
    bool operator!=(const Date& d)
    {
        return !(*this == d);
    }
    bool operator<(const Date& d)
    {
        return !(*this >= d);
    }
    bool operator<=(const Date& d)
    {
        return !(*this > d);
    }

1、注意右操組數(shù)一定要加上&,減少一次傳參時(shí)的拷貝構(gòu)造;再加上const,防止被引用的對(duì)象被改變。

2、寫完==和>函數(shù),其他運(yùn)算符都可以復(fù)用邏輯。

3、前置++和--、后置++和--

Date& operator++()
    {
        ++_day;
        if (_day > GetMonthDay(_year, _month))
        {
            _day = 1;
            ++_month;
            if (_month > 12)
            {
                _month = 1;
                ++_year;
            }
        }
        return *this;
    }
    Date operator++(int)
    {
        Date tmp(*this);
        ++* this;
        return tmp;
    }
    Date& operator--()
    {
        --_day;
        if (_day <= 0)
        {
            --_month;
            if (_month == 0)
            {
                _month = 12;
                --_year;
            }
            _day += GetMonthDay(_year, _month);
        }
        return *this;
    }
    Date operator--(int)
    {
        Date tmp(*this);
        --* this;
        return tmp;
    }

1、因?yàn)?+和--是單操作數(shù)的運(yùn)算符,在重載時(shí),無法區(qū)分是前置的重載還是后置的重載,所以C++規(guī)定:前置重載與普通運(yùn)算符重載一致,后置重載需要在參數(shù)列表中加入一個(gè)無用的參數(shù)。這個(gè)參數(shù)必須是int類型(用別的類型編譯器報(bào)錯(cuò))。

2、前置++--可以使用傳引用返回,但后置++--因?yàn)榉祷刂禃簳r(shí)不改變,所以只能傳值返回。這也是使用前置++--性能優(yōu)于后置++--的原因。

五、日期類代碼

class Date
{
public:
    void Print()
    {
        cout << _year << " " << _month << " " << _day << endl;
    }
    int GetMonthDay(int year, int month)
    {
        //靜態(tài)數(shù)組,每次調(diào)用不用頻繁在棧區(qū)創(chuàng)建數(shù)組
        static int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
        //判斷是否閏年
        int day = monthArr[month - 1];
        if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
        {
            day = 29;
        }
        return day;
    }
    //構(gòu)造函數(shù)
    Date(int year = 1, int month = 1, int day = 1)
    {
        if (year >= 1 && month >= 1 && day >= 1 && day <= GetMonthDay(year, month))
        {
            _year = year;
            _month = month;
            _day = day;
            //cout << "構(gòu)造成功" << endl;
        }
        else
        {
            cout << "日期不合法" << endl;
        }
    }
    //析構(gòu)函數(shù)
    ~Date()
    {
        cout << "析構(gòu)成功" << endl;;
    }
    //拷貝構(gòu)造
    Date(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        cout << "拷貝構(gòu)造成功" << endl;
    }
    ////賦值運(yùn)算符重載
    Date& operator=(Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        cout << "賦值成功" << endl;
        return *this;
    }
    //運(yùn)算符重載
    Date& operator+=(int day)
    {
        if (day < 0)
            *this -= -day;
        else
        {
            _day += day;
            while (_day > GetMonthDay(_year, _month))
            {
                _day -= GetMonthDay(_year, _month);
                ++_month;
                if (_month > 12)
                {
                    _month = 1;
                    ++_year;
                }
            }
        }
        return *this;
    }
    Date operator+(int day)
    {
        Date tmp(*this);
        return tmp += day;
    }
    Date& operator-=(int day)
    {
        if (day < 0)
            *this += -day;
        else
        {
            _day -= day;
            while (_day <= 0)
            {
                --_month;
                if (_month <= 0)
                {
                    _month = 12;
                    --_year;
                }
                _day += GetMonthDay(_year, _month);
            }
        }
        return *this;
    }
    Date operator-(int day)
    {
        Date tmp(*this);
        return tmp -= day;
    }
    int operator-(const Date& d)
    {
        Date tmpThis = *this, tmpDay = d;
        int count = 0;//用于計(jì)數(shù)
        if (*this >= d)
        {
            while (tmpDay != tmpThis)
            {
                ++tmpDay;
                ++count;
            }
        }
        else
        {
            while (tmpDay != tmpThis)
            {
                ++tmpThis;
                --count;
            }
        }
        return count;
    }
    bool operator==(const Date& d)
    {
        if (_year == d._year && _month == d._month && _day == d._day)
        {
            return true;
        }
        return false;
    }
    bool operator>(const Date& d)
    {
        if (_year > d._year)
            return true;
        if (_year == d._year && _month > d._month)
            return true;
        if (_year == d._year && _month == d._month && _day > d._day)
            return true;
        return false;
    }
    bool operator>=(const Date& d)
    {
        return *this > d || *this == d;
    }
    bool operator!=(const Date& d)
    {
        return !(*this == d);
    }
    bool operator<(const Date& d)
    {
        return !(*this >= d);
    }
    bool operator<=(const Date& d)
    {
        return !(*this > d);
    }
    Date& operator++()
    {
        ++_day;
        if (_day > GetMonthDay(_year, _month))
        {
            _day = 1;
            ++_month;
            if (_month > 12)
            {
                _month = 1;
                ++_year;
            }
        }
        return *this;
    }
    Date operator++(int)
    {
        Date tmp(*this);
        ++* this;
        return tmp;
    }
    Date& operator--()
    {
        --_day;
        if (_day <= 0)
        {
            --_month;
            if (_month == 0)
            {
                _month = 12;
                --_year;
            }
            _day += GetMonthDay(_year, _month);
        }
        return *this;
    }
    Date operator--(int)
    {
        Date tmp(*this);
        --* this;
        return tmp;
    }
private:
    int _year;
    int _month;
    int _day;
};

因?yàn)楹瘮?shù)的聲明和定義全部放在類中,會(huì)被編譯器當(dāng)成內(nèi)聯(lián)函數(shù)處理。所以可以根據(jù)自身需要,將部分調(diào)用不頻繁、稍長的函數(shù)的聲明寫在類中,而定義寫在類外。

到此這篇關(guān)于基于C++實(shí)現(xiàn)一個(gè)日期計(jì)算器的文章就介紹到這了,更多相關(guān)C++日期計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言實(shí)現(xiàn)動(dòng)態(tài)順序表的實(shí)現(xiàn)代碼

    C語言實(shí)現(xiàn)動(dòng)態(tài)順序表的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C語言實(shí)現(xiàn)動(dòng)態(tài)順序表的實(shí)現(xiàn)代碼的相關(guān)資料,動(dòng)態(tài)順序表在內(nèi)存中開辟一塊空間,可以隨我們數(shù)據(jù)數(shù)量的增多來擴(kuò)容,需要的朋友可以參考下
    2017-08-08
  • C++加密解密php代碼的方法

    C++加密解密php代碼的方法

    這篇文章主要介紹了C++加密解密php代碼的方法,實(shí)例分析了基于C++實(shí)現(xiàn)加密解密的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C++ 結(jié)構(gòu)體初始化與賦值詳解

    C++ 結(jié)構(gòu)體初始化與賦值詳解

    本文主要介紹了C++ 結(jié)構(gòu)體初始化與賦值詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • vs2019創(chuàng)建WebService服務(wù)的實(shí)現(xiàn)

    vs2019創(chuàng)建WebService服務(wù)的實(shí)現(xiàn)

    這篇文章主要介紹了vs2019創(chuàng)建WebService服務(wù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • VisualStudio?禁用移動(dòng)文件到文件夾自動(dòng)修改命名空間功能

    VisualStudio?禁用移動(dòng)文件到文件夾自動(dòng)修改命名空間功能

    這篇文章主要介紹了VisualStudio?禁用移動(dòng)文件到文件夾自動(dòng)修改命名空間功能,文章底部給大家介紹了解決安裝VS2022時(shí),出現(xiàn)未能安裝包“Microsoft.VisualCpp.Redist.14,version=14.32.31332,chip”=x86,的問題及解決方法,需要的朋友可以參考下
    2022-09-09
  • C語言實(shí)現(xiàn)英文文本詞頻統(tǒng)計(jì)

    C語言實(shí)現(xiàn)英文文本詞頻統(tǒng)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)英文文本詞頻統(tǒng)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 關(guān)于單片機(jī)按鍵問題性能提升總結(jié)

    關(guān)于單片機(jī)按鍵問題性能提升總結(jié)

    今天小編就為大家分享一篇關(guān)于關(guān)于單片機(jī)按鍵問題性能提升總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • C++ float、double判斷是否等于0問題

    C++ float、double判斷是否等于0問題

    這篇文章主要介紹了C++ float、double判斷是否等于0問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • C++使用ffmpeg實(shí)現(xiàn)rtsp取流的代碼

    C++使用ffmpeg實(shí)現(xiàn)rtsp取流的代碼

    這篇文章主要介紹了C++使用ffmpeg實(shí)現(xiàn)rtsp取流,文章介紹了ffmepg采用rtsp取流流程圖,CMakeLists.txt編寫方法,通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • C++ CopyFile,MoveFile用法案例詳解

    C++ CopyFile,MoveFile用法案例詳解

    這篇文章主要介紹了C++ CopyFile,MoveFile用法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評(píng)論

延长县| 香格里拉县| 托克逊县| 曲靖市| 舟山市| 石狮市| 新津县| 宁乡县| 名山县| 神木县| 延川县| 望奎县| 常山县| 察隅县| 武汉市| 时尚| 象州县| 修武县| 奉化市| 洛隆县| 南丹县| 昌黎县| 五河县| 古田县| 惠来县| 阳高县| 长丰县| 平南县| 泸定县| 南部县| 永平县| 普定县| 长寿区| 安仁县| 济阳县| 安阳县| 乐安县| 东乡县| 东方市| 蒲江县| 临安市|