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

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

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

類的定義

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

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

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

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

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

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

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

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

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

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

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

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

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

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

確定某年某月有多少天

//因為閏年和平年二月份的天數不一樣,隨便加加減減會有問題,這個函數可以根據閏年和平年給出對應的天數
//因為后面需要頻繁調用它,設置成內聯(lián)函數
inline int Date::GetMonthDay(int year, int month) {
    //設置成靜態(tài)變量,不用每次調用這個函數都開辟一個數組
    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;
}

構造函數

Date::Date(int year, int month, int day) {
    //防止給的日期有錯誤
    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;
}

日期+=天數

Date& Date::operator+=(int day) {
    if (day < 0){
        *this -= (-day);
    }
    else {
        _day += day;
        //如果天數大于月份對應的最大天數,就先減去這個月天數,然后月份++,然后再判斷月份對應的天數對不對,往復循環(huán)
        while (_day > GetMonthDay(_year, _month)) {
            _day -= GetMonthDay(_year, _month);
            ++_month;
            if (_month > 12) {
                ++_year;
                _month = 1;
            }
        }
        //如果是傳值返回的話傳的是他的一個拷貝臨時變量,又需要調用拷貝構造函數,因為出了函數作用域對象還在,所以傳引用更好。
    }
    return *this;
}

日期+天數

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

日期-=天數

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;
}

日期-天數

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

前置++

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

后置++

//返回的是++前的值,不能傳引用返回
//為了和前置++賦值重載函數構成函數重載,需要加個int ,不需要傳實參,因為沒用,如果不加參數那么前置++和后置++函數就一樣了,們在進行前置++或者后置++操作時,編譯器就不知道調用哪一個了
Date Date::operator++(int) {
    Date ret(*this);
    *this += 1;
    return ret;
}

后置–

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

前置–

返回--后的對象

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

>運算符重載

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;
}

==運算符重載

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

>=運算符重載

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

<運算符重載

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

<=運算符重載

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

!=運算符重載

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

計算兩個日期之間的間隔天數,日期減去日期

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;
}

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

相關文章

  • 深入C++浮點數無效值定義與判定的解決辦法

    深入C++浮點數無效值定義與判定的解決辦法

    本篇文章是對C++中浮點數無效值定義與判定進行了介紹,需要的朋友參考下
    2013-05-05
  • c++迭代器失效的情況匯總

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

    這篇文章主要介紹了C++迭代器失效的幾種情況總結,文中代碼非常詳細,幫助大家更好的了解學習,感興趣的朋友可以參考下
    2020-06-06
  • C語言數據結構之堆排序詳解

    C語言數據結構之堆排序詳解

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

    C++小知識:用++i替代i++

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

    如何用c++表驅動替換if/else和switch/case語句

    本文將介紹使用表驅動法,替換復雜的if/else和switch/case語句,想了解詳細內容,請看下文
    2021-08-08
  • C語言實現(xiàn)學生學籍管理系統(tǒng)程序設計

    C語言實現(xiàn)學生學籍管理系統(tǒng)程序設計

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

    如何通過UltraEdit解析BMP文件內部結構(BMP位圖基礎)

    我們先打開畫圖隨便畫一幅圖并采用24位bmp圖像格式保存,就得到了一張24位真彩色的位圖,下面我們來詳細分析bmp位圖的各個組成部分,感興趣的朋友跟隨小編一起看看吧
    2021-08-08
  • C語言繪制簡單時鐘小程序

    C語言繪制簡單時鐘小程序

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

    C++如何用數組模擬鏈表

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

    C++ 花括號{}初始化小結

    在C++11及以后的版本中,花括號{}語法在不同語境下有不同的用法,本文就詳細的介紹C++ 花括號{}初始化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06

最新評論

芜湖市| 泊头市| 连城县| 永城市| 惠来县| 普兰县| 亳州市| 定日县| 金昌市| 阿瓦提县| 友谊县| 延边| 吉安县| 津南区| 苏尼特左旗| 太保市| 原平市| 天等县| 元阳县| 札达县| 绿春县| 高州市| 恩施市| 永康市| 保德县| 同德县| 剑阁县| 公安县| 和林格尔县| 上饶市| 长葛市| 竹北市| 西充县| 平度市| 民勤县| 鹿邑县| 哈密市| 滁州市| 威宁| 高安市| 灵璧县|