[One question per day] 16: Design date calculator

Ideas:

Design a date class with the following functions:

  1. Initialize by incoming year, month, and day.
  2. You can add a number n to return a date pushed n days after that date.
  3. You can subtract a number n and return a date that is n days before the date.
  4. By entering two years, months and days, calculate how many days between

date.h (code example :)

#pragma once

#include <iostream>
using namespace std;

typedef unsigned int _uint;

// .h文件,做类的声明;.cpp文件,做类的实现
class Date {
	int m_year;
	_uint m_month;
	_uint m_day;

	_uint getDayOfYear();
public:
	Date(int y, _uint m, _uint d) :
		m_year(y),
		m_month(m),
		m_day(d)
	{
	}

	Date operator + (_uint delay) const;
	Date operator - (_uint delay) const;
	int operator - (const Date& d) const;

	bool operator < (const Date & d) const;
	friend ostream & operator << (ostream & os, const Date & d);
};

date.cpp (code example :)

#include "date.h"

static _uint getMonthDay(int y, _uint m) {
	if (m > 12 || m == 0) {
		return 0;
	}
	if (m == 4 || m == 6 || m == 9 || m == 11)
		return 30;
	else if (m == 2) {
		return 28 + (((y % 100) && !(y % 4)) || !(y % 400));
	}
	else
		return 31;
}

//得到闰年个数
int getLeapNum(int sy, int ey) {
	int count = 0;
	while (sy <= ey) {
		if (((sy % 4 == 0) && (sy % 100)) || (sy % 400 == 0)) {
			++count;
		}
		++sy;
	}
	return count;
}

Date Date::operator + (_uint delay) const {
	Date res = *this;
	_uint tmp;

#if 1
	//优化算法
	int num_y = delay / 365;
	int num_d = delay % 365;
	int flag = 0;

	if (res.m_month > 2) {
		flag = 1;
	}
	
	int leapNum = getLeapNum(res.m_year + flag, res.m_year + num_y);

	while (num_d < leapNum){
		--num_y;
		leapNum = getLeapNum(res.m_year + flag, res.m_year + num_y);
		num_d += 365;
	}
	num_d -= leapNum;

	res.m_year += num_y;
	delay = num_d;
#endif
	tmp = getMonthDay(res.m_year, res.m_month);
	while (delay >= tmp) {
		delay -= tmp;
		res.m_month++;
		if (res.m_month > 12) {
			res.m_month = 1;
			res.m_year++;
		}
		tmp = getMonthDay(res.m_year, res.m_month);
	}

	res.m_day += delay;

	//不用if是因为存在1月31号,加30天这种特殊情况
	while (res.m_day > tmp) {
		res.m_day -= tmp;
		res.m_month++;
		tmp = getMonthDay(res.m_year, res.m_month);

		if (res.m_month > 12) {
			res.m_month = 1;
			res.m_year++;
		}
	}
	return res;
}

Date Date::operator - (_uint delay) const {
	Date res = *this;
	_uint tmp;

	//存在3月31号,-30天这种特殊情况
	if (delay < res.m_day) {
		res.m_day -= delay;
		return res;
	}

	if (res.m_month == 1) {
		res.m_year--;
		res.m_month = 12;
	}
	else {
		res.m_month--;
	}

	tmp = getMonthDay(res.m_year, res.m_month);
	while (delay >= tmp) {
		delay -= tmp;

		if (res.m_month == 1) {
			res.m_year--;
			res.m_month = 12;
		}
		else {
			res.m_month--;
		}

		tmp = getMonthDay(res.m_year, res.m_month);
	}

	int tmpday = res.m_day - delay;

	if (tmpday <= 0) {
		tmpday += tmp;
	} 
	else {
		res.m_month++;

		if (res.m_month > 12) {
			res.m_month = 1;
			res.m_year++;
		}
	}
	res.m_day = tmpday;

	return res;
}

_uint Date::getDayOfYear() {
	_uint i, days = 0;

	for (i = 1; i < m_month; i++) {
		days += getMonthDay(m_year, i);
	}

	return days + m_day;
}

int Date::operator - (const Date& d) const {
	Date bigger = *this;
	Date smaller = d;
	int flag = 1;
	if (*this < d) {
		bigger = d;
		smaller = *this;
		flag = -1;
	}
	int years = bigger.m_year - smaller.m_year;
	int days = years * 365 + getLeapNum(smaller.m_year, bigger.m_year - 1);

	days = days - smaller.getDayOfYear() + bigger.getDayOfYear();
	return days * flag;
}

ostream & operator << (ostream & os, const Date & d) {
	os << d.m_year << '-' << d.m_month << '-' << d.m_day;
	return os;
}

bool Date:: operator < (const Date& d) const {
	if (m_year < d.m_year) {
		return true;
	}
	if (m_year == d.m_year && m_month < d.m_month) {
		return true;
	}
	if (m_year == d.m_year && m_month == d.m_month&& m_day < d.m_day) {
		return true;
	}
	return false;
}

main.cpp (code example :)

#include "date.h"

int main() {
	//int year, delay;
	//_uint month, day;

	//cout << "请输入开始的年、月、日" << endl;
	//cin >> year >> month >> day;

	//Date test(year, month, day);

	//cout << "请输入间隔的天数" << endl;
	//cin >> delay;
	//cout << "最终的年、月、日为:" << endl;
	//if (delay >= 0) {
	//	cout << (test + delay) << endl;
	//}
	//else {
	//	delay *= -1;
	//	cout << test - delay << endl;
	//}

	int year_1, year_2;
	_uint month_1, day_1, month_2, day_2;
	cout << "请输入开始的年、月、日" << endl;
	cin >> year_1 >> month_1 >> day_1;
	Date test_1(year_1, month_1, day_1);
	cout << "请输入截至的年、月、日" << endl;
	cin >> year_2 >> month_2 >> day_2;
	Date test_2(year_2, month_2, day_2);

	cout << "总共间隔了" << (test_1 - test_2) << "天" << endl;

	return 0;
}

Code generation diagram:
Insert picture description here

Insert picture description here
The graph of the webpage calculator is as follows:
Insert picture description here
Insert picture description here

Published 152 original articles · praised 45 · 10,000+ views

Guess you like

Origin blog.csdn.net/AngelDg/article/details/104932607