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

C++實(shí)現(xiàn)日期計(jì)算器詳細(xì)代碼示例

 更新時(shí)間:2024年03月05日 09:09:30   作者:蔣志昂  
這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)日期計(jì)算器的相關(guān)資料,基于C++編寫的簡(jiǎn)單的日期計(jì)算器,供大家參考,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

概要

本篇主要探究C++ 實(shí)現(xiàn)日期計(jì)算器

Date類的規(guī)劃

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int year = 2024 , int month = 2, int day = 6);
	Date(const Date& d);
	Date& operator=(const Date& d);
	~Date();
	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) const;
	bool operator==(const Date& d) const;
	bool operator>= (const Date& d) const;
	bool operator< (const Date& d) const;
	bool operator<= (const Date& d) const;
	bool operator!= (const Date& d) const;
	int operator-(const Date& d) const;
	void Print();

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

Date類的實(shí)現(xiàn)

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

Date::Date(int year, int month, int day)
	:_year(year)
	, _month(month)
	, _day(day)
{ }

構(gòu)造函數(shù),他是在創(chuàng)建類的時(shí)候調(diào)用進(jìn)行初始化操作,我們這里聲明與定義分離,所以它的參數(shù)不需要填寫缺省值,缺省值在聲明的時(shí)候定義即可。

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

Date::Date(const Date& x)
	:_year(x._year)
	, _month(x._month)
	, _day(x._day)
{ }

拷貝構(gòu)造函數(shù)和構(gòu)造函數(shù)作用相似,拷貝構(gòu)造函數(shù)是將已有的類的對(duì)象拷貝給新創(chuàng)建的類的對(duì)象,如上所示。

~Date 析構(gòu)函數(shù)

Date::~Date()
{
	_year = _month = _day = 0;
}

構(gòu)析函數(shù)是在對(duì)象即將銷毀前所調(diào)用的函數(shù),常用來清理數(shù)據(jù)空間,這里我們的內(nèi)存都在棧上申請(qǐng)的,所以影響不大。

GetMonthDay 求某年某月的天數(shù)

int Date::GetMonthDay(int year,int month)
{
	int a[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;
	}
	return a[month];
}

該函數(shù)是用來求某年某月的月份天數(shù),通過創(chuàng)建一個(gè)大小為13的數(shù)組a, 再對(duì)二月份進(jìn)行判斷閏年來確定是否為特殊情況,否則返回已設(shè)定好的月份天數(shù)。

operator= 賦值操作符重載

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

該函數(shù)用于對(duì)象與對(duì)象之間的賦值操作,在對(duì)象的年月日進(jìn)行逐一復(fù)制后并返回this指針的解引用。

operator+= 加等操作符重載

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

該函數(shù)用于日期與天數(shù)的相加,在實(shí)現(xiàn)該函數(shù)時(shí)先將this->_day加上想加的day。然后再通過月份的天數(shù)以及月與年的關(guān)系進(jìn)行調(diào)整,如上所示。

operator+ 加號(hào)操作符重載

Date Date::operator+(int day)
{
	Date newdate = *this;
	newdate+=day;
	return newdate;
}

通過復(fù)用加等操作符重載,在不改變*this的情況下,返回相加后的對(duì)象

operator-= 減等操作符重載

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

該函數(shù)和加等操作符重載類似,先將this->_day減去day,然后進(jìn)行月份的天數(shù)和年與月之間的調(diào)整即可。

operator- 減法操作符重載 (日期 - 天數(shù))

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

通過復(fù)用減等操作符重載,在不改變*this的情況下,返回相加后的對(duì)象

operator++ 前置自增操作符重載

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

operator++ 后置自增操作符重載

Date Date::operator--(int)
{
	Date a = *this;
	(*this)+=1;
	return a;
}

operator-- 前置自減操作符重載

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

operator-- 后置自減操作符重載

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

operator< 小于操作符重載

bool Date::operator<(const Date& d)
{
	if(_year<d._year)
	{
		return 1;
	}
	else if(_year==d._year)
	{
		if(_month<d._month)
		{
			return 1;
		}
		else if(_month==d._month)
		{
			if(_day<d._day)
			{
				return 1;
			}
		}
	}
	return 0;
}

operator== 相等操作符重載

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

operator!= 不等操作符重載

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

operator<= 小于或等于操作符重載

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

operator> 大于操作符重載

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

operator>= 大于或等于操作符重載

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

operator- 減法操作符重載(日期 - 日期)

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

