C++给定一个日期, 计算n天之后的日期

设计一个日期类,包含以下功能: 
1、只能通过传入年月日初始化。 
2、可以加上一个数字n,返回一个该日期后推n天之后的日期。 

值得注意的是, 闰年并不是4年一闰, 我们通常习惯理解为4年一闰
闰年判断条件 (year % 400 == 0 || (year % 4 == 0 && year % 100)), 可以看到, 当年份是4的倍数, 但可以被100整除, 且不能被400整除时, 并不是闰年, 例如2096年是闰年, 按习惯2100年是闰年, 但2100年不能满足判断条件,不是闰年,下一个闰年是2104, 相隔8年, 还可以发现 2100,2200,2300,2500,2600,2700,2900等等都不是闰年, 遇到这种年份前后闰年相隔8年 

Date.h

#pragma once
#include<iostream>
using namespace std;
class Date {
	static int m_s_month[12];
	int m_year;
	int m_month;
	int m_day;
public:
	Date(int y = 0, int m = 0, int d = 0) {
		m_year = y;
		m_month = m;
		m_day = d;
	}
	friend ostream& operator<<(ostream& os, Date& a);
	friend istream& operator>>(istream& os, Date& a);
	static int getMonthDay(int y, size_t n);//返回y年n月的天数
	bool LeapYear(int year)const;
	int countLeapYear(int year)const;
	Date operator+(size_t n)const;
};

Date.cpp

#include "Date.h"
int Date::m_s_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int Date::getMonthDay(int y, size_t n) {
	return m_s_month[n - 1] + (n == 2 && (y % 400 == 0 || (y % 4 == 0 && y % 100)));
}
bool Date::LeapYear(int y)const {
	return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
ostream& operator<<(ostream& os, Date& a) {
	os << a.m_year << "年" << a.m_month << "月" << a.m_day << "日";
	return os;
}
istream& operator>>(istream& os, Date& t) {
	printf("请输入年份\n");
	while (1) {
		os >> t.m_year;
		if (t.m_year >= 0) {
			break;
		}
		cout << "年份必须非负,请重新输入年份\n";
	}
	cout << "请输入月份\n";
	while (1) {
		os >> t.m_month;
		if (t.m_month < 1) {
			cout << "月份必须大于0, 请重新输入\n";
			continue;
		}
		if (t.m_month > 12) {
			cout << "月份必须小于等于12, 请重新输入\n";
			continue;
		}
		break;
	}
	cout << "请输入日\n";
	while (1) {
		os >> t.m_day;
		if (t.m_day < 1) {
			cout << "请输入大于0的天数\n";
			continue;
		}
		if (t.m_day > Date::getMonthDay(t.m_year, t.m_month)) {
			cout << "你输入的号数大于本月天数" << Date::getMonthDay(t.m_year, t.m_month) << "请重新输入\n";
			continue;
		}
		break;
	}
	return os;
}
int Date::countLeapYear(int year)const {
	if (m_year == year) {
		return LeapYear(year);
	}
	int begin = m_year;
	int end = year - 1;
	if (m_year > year) {
		begin = year;
		end = m_year - 1;
	}
	while (!(LeapYear(begin))) {
		++begin;
	}
	while (!(LeapYear(end))) {
		--end;
	}
	int tmp = begin;
	int count = 0;
	if (begin <= end) {
		while (tmp <= end && tmp % 100) {
			++tmp;;
		}
		for (; tmp <= end; !LeapYear(tmp) ? ++count : count, tmp += 100);
		return (end - begin) / 4 + 1 - count;
	}
	return 0;
}
Date Date::operator+(size_t n)const {
	Date tmp = *this;
	size_t count = 0;
	for (int i = 1; i < (int)tmp.m_month; ++i) {
		count += getMonthDay(tmp.m_year, i);
	}
	count += tmp.m_day;
	if (n >= (365 + LeapYear(tmp.m_year) - count)) {
		n -= (365 + LeapYear(tmp.m_year) - count);
		tmp.m_month = 12;
		tmp.m_day = 31;
		while ((int)n >= (365 + LeapYear(tmp.m_year + 1))) {
			n -= (365 + LeapYear(++tmp.m_year));
		}
	}
	for (; n > 0; --n) {
		if (tmp.m_day == getMonthDay(tmp.m_year, tmp.m_month)) {
			tmp.m_day = 1;
			tmp.m_month == 12 ? tmp.m_month = 1, ++tmp.m_year : ++tmp.m_month;
		}
		else {
			++tmp.m_day;
		}
	}
	return tmp;
}

main.cpp

#include"Date.h"
int main() {
	int n;
	Date a, b;
	cout << "请输入日期\n";
	cin >> a;
	cout << "你想知道多少天后的日期,请输入\n";
	cin >> n;
	b = a + n;
	cout << a << endl;
	cout << "在" << n << "天后是";
	cout << b << endl;
	system("pause");
	return 0;
}

 

  • 10
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值