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

C++日期類計(jì)算器的模擬實(shí)現(xiàn)舉例詳解

 更新時(shí)間:2023年04月26日 14:38:36   作者:EnticE152  
兩個(gè)日期之間相隔天數(shù)的計(jì)算網(wǎng)上有許多的軟件,這里主要介紹如何使用C/C++語言來完成這樣的功能,下面這篇文章主要給大家介紹了關(guān)于C++日期類計(jì)算器的模擬實(shí)現(xiàn),需要的朋友可以參考下

日期類計(jì)算器的模擬實(shí)現(xiàn)::

1.獲取某年某月的天數(shù)

int GetMonthDay(int year, int month)
{
	static int monthDayArray[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;
	}
	else
	{
		return monthDayArray[month];
	}
}

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

Date(int year = 1, int month = 1, int day = 1)
{
		_year = year;
		_month = month;
		_day = day;
		//檢查日期是否合法
		if (!((year >= 1)
			&& (month >= 1 && month <= 12)
			&& (day >= 1 && day <= GetMonthDay(year, month))))
		{
			cout << "非法日期" << endl;
		}
}

3.拷貝構(gòu)造函數(shù)

// 拷貝構(gòu)造函數(shù) 形參加const 防止寫反了 問題就可以檢查出來了
Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

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

//d1 = d2 
//注:1.要注意兩個(gè)參數(shù)的順序 2.這里面參數(shù)不加引用不會(huì)導(dǎo)致無窮遞歸 但為了避免拷貝構(gòu)造最好加引用
Date& operator=(const Date& d)
{
	//為了支持鏈?zhǔn)劫x值 if是為了避免自己給自己賦值 d1 = d1
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

5.析構(gòu)函數(shù)

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

日期類因?yàn)闆]有申請(qǐng)資源,所以無需寫析構(gòu)函數(shù),編譯器默認(rèn)生成的析構(gòu)函數(shù)就可以。

6.日期+=天數(shù)

//d1 += 100
//天滿了進(jìn)月 月滿了進(jìn)年 
Date& operator+=(int day)
{
	//避免 d1 += -1000的情形
	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;
}

7.日期+天數(shù)

//d1 + 100
Date operator+(int day) const
{
	Date ret(*this);
	ret += day;//ret.operator+=(day)
	return ret;
}

8.日期-天數(shù)

//d1 - 100
Date operator-(int day) const
{
	Date ret(*this);
	ret -= day;
	return ret;
}

9.日期-=天數(shù)

//d1 -= 100
Date& operator-=(int day)
{
	//避免 d1 -= -1000
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

10.前置++的運(yùn)算符重載

//前置++
Date& operator++()
{
	//會(huì)調(diào)用 operator+=(int day)
	*this += 1;
	return *this;
}

11.后置++的運(yùn)算符重載

//后置++ —多一個(gè)int參數(shù)主要是為了和前置++進(jìn)行區(qū)分 構(gòu)成函數(shù)重載
Date operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

12.前置--的運(yùn)算符重載

//前置--
Date& operator--()
{
	//復(fù)用運(yùn)算符重載-=
	*this -= 1;
	return *this;
}

13.后置--的運(yùn)算符重載

//后置--
Date operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

14.>的運(yùn)算符重載

//d1 > d2
bool operator>(const Date& d) const
{
	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;
	}
	return false;
}

15.<的運(yùn)算符重載

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

16.==的運(yùn)算符重載

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

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

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

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

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

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

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

20.<<的運(yùn)算符重載

//內(nèi)聯(lián)函數(shù)和靜態(tài)成員一樣 調(diào)用處展開 不進(jìn)符號(hào)表
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

21.>>的運(yùn)算符重載

//cin >> d1 編譯器轉(zhuǎn)化成operator(cin,d1) 形參中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

22.日期-日期

//日期-日期
int operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
  //總結(jié):凡是內(nèi)部不改變成員變量 也就是不改變*this數(shù)據(jù)的 這些成員函數(shù)都應(yīng)該加const
  //if (d > *this)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++n;
		//復(fù)用++ ++到和d1日期相等 就是相差多少天
		++min;
	}
	return n * flag;
}

Date.h

