题解 | #日期差值#

日期差值

https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c

#include <iostream>
using namespace std;
#include <assert.h>

class Date
{
public:
Date(int year, int month, int day)
{
	if (month > 0 && month < 13 && day>0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "非法日期" << endl;
		assert(false);
	}
}

bool operator<(const Date& x) const
{
	if (_year < x._year)
	{
		return true;
	}
	else if (_year == x._year && _month < x._month)
	{
		return true;
	}
	else if (_year == x._year && _month == x._month && _day < x._day)
	{
		return true;
	}
	else
		return false;
}

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

//复用上面写过的<和==实现<=
bool operator<=(const Date& x) const
{
	return *this < x || *this == x;
}

//复用<=实现>
bool operator>(const Date& x) const
{
	return !(*this <= x);
}

//复用<实现>=
bool operator>=(const Date& x) const
{
	return !(*this < x);
}

//复用==实现!=
bool operator!=(const Date& x) const
{
	return !(*this == x);
}


int GetMonthDay(int year, int month)//获得某年某月的具体天数
{
	static int daysArr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//因为查询月份对应的天数这个函数经常调用,每次调用都创建数组太麻烦,直接把这个数组放到静态区,就不用每次调函数都创建数组了
	       //用下标映射月份  0月,1, 2 ........

	//if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && month == 2)//这样写每次都要判断闰年,条件有点多,先判断月份效率高a
	if ((month == 2) && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))//判断是否为2月,是2月则继续判断是否为闰年,闰年2月是29天
	{
		return 29;
	}

	return daysArr[month];
}


Date& operator+=(int day)//+=是会改变原对象的值为新值
{
	if (day < 0)//如果是+=一个负数,就要转换成-=一个正数
	{
		return *this -= -day;
	}

	_day += day;//this->_day-=day
	while (_day > GetMonthDay(_year, _month))//this->_day > GetMonthDay(this->_year, this->_month)
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;
}

//复用+=
Date operator+(int day) const//+不会改变原对象的值,+的结果是存在一个临时对象中,传值返回临时对象的值做结果
{
	Date tmp = *this;//创建一个中间对象,这个对象用*this指向的对象初始化
	tmp += day;//tmp的值+=day,不影响*this指向的对象
	return tmp;//传值返回tmp(过程中会调用拷贝构造函数完成传值返回)
}


//前置++
Date& operator++()
{
	*this += 1;
	return *this;
}
//后置++
Date operator++(int)//增加这个int参数不是为了接收具体的值,仅仅是为了占位,便于区分,跟前置++构成重载
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}


//-=
Date& operator-=(int day)//-=是会改变原对象的值为新值
{
	if (day < 0)//如果是-=一个负数,就要转换成+=一个正数
	{
		return *this += -day;
	}

	_day -= day;
	while (_day <= 0)
	{
		_month--;//向上个月借位
		if (_month == 0)
		{
			_year--;//向上一年借位
			_month = 12;
		}
		_day += GetMonthDay(_year, --_month);
	}

	return *this;
}

//-
Date operator-(int day) const//-不会改变原对象的值,+的结果是存在一个临时对象中,传值返回临时对象的值做结果
{
	Date tmp = *this;
	tmp -= day;//_day-=day也行

	return tmp;
}


//前置--,返回值是-1后的值
Date& operator--()
{
	*this -= 1;

	return *this;
}
//后置--,返回值是-1之前的值
Date operator--(int)
{
	Date tmp = *this;
	*this-=1;

	return tmp;
}

//自定义类型优先用前置++或--


int operator-(const Date& d) const//计算2个对象相差几天
{
	Date bigdate = *this;
	Date smalldate = d;

	if (*this < d)
	{
		bigdate = d;
		smalldate = *this;
	}

	int day = 0;
	while (bigdate != smalldate)
	{
		++smalldate;
		day++;
	}

	return day;
}


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



int main() {
    string s1;
    cin>>s1;
    string year=s1.substr(0,4);
    int y1=stoi(year);
    string month=s1.substr(4,2);
    int m1=stoi(month);
    string day=s1.substr(6,2);
    int a1=stoi(day);    

    string s2;
    cin>>s2;
    string year2=s2.substr(0,4);
    int y2=stoi(year2);
    string month2=s2.substr(4,2);
    int m2=stoi(month2);
    string day2=s2.substr(6,2);
    int a2=stoi(day2);    

    Date d1(y1,m1,a1);
    Date d2(y2,m2,a2);

    cout<<(d1-d2)+1<<endl;

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

03-20 17:35
武汉大学 Java
薛悦悦:强烈推荐!这个笔记写得很清晰 http://github.com/AccumulateMore/CV
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
9305次浏览 86人参与
# 你的实习产出是真实的还是包装的? #
1707次浏览 40人参与
# 巨人网络春招 #
11300次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7424次浏览 43人参与
# 简历第一个项目做什么 #
31529次浏览 327人参与
# 重来一次,我还会选择这个专业吗 #
433325次浏览 3926人参与
# 米连集团26产品管培生项目 #
5673次浏览 214人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186945次浏览 1120人参与
# 牛客AI文生图 #
21408次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152282次浏览 887人参与
# 研究所笔面经互助 #
118864次浏览 577人参与
# 简历中的项目经历要怎么写? #
310019次浏览 4189人参与
# AI时代,哪些岗位最容易被淘汰 #
63364次浏览 800人参与
# 面试紧张时你会有什么表现? #
30482次浏览 188人参与
# 你今年的平均薪资是多少? #
213000次浏览 1039人参与
# 你怎么看待AI面试 #
179828次浏览 1232人参与
# 高学历就一定能找到好工作吗? #
64303次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76429次浏览 374人参与
# 我的求职精神状态 #
447971次浏览 3128人参与
# 正在春招的你,也参与了去年秋招吗? #
363215次浏览 2637人参与
# 腾讯音乐求职进展汇总 #
160572次浏览 1110人参与
# 校招笔试 #
470247次浏览 2962人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务