题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

#include <iostream>
using namespace std;

int Count = 0;
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	int Days(int year, int month)
	{
		int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (month == 2)
			if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
			    return 29;
		return a[month];
	}
	bool operator==(Date d)
	{
		if (d._year == this->_year&&d._month == this->_month&&d._day == this->_day)
			return true;
		return false;
	}
	Date& operator++()
	{
		this->_day += 1;
		if (this->_day>Days(this->_year, this->_month))
		{
			this->_month++;
			this->_day = 1;
		}
		if (this->_month>12)
		{
			this->_year++;
			this->_month = 1;
		}
		return *this;
	}
	int operator-(Date d)
	{
		int res = 1;
		while (!(*this == d))
		{
			++res;
			++d;
		}
		return res;
	}
private:
	int _year;
	int _month;
	int _day;

};
int main()
{
	int year, month, day;
	cin >> year >> month >> day;
	Date d1(year, month, day);
	Date d2(year);

	cout << d1 - d2 << endl;
	return 0;
}

全部评论

相关推荐

03-27 17:33
门头沟学院 Java
代码飞升:同学院本,你要注意hr当天有没有回复过,早上投,还要打招呼要推销自己,不要一个劲投
点赞 评论 收藏
分享
求面试求offer啊啊啊啊:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务