#pragma once
#include <iostream>
using namespace std;
class Date
{
	//友元聲明(類的任意位置)聲明友元時(shí)可以不用加inline
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	int GetMonthDay(int year, int month)
	{
		static int monthDayArray[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;
		}
		else
		{
			return monthDayArray[month];
		}
	}
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		//檢查日期是否合法
		if (!((year >= 1)
			&& (month >= 1 && month <= 12)
			&& (day >= 1 && day <= GetMonthDay(year, month))))
		{
			cout << "非法日期" << endl;
		}
	}
	// 拷貝構(gòu)造函數(shù) 形參加const 防止寫反了 問題就可以檢查出來了
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	//d1 == d2
	bool operator==(const Date& d) const;
	//d1 > d2
	bool operator>(const Date& d) const;
	//d1 >= d2
	bool operator>=(const Date& d) const;
	//d1 <= d2
	bool operator<=(const Date& d) const;
	//d1 < d2
	bool operator<(const Date& d) const;
	//d1 != d2
	bool operator!=(const Date& d) const;
	//d1 += 100
	Date& operator+=(int day);
	//d1 + 100
	Date operator+(int day) const;
	//d1 = d2 注:1.要注意兩個(gè)參數(shù)的順序 2.這里面參數(shù)不加引用不會(huì)導(dǎo)致無窮遞歸 但為了避免拷貝構(gòu)造最好加引用
	Date& operator=(const Date& d)
	{
		//為了支持鏈?zhǔn)劫x值 if是為了避免自己給自己賦值 d1 = d1
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	//d1 -= 100
	Date& operator-=(int day);
	//d1 - 100
	Date operator-(int day) const;
	//++的操作數(shù)只有一個(gè) 不傳參
	//前置++
	Date& operator++();
	//編譯器為了區(qū)分前置++和后置++ 規(guī)定在后置的函數(shù)上加了一個(gè)參數(shù)
	//后置++
	Date operator++(int);
	//允許成員函數(shù)加const 此時(shí)this指針的類型為:const Date* const this
	void Print() const
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);
	//日期-日期
	int operator-(const Date& d) const;
	//流插入
	//d1 << cout編譯器會(huì)轉(zhuǎn)化成d1.operator<<(cout) this指針搶了左操作數(shù)d1的位置
	//<<和>>的重載一般不寫成成員函數(shù) 因?yàn)閠his默認(rèn)搶了第一個(gè)參數(shù)的位置 Date類對(duì)象就是左操作數(shù) 不符合使用習(xí)慣和可讀性
	/*void operator<<(ostream& out)
	{
		out << _year << "年" << _month << "月" << _day << "日" << endl;
	}*/
	//取地址重載
	Date* operator&()
	{
		return this;
	}
	//const成員取地址重載
	const Date* operator&() const
	{
		return this;
	}
	//取地址重載和const成員取地址重載不實(shí)現(xiàn) 編譯器會(huì)默認(rèn)生成
private:
	int _year;
	int _month;
	int _day;
};
//結(jié)論:對(duì)于自定義類型,盡量用前置,減少拷貝,提高效率
//全局函數(shù)調(diào)用:cout << d1轉(zhuǎn)化成operator<<(cout,d1)
//全局函數(shù)的定義和全局變量不能放在.h文件中 因?yàn)楹瘮?shù)的定義在Date.cpp和test.cpp都會(huì)展開 函數(shù)地址進(jìn)入符號(hào)表 鏈接器鏈接兩個(gè).cpp文件時(shí)相同的函數(shù)地址會(huì)報(bào)錯(cuò)
//解決方法:1.改成靜態(tài) 2.聲明和定義分離
//static修飾函數(shù)只在當(dāng)前文件可見 不會(huì)進(jìn)入符號(hào)表
//static void operator<<(ostream& out,const Date& d)
//{
//	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//}
//ostream& operator<<(ostream& out, const Date& d);
//內(nèi)聯(lián)函數(shù)和靜態(tài)成員一樣 調(diào)用處展開 不進(jìn)符號(hào)表
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
//cin >> d1 編譯器轉(zhuǎn)化成operator(cin,d1) 形參中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

Date.cpp

#include"Date.h"
//d1 == d2
bool Date::operator==(const Date& d) const
{ 	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
//d1 > d2
bool Date::operator>(const Date& d) const
{
	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;
	}
	return false;
}
//d1 >= d2
bool Date::operator>=(const Date& d) const
{
	return *this > d || *this == d;
}
//d1 <= d2
bool Date::operator<=(const Date& d) const
{
	return !(*this > d);
}
//d1 < d2
bool Date::operator<(const Date& d) const
{
	return !(*this >= d);
}
//d1 != d2
bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}
//d1 += 100
//天滿了進(jìn)月 月滿了進(jìn)年 
Date& Date::operator+=(int day)
{
	//避免 d1 += -1000的情形
	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;
}
//d1 + 100
Date Date::operator+(int day) const
{
	Date ret(*this);
	ret += day;//ret.operator+=(day)
	return ret;
}
//d1 -= 100
Date& Date::operator-=(int day)
{
	//避免 d1 -= -1000
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
//d1 - 100
Date Date::operator-(int day) const
{
	Date ret(*this);
	ret -= day;
	return ret;
}
//前置++
Date& Date::operator++()
{
	//會(huì)調(diào)用 operator+=(int day)
	*this += 1;
	return *this;
}
//后置++ —多一個(gè)int參數(shù)主要是為了和前置++進(jìn)行區(qū)分 構(gòu)成函數(shù)重載
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
//前置--
Date& Date::operator--()
{
	//復(fù)用運(yùn)算符重載-=
	*this -= 1;
	return *this;
}
//后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
//日期-日期
int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
  //總結(jié):凡是內(nèi)部不改變成員變量 也就是不改變*this數(shù)據(jù)的 這些成員函數(shù)都應(yīng)該加const
  //if (d > *this)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++n;
		//復(fù)用++ ++到和d1日期相等 就是相差多少天
		++min;
	}
	return n * flag;
}
//為了支持鏈?zhǔn)搅鞑迦?cout<< d1 <<d2 返回cout類對(duì)象
//ostream& operator<<(ostream& out,const Date& d)
//{
//	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//	return out;
//}

