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

C++逐步介紹日期類的使用

 更新時間:2022年07月07日 11:01:06   作者:幻荼  
下面小編就為大家?guī)硪黄狢++實現(xiàn)日期類(Date類)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

我們今天實現(xiàn)一個簡單的計算日期

我們這里先給出頭文件,后面一個一個解釋

頭文件

#pragma once
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
	//定義構(gòu)造函數(shù)
	Date(int year = 1900, int month = 1, int day = 1);
	//打印日期
	void Print()
	{
		cout << _year <<" " <<_month<<" " << _day << endl;
	}
	//運算符重載
	Date operator+(int day);
	Date operator-(int day);
	Date& operator+=(int day);
	Date& operator-=(int day);
	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);
	Date& operator++();//前置
	Date operator++(int);//后置
	Date& operator--();//前置
	Date operator--(int);//后置
	int operator-(Date d);//計算兩個日期的差值
private:
	int _year;
	int _month;
	int _day;
};

詳細(xì)步驟

第一步

計算閏年和判斷日期是否合法

inline int GetMonthDay(int year, int month)
{
	//多次調(diào)用的時候static能極大減小內(nèi)存消耗,但是也可以刪除,并不影響程序運行
	static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = _monthArray[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)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))//看日期是否合法
	{
		cout << "fail" << endl;
        exit(-1);
	}
}

我們將year,month,day賦值之后,首先進(jìn)行判斷,如果出現(xiàn)異常天數(shù)直接終結(jié)程序(如2022,1,32,這種不存在的日期)

隨后一個問題就出來了,因為閏年和非閏年的2月不同,所以我們又使用一個函數(shù)getmonthday,來判斷閏年的同時獲取當(dāng)月天數(shù);

有些同學(xué)可能直接是【0,31,59(31+28 1月+2月),89(1月+2月+3月)......+.....365】

我們稍后再解釋為什么要用我這種定義方式。

第二步

各類運算符重載

我們首先來看運算符”+”和”+=”

這是一般情況下的+

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp._day += day;
	//判斷天數(shù)是否大于當(dāng)月的天數(shù)
		while (tmp._day > GetMonthDay(tmp._year, tmp._month))
		{
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			tmp._month++;
			//判斷月數(shù)是否大于12
			if (tmp._month > 12)
			{
		        tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;
}

這是一般情況下的+=

Date& Date::operator+=(int day)
{
	Date ret(*this);
		ret._day += day;
		//判斷天數(shù)是否超越了當(dāng)月的天數(shù)
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			//判斷月數(shù)是否超過了12
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
	}
	return *this;
}

我們可以發(fā)現(xiàn)兩者其實都用了同一個while循環(huán)來判斷日期是否合法。

我們可以選擇將while循環(huán)打包成一個新的函數(shù),也可以選擇直接復(fù)用

+復(fù)用+=

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;//直接調(diào)用operator的+=
		return tmp;
}

總代碼

#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"
inline int GetMonthDay(int year, int month)
{
	static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = _monthArray[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)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))
	{
		cout << "!legal" << endl;
	}
}
Date& 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)
			{
				_year++;
				_month = 1;
			}
		}
	}
	return *this;
}
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;
	}
	else {
		_day -= day;
		while (_day <= 0)
		{
			if (_month == 1)
			{
				--_year;
				_month = 12;
				_day += GetMonthDay(_year, _month);
			}
			else
			{
				--_month;
				_day += GetMonthDay(_year, _month);
			}
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
		return tmp;
}
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
		return tmp;
}
Date& Date::operator++()//前置
{
	return 	*this += 1;;
}
Date  Date::operator++(int)//后置
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator--()//前置
{
	return *this -= 1;
}
Date  Date::operator--(int)//后置
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
		return true;
	else if (_year < d._year)
		return false;
	else//_year < d._year
	{
		if (_month > d._month)
			return true;
		else if (_month < d._month)
			return false;
		else//_month < d._month
		{
			if (_day > d._day)
				return true;
			else
				return false;
		}
	}
}
bool  Date::operator<(const Date& d)
{
	return !(*this > d || *this == d);
}
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)
{
	if (_year == d._year)
	{
		if (_month == d._month)
		{
			if (_day == d._day)
				return true;
		}
	}
	return false;
}
bool  Date::operator!=(const Date& d)
{
	//復(fù)用==
	return !(*this == d);
}
int Date::operator-(Date d)
{
	int count = 0;
	Date tmp(*this);
	if (tmp < d)
	{
		while (tmp != d)
		{
			++count;
			tmp += 1;
		}
	}
	else if (tmp > d)
	{
		while (tmp != d)
		{
			--count;
			tmp -= 1;
		}
	}
	return count;
}

到此這篇關(guān)于C++逐步介紹日期類的使用的文章就介紹到這了,更多相關(guān)C++日期類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • cocos2dx-3.10 C++實現(xiàn)滾動數(shù)字

    cocos2dx-3.10 C++實現(xiàn)滾動數(shù)字

    這篇文章主要為大家詳細(xì)介紹了cocos2dx-3.10 C++實現(xiàn)滾動數(shù)字效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • OpenCV利用對比度亮度變換實現(xiàn)水印去除

    OpenCV利用對比度亮度變換實現(xiàn)水印去除

    OpenCV中去除水印最常用的方法是inpaint,通過圖像修復(fù)的方法來去除水印。本文將介紹另一種方法:利用對比度亮度變換去除水印,需要的朋友可以參考一下
    2021-11-11
  • C# 使用反射來實現(xiàn)對象的深度復(fù)制方法

    C# 使用反射來實現(xiàn)對象的深度復(fù)制方法

    下面小編就為大家?guī)硪黄狢# 使用反射來實現(xiàn)對象的深度復(fù)制方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • C++通過COM接口操作PPT

    C++通過COM接口操作PPT

    這篇文章主要為大家詳細(xì)介紹了C++通過COM接口操作PPT的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C++ throw關(guān)鍵字實現(xiàn)拋出異常和異常規(guī)范

    C++ throw關(guān)鍵字實現(xiàn)拋出異常和異常規(guī)范

    本文主要介紹了C++ throw關(guān)鍵字實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • c++ 預(yù)處理的圖靈完備之引言

    c++ 預(yù)處理的圖靈完備之引言

    這篇文章主要介紹了c++ 預(yù)處理的圖靈完備之引言,需要的朋友可以參考下
    2017-07-07
  • C C++ LeetCode題解在二叉樹中增加一行示例詳解

    C C++ LeetCode題解在二叉樹中增加一行示例詳解

    這篇文章主要為大家介紹了C C++ LeetCode題解在二叉樹中增加一行示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • VC中Tab control控件的用法詳細(xì)解析

    VC中Tab control控件的用法詳細(xì)解析

    以下是對VC中Tab control控件的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下哦
    2013-09-09
  • C語言進(jìn)階教程之預(yù)處理

    C語言進(jìn)階教程之預(yù)處理

    C語言提供了多種預(yù)處理功能,如宏定義、文件包含、條件編譯等,下面這篇文章主要給大家介紹了關(guān)于C語言進(jìn)階教程之預(yù)處理的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • C語言中聯(lián)合體與共用體和枚舉使用語法示例

    C語言中聯(lián)合體與共用體和枚舉使用語法示例

    這篇文章主要介紹了C語言中聯(lián)合體與共用體和枚舉使用語法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-12-12

最新評論