通過比較選出最大的日期max與最小的日期min,讓最小的日期min和記錄數(shù)的count進(jìn)行循環(huán)自增,直到max與min相等為止,此時(shí)刻的count就是相差的天數(shù),而flag先前用于比較的就是它的符號(hào),所以返回flag*count。

Print 打印函數(shù)

void Date::Print()
{
	printf("%d %d %d\n",_year,_month,_day);
}

Date 類的測(cè)試

測(cè)試操作符重載(+,+=,- ,-=,x++,++x,x–,–x)

void Test_1()
{
	std::cout << "Test_1:\n";
	Date a(2024, 2, 6);
	std::cout << "a ||";
	a.Print();
	Date b = a;
	std::cout << "b ||";
	b.Print();
	b += 30;
	std::cout << "b += 30 ||";
	b.Print();
	a -= 20;
	std::cout << "a -= 20 ||";
	a.Print();
	Date c = b - 30;
	std::cout << "c 或 b - 30 ||";
	c.Print();
	Date d = a + 20;
	std::cout << "d 或 a + 20 ||";
	d.Print();
	d++;
	std::cout << "++d ||";
	d.Print();
	c--;
	std::cout << "++c ||";
	c.Print();
	Date e = a++;
	std::cout << "e = a++ -> e||";
	e.Print();
	std::cout << "e = a++ -> a||";
	a.Print();
	Date f = a++;
	std::cout << "f = b-- -> f||";
	f.Print();
	std::cout << "f = b-- -> b||";
	b.Print();

	std::cout << "\n";
}

運(yùn)行結(jié)果如下:

測(cè)試操作符重載(>,>=,==,<,<=,!=)

void Test_2()
{
	std::cout << "Test_2:\n";
	Date a(2024, 2, 6);
	std::cout << "a ||";
	a.Print();
	Date b = a + 999;
	std::cout << "b ||";
	b.Print();
	Date c = a - 999;
	std::cout << "c ||";
	c.Print();
	Date d = a;
	std::cout << "d ||";
	d.Print();

	std::cout << "a < b  ->" << (a < b) << std::endl;
	std::cout << "a > b  ->" << (a > b) << std::endl;
	std::cout << "a == b ->" << (a == b) << std::endl;
	std::cout << "a != b ->" << (a != b) << std::endl;

	std::cout << "a < c  ->" << (a < c) << std::endl;
	std::cout << "a > c  ->" << (a > c) << std::endl;
	std::cout << "a == c ->" << (a == c) << std::endl;
	std::cout << "a != c ->" << (a != c) << std::endl;

	std::cout << "a == d ->" << (a == d) << std::endl;
	std::cout << "a != d ->" << (a != d) << std::endl;
	std::cout << "a >= d ->" << (a >= d) << std::endl;
	std::cout << "a <= d ->" << (a <= d) << std::endl;
	std::cout << "a < d  ->" << (a < d) << std::endl;
	std::cout << "a > d  ->" << (a > d) << std::endl;

	std::cout << "\n";
}

運(yùn)行結(jié)果如下:

測(cè)試操作符重載(日期 - 日期)

void Test_3()
{
	std::cout << "Test_3:\n";
	Date a(2024, 2, 6);
	Date b(2025, 2, 6);
	Date c = a + 99;
	std::cout << "c ||";
	c.Print();

	std::cout << "a ||";
	a.Print();
	std::cout << "b ||";
	b.Print();

	std::cout << "a - b ||" << (a - b) << std::endl;
	std::cout << "b - b ||" << (b - b) << std::endl;
	std::cout << "b - a ||" << (b - a) << std::endl;

	std::cout << "c - a ||" << (c - a) << std::endl;
	std::cout << "a - c ||" << (a - c) << std::endl;

	std::cout << "\n";
}

運(yùn)行結(jié)果如下:

測(cè)試完畢,綜合上述代碼及運(yùn)行結(jié)果,可得代碼正確,可以正常模擬日期計(jì)算器。

結(jié)語(yǔ)

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

相關(guān)文章

最新評(píng)論

凯里市| 昆明市| 精河县| 随州市| 阳西县| 秭归县| 郓城县| 咸宁市| 微山县| 盘山县| 措勤县| 北海市| 大渡口区| 雷山县| 汝阳县| 石楼县| 南华县| 怀远县| 甘孜| 张掖市| 禄丰县| 新建县| 榆中县| 阳信县| 曲周县| 阿克陶县| 兴安盟| 从化市| 新宾| 平湖市| 广汉市| 南皮县| 上高县| 大理市| 嘉义市| 五寨县| 当涂县| 清水县| 出国| 兴和县| 方城县|