欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2023年04月26日 14:38:36   作者:EnticE152  
兩個(gè)日期之間相隔天數(shù)的計(jì)算網(wǎng)上有許多的軟件,這里主要介紹如何使用C/C++語(yǔ)言來完成這樣的功能,下面這篇文章主要給大家介紹了關(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)致無(wú)窮遞歸 但為了避免拷貝構(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)資源,所以無(wú)需寫析構(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)致無(wú)窮遞歸 但為了避免拷貝構(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語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng)項(xiàng)目

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng)項(xiàng)目

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng)項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Opencv二幀差法檢測(cè)運(yùn)動(dòng)目標(biāo)與提取輪廓

    Opencv二幀差法檢測(cè)運(yùn)動(dòng)目標(biāo)與提取輪廓

    這篇文章主要為大家詳細(xì)介紹了Opencv使用二幀差法檢測(cè)運(yùn)動(dòng)目標(biāo)與提取輪廓,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)順序表中的增刪改(頭插頭刪)教程示例詳解

    C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)順序表中的增刪改(頭插頭刪)教程示例詳解

    這篇文章主要為大家介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)順序表中增刪改關(guān)于頭插頭刪的教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • c語(yǔ)言重要的字符串與內(nèi)存函數(shù)

    c語(yǔ)言重要的字符串與內(nèi)存函數(shù)

    這篇文章主要介紹一些c語(yǔ)言中常用字符串函數(shù)和內(nèi)存函數(shù)的使用和注意事項(xiàng),并且為了幫助讀者理解和使用,也都模擬實(shí)現(xiàn)了他們的代碼,需要的朋友可以參考一下
    2021-09-09
  • c語(yǔ)言與c++基礎(chǔ)知識(shí)點(diǎn)(必看)

    c語(yǔ)言與c++基礎(chǔ)知識(shí)點(diǎn)(必看)

    下面小編就為大家?guī)硪黄猚語(yǔ)言與c++基礎(chǔ)知識(shí)點(diǎn)(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07
  • C++實(shí)現(xiàn)簡(jiǎn)單職工信息管理系統(tǒng)

    C++實(shí)現(xiàn)簡(jiǎn)單職工信息管理系統(tǒng)

    本文給大家分享的是使用C++實(shí)現(xiàn)簡(jiǎn)單的職工信息管理系統(tǒng)的代碼,本系統(tǒng)采用了面向?qū)ο蟮某绦蛟O(shè)計(jì)方法,所有的方法均以類為基礎(chǔ),感興趣的小伙伴們可以參考一下
    2015-08-08
  • 基于matlab實(shí)現(xiàn)DCT數(shù)字水印嵌入與提取

    基于matlab實(shí)現(xiàn)DCT數(shù)字水印嵌入與提取

    數(shù)字水印技術(shù)是將一些標(biāo)識(shí)信息直接嵌入數(shù)字載體當(dāng)中,?或間接表示在信號(hào)載體中,?且不影響原載體的使用價(jià)值。本文主要為大家介紹了基于matlab如何實(shí)現(xiàn)數(shù)字水印的嵌入與提取,感興趣的可以學(xué)習(xí)一下
    2022-01-01
  • C++基于reactor的服務(wù)器百萬(wàn)并發(fā)實(shí)現(xiàn)與講解

    C++基于reactor的服務(wù)器百萬(wàn)并發(fā)實(shí)現(xiàn)與講解

    這篇文章主要介紹了C++基于reactor的服務(wù)器百萬(wàn)并發(fā)實(shí)現(xiàn)與講解,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • OpenCV實(shí)現(xiàn)幀間差分法詳解

    OpenCV實(shí)現(xiàn)幀間差分法詳解

    這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)幀間差分法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • c++中堆棧及創(chuàng)建對(duì)象示例代碼

    c++中堆棧及創(chuàng)建對(duì)象示例代碼

    這篇文章主要給大家詳細(xì)介紹了c++如何實(shí)現(xiàn)堆棧及創(chuàng)建對(duì)象,文中先進(jìn)行了簡(jiǎn)單的介紹,而后給出了詳細(xì)的示例代碼及注釋,相信對(duì)大家的理解和學(xué)習(xí)很有幫助,有需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2016-12-12

最新評(píng)論