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

C++日期類(Date)實(shí)現(xiàn)的示例代碼

 更新時(shí)間:2022年07月11日 15:22:55   作者:繁華的夢(mèng)境  
這篇文章主要為大家詳細(xì)介紹了如何利用C++語(yǔ)言實(shí)現(xiàn)日期類(Date),可以實(shí)現(xiàn)確定某年某月有多少天、打印日期等功能,感興趣的可以了解一下

類的定義

#pragma once
#include < iostream>
using std::cout;
using std::cin;
using std::endl;
class Date
{
public:
	// 獲取某年某月的天數(shù)
	int GetMonthDay(int year, int month);
	// 全缺省的構(gòu)造函數(shù)
	Date(int year = 0, int month = 1, int day = 1);
	void Print();
    //析構(gòu),拷貝構(gòu)造,賦值重載函數(shù)可以不用寫,默認(rèn)生成的就夠用
	//日期+=天數(shù)
	Date& operator+=(int day);

	// 日+天數(shù)
	Date operator+(int day);

	// 日期-天數(shù)
	Date operator-(int day);

	// 日期-=天數(shù)
	Date& operator-=(int day);

	// 前置++
	Date& operator++();

	// 后置++   
	Date operator++(int);

	//后置--   
	Date operator--(int);
	
	// 前置--
	Date& operator--();
	
	// >運(yùn)算符重載
	bool operator>(const Date& d);

	// ==運(yùn)算符重載
	bool operator==(const Date& d);

	// >=運(yùn)算符重載
	bool operator >= (const Date& d);

	// <運(yùn)算符重載
	bool operator < (const Date& d);

	// <=運(yùn)算符重載
	bool operator <= (const Date& d);

	// !=運(yùn)算符重載
	bool operator != (const Date& d);

	// 日期-日期 返回天數(shù)
	int operator-(const Date& d);

private:
	int _year;
	int _month;
	int _day;
};

確定某年某月有多少天

//因?yàn)殚c年和平年二月份的天數(shù)不一樣,隨便加加減減會(huì)有問(wèn)題,這個(gè)函數(shù)可以根據(jù)閏年和平年給出對(duì)應(yīng)的天數(shù)
//因?yàn)楹竺嫘枰l繁調(diào)用它,設(shè)置成內(nèi)聯(lián)函數(shù)
inline int Date::GetMonthDay(int year, int month) {
    //設(shè)置成靜態(tài)變量,不用每次調(diào)用這個(gè)函數(shù)都開(kāi)辟一個(gè)數(shù)組
    static int arr[13] = {0,31,28,31,30,31,30,31,31,30,31, 30, 31};
    int day = arr[month];
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
        day = 29;
    return day;
}

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

Date::Date(int year, int month, int day) {
    //防止給的日期有錯(cuò)誤
    if (year >= 0 && month > 0 && month < 13 
        && day >0 && day <= GetMonthDay(year, month)) {
        _year = year;
        _month = month;
        _day = day;
    }
    else {
        cout << "非法日期" << endl;
        cout << year << "年" << month << "月" << day << "日" << endl;
    }
}

打印日期