總結(jié)

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

相關(guān)文章

  • C++11?std::transform函數(shù)使用小結(jié)

    C++11?std::transform函數(shù)使用小結(jié)

    std::transform是C++標(biāo)準(zhǔn)庫中的一個(gè)算法,它用于對(duì)輸入范圍內(nèi)的元素進(jìn)行操作,并將結(jié)果存儲(chǔ)在輸出范圍內(nèi),本文就介紹了std::transform函數(shù)的具體使用,感興趣的可以了解一下
    2023-09-09
  • C語言實(shí)現(xiàn)飛機(jī)游戲(進(jìn)階版)的示例代碼

    C語言實(shí)現(xiàn)飛機(jī)游戲(進(jìn)階版)的示例代碼

    在前文中,已經(jīng)帶大家利用C語言實(shí)現(xiàn)了簡(jiǎn)單的飛機(jī)游戲,但它還存在一些缺陷。因此,本文將給大家?guī)磉M(jìn)階版的飛機(jī)游戲,需要的可以參考一下
    2022-10-10
  • C語言實(shí)現(xiàn)BMP格式圖片轉(zhuǎn)化為灰度

    C語言實(shí)現(xiàn)BMP格式圖片轉(zhuǎn)化為灰度

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)BMP格式圖片轉(zhuǎn)化為灰度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • C++的拷貝構(gòu)造函數(shù)你了解嗎

    C++的拷貝構(gòu)造函數(shù)你了解嗎

    這篇文章主要為大家詳細(xì)介紹了C++的拷貝構(gòu)造函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C++實(shí)現(xiàn)LeetCode(208.實(shí)現(xiàn)字典樹(前綴樹))

    C++實(shí)現(xiàn)LeetCode(208.實(shí)現(xiàn)字典樹(前綴樹))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(208.實(shí)現(xiàn)字典樹(前綴樹)),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C++中的異常實(shí)例詳解

    C++中的異常實(shí)例詳解

    異常處理是C++的一項(xiàng)語言機(jī)制,用于在程序中處理異常事件,下面這篇文章主要給大家介紹了關(guān)于C++中異常的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • 利用Qt實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)報(bào)文大小端數(shù)據(jù)的收發(fā)

    利用Qt實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)報(bào)文大小端數(shù)據(jù)的收發(fā)

    大小端(Endianness)是計(jì)算機(jī)體系結(jié)構(gòu)的一個(gè)術(shù)語,它描述了多字節(jié)數(shù)據(jù)在內(nèi)存中的存儲(chǔ)順序,下面我們來看看如何利用Qt實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)報(bào)文大小端數(shù)據(jù)的收發(fā)吧
    2024-11-11
  • C語言基礎(chǔ)使用IDE快速開發(fā)的方法

    C語言基礎(chǔ)使用IDE快速開發(fā)的方法

    這篇文章主要介紹了C語言基礎(chǔ)使用IDE快速開發(fā)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • C語言中的運(yùn)算符和結(jié)合性問題

    C語言中的運(yùn)算符和結(jié)合性問題

    這篇文章主要介紹了C語言中的運(yùn)算符和結(jié)合性問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • C語言利用EasyX繪制小企鵝表情包

    C語言利用EasyX繪制小企鵝表情包

    這篇文章主要為大家詳細(xì)介紹了C語言如何利用EasyX繪圖庫實(shí)現(xiàn)繪制可愛的小企鵝表情包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-12-12

最新評(píng)論

黄山市| 侯马市| 平远县| 常宁市| 新平| 元谋县| 原平市| 茶陵县| 渝北区| 遂平县| 上栗县| 克山县| 东方市| 高碑店市| 禹州市| 延吉市| 泸西县| 广水市| 汪清县| 巴青县| 柳州市| 伊通| 贵州省| 孟州市| 九龙县| 彝良县| 漯河市| 怀仁县| 县级市| 永吉县| 武义县| 京山县| 灵台县| 容城县| 永泰县| 宣汉县| 嘉义市| 荃湾区| 沁水县| 军事| 开鲁县|