我是靠谱客的博主 温婉烤鸡,这篇文章主要介绍时间类的实现要求Date.h其中几个巧妙实现的函数完整代码Date.c,现在分享给大家,希望可以做个参考。

文章目录

  • 要求Date.h
  • 其中几个巧妙实现的函数
    • (1)比较日期
    • (2)计算日期的加法
    • (3)计算日期的减法
    • (4)计算日期之差
  • 完整代码Date.c

要求Date.h

Date.h

#pragma once
#include<iostream>
using namespace std;

 class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1);

	// Date(const Date& d);
	// Date& operator=(const Date& d);
	// ~Date();
//两个时间的比较
	inline bool operator>(const Date& d);
	inline bool operator<(const Date& d);
	inline bool operator<=(const Date& d);
	inline bool operator>=(const Date& d);
	inline bool operator==(const Date& d);
	inline bool operator!=(const Date& d);

//时间的加减法运算
	Date operator+(int day);
	Date& operator+=(int day);
	Date operator-(int day);
	Date& operator-=(int day);
	int operator-(const Date& d);
	Date operator++(); // ++d => d.operator++(&d)
	Date operator++(int); // d++ => d.operator(&d, 0);
	Date operator--(); // --d 
	Date operator--(int); // d--
	
	//重复调用次数比较多,定义为内敛
	inline int GetMonthDay(int year, int month);
	
	//距离1年1月1日的天数
	int Date::GetDays(const Date& d);

	void Print();
private:
	int _year;
	int _month;
	int _day;
};

其中几个巧妙实现的函数

(1)比较日期

优化程序
1.先实现一个>和一个==,其他比较全部都可以在此基础上实现

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

	}
	return false;
}

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

(2)计算日期的加法

优化程序
1.因为有平年和闰年之分所以
定义GetMonthDay(int year, int month)得到某年某月的总天数。
先直接把天数加到_day上,如果_day大于当前月的天数,就月份递增,天数减少,然后继续这个过程。
2.我们可以先实现+=然后再实现+
因为如果先实现+再实现+=会多一次拷贝数据的次数。
3.变量的前置和后置++,我们选择前置,因为后置++比前置++多了两次数据的拷贝。
4.注意day为负数的情况,如果day为负数,就直接调用减法进行运算

int Date::GetMonthDay(int year, int month)
{
										//1	 2	 3	 4	 5	 6	 7	 8	 9	 10	 11	 12
	const static int monthday[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 monthday[month];
}

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

(3)计算日期的减法

程序优化
1.实现方法和加法类似。
2.这里实现的是-,建议先实现-=再实现=。
3.前置–比后置–更加好,少两次拷贝

//注意day为负的情况
Date Date::operator-(int day)//这里返回值只能用Date 而不能是Date&
{
	if (day < 0)
	{
		return Date::operator+(-day);
	}
	Date tmp(*this);
	tmp._day -= day;
	while (tmp._day <= 0)
	{
		//月份减1
		--tmp._month;
		//年份减1
		if (tmp._month == 0)
		{
			tmp._month = 12;
			--tmp._year;
		}
		//加上上个月的天数
		tmp._day += GetMonthDay(tmp._year,tmp._month);
	}
	return tmp;//一次
}

(4)计算日期之差

程序优化
方法1-巧
1.分别计算两个时间距离公元第一天的天数
2.两个天数之差就是日期之差
3.日期之差是正数
方法2-暴力
让比较小的日期不断增加直到与大的日期相等,记录增加的天数,增加的天数就是日期之差。

方法1代码

int Date::GetDays(const Date& d)
{
	int ret = 0;
	ret += d._day-1;//因为没有0年
	for (int i = 1; i < d._month; i++)
	{
		ret += GetMonthDay(d._year, i);
	}
	ret += d._year * 365 + d._year / 400 + d._year / 4 - d._year / 4;
	return ret;
}

//得到两个日期的天数之差
int Date::operator-(const Date& d)
{

	//计算两个日期离0年0月0日差  再求两者之差
	int ret = 0;
	int d1 = GetDays(*this);
	int d2 = GetDays(d);
	if (d1 >= d2)
	{
		return d1 - d2;
	}
	else
	{
		cout << "Date Invalid" << endl;
	}
	return 0;
}

完整代码Date.c

Date.c

#include"Date.h"


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 << "Date Invalid" << endl;
	}

}

// Date(const Date& d);
// Date& operator=(const Date& d);
// ~Date();

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

	}
	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||(*this)==d);
}

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

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


//注意day为负的情况
Date Date::operator+(int day)
{
	Date tmp(*this);//一 //这样只有两次拷贝
	(*this) += day;
	return tmp;//二
}

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



//注意day为负的情况
Date Date::operator-(int day)//这里返回值只能用Date 而不能是Date&
{
	if (day < 0)
	{
		return Date::operator+(-day);
	}
	Date tmp(*this);
	tmp._day -= day;
	while (tmp._day <= 0)
	{
		//月份减1
		tmp._month--;
		//年份减1
		if (tmp._month == 0)
		{
			tmp._month = 12;
			tmp._year--;
		}
		//加上上个月的天数
		tmp._day += GetMonthDay(tmp._year,tmp._month);
	}
	return tmp;//一次
}

Date& Date::operator-=(int day)
{
	Date &tmp = (*this);
	tmp = tmp - day;//这里有两次拷贝,tmp拷贝到临时变量,临时变量再拷贝到接收的变量
	return tmp;
}

//得到两个日期的天数之差
int Date::operator-(const Date& d)
{

	//计算两个日期离0年0月0日差  再求两者之差
	int ret = 0;
	int d1 = GetDays(*this);
	int d2 = GetDays(d);
	if (d1 >= d2)
	{
		return d1 - d2;
	}
	else
	{
		cout << "Date Invalid" << endl;
	}
	return 0;
}

// ++d => d.operator++(&d)
Date Date::operator++()
{
	

	(*this) = (*this) + 1;
	return (*this);
}

// d++ => d.operator(&d, 0);
Date Date::operator++(int)
{
	Date tmp(*this);
	(*this) = (*this) + 1;
	return tmp;
}

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

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

int Date::GetMonthDay(int year, int month)
{
										//1	 2	 3	 4	 5	 6	 7	 8	 9	 10	 11	 12
	const static int monthday[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 monthday[month];
}

//这个函数计算的是距离公元后第一天的天数
int Date::GetDays(const Date& d)
{
	int ret = 0;
	ret += d._day-1;//因为没有0年
	for (int i = 1; i < d._month; i++)
	{
		ret += GetMonthDay(d._year, i);
	}
	ret += d._year * 365 + d._year / 400 + d._year / 4 - d._year / 4;
	return ret;
}


void  Date::Print()
{
	cout << _year << "-" << _month << "-"<<_day << endl;
}


最后

以上就是温婉烤鸡最近收集整理的关于时间类的实现要求Date.h其中几个巧妙实现的函数完整代码Date.c的全部内容,更多相关时间类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部