void Date::Print() {
    cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

日期+=天數(shù)

Date& Date::operator+=(int day) {
    if (day < 0){
        *this -= (-day);
    }
    else {
        _day += day;
        //如果天數(shù)大于月份對(duì)應(yīng)的最大天數(shù),就先減去這個(gè)月天數(shù),然后月份++,然后再判斷月份對(duì)應(yīng)的天數(shù)對(duì)不對(duì),往復(fù)循環(huán)
        while (_day > GetMonthDay(_year, _month)) {
            _day -= GetMonthDay(_year, _month);
            ++_month;
            if (_month > 12) {
                ++_year;
                _month = 1;
            }
        }
        //如果是傳值返回的話傳的是他的一個(gè)拷貝臨時(shí)變量,又需要調(diào)用拷貝構(gòu)造函數(shù),因?yàn)槌隽撕瘮?shù)作用域?qū)ο筮€在,所以傳引用更好。
    }
    return *this;
}

日期+天數(shù)

Date Date::operator+(int day) {
    Date ret(*this);
    //復(fù)用operator+=函數(shù)
    ret += day;
    //ret是一個(gè)局部變量,出了作用域就銷毀了,傳不了引用
    return ret;
}

日期-=天數(shù)

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

日期-天數(shù)

Date Date::operator-(int day) {
    Date ret = *this;
    ret -= day;
    return ret;
}

前置++

//返回的是++后的值,可以復(fù)用+=函數(shù),邏輯是一樣的,把day換成1即可
Date& Date::operator++() {
    *this += 1;
    return *this;
}

后置++

//返回的是++前的值,不能傳引用返回
//為了和前置++賦值重載函數(shù)構(gòu)成函數(shù)重載,需要加個(gè)int ,不需要傳實(shí)參,因?yàn)闆](méi)用,如果不加參數(shù)那么前置++和后置++函數(shù)就一樣了,們?cè)谶M(jìn)行前置++或者后置++操作時(shí),編譯器就不知道調(diào)用哪一個(gè)了
Date Date::operator++(int) {
    Date ret(*this);
    *this += 1;
    return ret;
}

后置–

//返回原對(duì)象,然后對(duì)象再--
Date Date::operator--(int) {
    Date ret = *this;
    *this -= 1;
    return ret;
}

前置–

返回--后的對(duì)象

Date& Date::operator--() {
    *this -= 1;
    return *this;
}

>運(yùn)算符重載

d1 > d2  -> d1.operator>(&d1, d2)
bool Date::operator>(const Date& d) {
    if (_year >= d._year)
    {
        if (_year > d._year)
            return true;
        else {
            if (_month >= d._month) {
                if (_month > d._month)
                    return true;
                else {
                    if (_day > d._day)
                        return true;
                }
            }
        }
    }
    return false;
}

==運(yùn)算符重載

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

>=運(yùn)算符重載

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

<運(yùn)算符重載

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

<=運(yùn)算符重載

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

!=運(yùn)算符重載

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

計(jì)算兩個(gè)日期之間的間隔天數(shù),日期減去日期

int Date::operator-(const Date& d) {
    Date min = *this;
    Date max = d;
    int flag = -1;
    int daycount = 0;
    if (*this > d) {
        min = d;
        max = *this;
        flag = 1;
    }
    while (min != max) {
        ++min;
        daycount++;
    }
    return flag * daycount;
}

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

相關(guān)文章

  • 深入C++浮點(diǎn)數(shù)無(wú)效值定義與判定的解決辦法

    深入C++浮點(diǎn)數(shù)無(wú)效值定義與判定的解決辦法

    本篇文章是對(duì)C++中浮點(diǎn)數(shù)無(wú)效值定義與判定進(jìn)行了介紹,需要的朋友參考下
    2013-05-05
  • c++迭代器失效的情況匯總

    c++迭代器失效的情況匯總

    這篇文章主要介紹了C++迭代器失效的幾種情況總結(jié),文中代碼非常詳細(xì),幫助大家更好的了解學(xué)習(xí),感興趣的朋友可以參考下
    2020-06-06
  • C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之堆排序詳解

    C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之堆排序詳解

    堆是計(jì)算機(jī)科學(xué)中一類特殊的數(shù)據(jù)結(jié)構(gòu)的統(tǒng)稱,通常是一個(gè)可以被看做一棵完全二叉樹(shù)的數(shù)組對(duì)象。而堆排序是利用堆這種數(shù)據(jù)結(jié)構(gòu)所設(shè)計(jì)的一種排序算法。本文將通過(guò)圖片詳細(xì)介紹堆排序,需要的可以參考一下
    2022-03-03
  • C++小知識(shí):用++i替代i++

    C++小知識(shí):用++i替代i++

    今天小編就為大家分享一篇關(guān)于C++小知識(shí):用++i替代i++,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 如何用c++表驅(qū)動(dòng)替換if/else和switch/case語(yǔ)句

    如何用c++表驅(qū)動(dòng)替換if/else和switch/case語(yǔ)句

    本文將介紹使用表驅(qū)動(dòng)法,替換復(fù)雜的if/else和switch/case語(yǔ)句,想了解詳細(xì)內(nèi)容,請(qǐng)看下文
    2021-08-08
  • C語(yǔ)言實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)程序設(shè)計(jì)

    C語(yǔ)言實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)程序設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)程序設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 如何通過(guò)UltraEdit解析BMP文件內(nèi)部結(jié)構(gòu)(BMP位圖基礎(chǔ))

    如何通過(guò)UltraEdit解析BMP文件內(nèi)部結(jié)構(gòu)(BMP位圖基礎(chǔ))

    我們先打開(kāi)畫圖隨便畫一幅圖并采用24位bmp圖像格式保存,就得到了一張24位真彩色的位圖,下面我們來(lái)詳細(xì)分析bmp位圖的各個(gè)組成部分,感興趣的朋友跟隨小編一起看看吧
    2021-08-08
  • C語(yǔ)言繪制簡(jiǎn)單時(shí)鐘小程序

    C語(yǔ)言繪制簡(jiǎn)單時(shí)鐘小程序

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言繪制簡(jiǎn)單時(shí)鐘小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++如何用數(shù)組模擬鏈表

    C++如何用數(shù)組模擬鏈表

    大家好,本篇文章主要講的是C++如何用數(shù)組模擬鏈表,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • C++ 花括號(hào){}初始化小結(jié)

    C++ 花括號(hào){}初始化小結(jié)

    在C++11及以后的版本中,花括號(hào){}語(yǔ)法在不同語(yǔ)境下有不同的用法,本文就詳細(xì)的介紹C++ 花括號(hào){}初始化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06

最新評(píng)論

同仁县| 甘洛县| 延川县| 巧家县| 阿克陶县| 当雄县| 阜康市| 沛县| 新昌县| 鄱阳县| 阳高县| 阿克陶县| 塔城市| 盐边县| 施秉县| 鄄城县| 怀仁县| 嘉黎县| 罗源县| 广河县| 普定县| 海盐县| 皋兰县| 榕江县| 山阴县| 民县| 大丰市| 普定县| 灵石县| 黄冈市| 南通市| 汝南县| 洛扎县| 敖汉旗| 大田县| 阳春市| 镇安县| 吉林市| 内乡县| 涞源县| 河曲县|