C++ —— 日期计算器


1. 头文件

cpp 复制代码
#pragma once
#include <iostream>
using namespace std;

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1);
	int GetMonthDay();
	bool operator>(const Date& d) const;
	bool operator>=(const Date& d)const;
	bool operator<(const Date& d)const;
	bool operator<=(const Date& d)const;
	bool operator==(const Date& d)const;
	bool operator!=(const Date& d)const;

	Date& operator+=(int day);
	Date operator+(int day) const;

	Date& operator-=(int day);
	Date operator-(int day) const;

	Date& operator++();
	Date operator++(int);

	Date& operator--();
	Date operator--(int);

	int operator-(const Date& d) const;

	friend ostream& operator<<(ostream& _out, const Date& d);
	friend istream& operator>>(istream& _in, Date& d);
	void Print() const;
	
private:
	int _year;
	int _month;
	int _day;
};

2. 主函数的实现

cpp 复制代码
#include "Date.h"


Date::Date(int year, int month, int day)
	: _year(year)
	, _month(month)
	, _day(day)
{}

int Date::GetMonthDay()
{
	int day[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 day[2] + 1;
	}
	return day[_month];
}

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

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

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

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

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

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

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

Date Date::operator+(int day) const
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

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

Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}


Date& Date::operator++()
{
	*this += 1;
	return *this;
}

Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

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

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

int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return (n * flag);
}

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

ostream& operator<<(ostream& _out, const Date& d)
{
	_out << d._year << "/" << d._month << "/" << d._day;
	return _out;
}

istream& operator>>(istream& _in, Date& d)
{
	_in >> d._year >> d._month >> d._day;
	return _in;
}

3. 目录文件

cpp 复制代码
#include "Date.h"
int main()
{
	int input = 0;
	do {
		cout << "                                **********  欢迎进入日期计算器  ****************" << endl;
		cout << "                                **********  1.计算两日期差几天  ****************" << endl;
		cout << "                                **********  2.计算某日往后n天   ****************" << endl;
		cout << "                                **********  3.计算某日往前n天   ****************" << endl;
		cout << "                                **********  0.  退出系统       ****************" << endl;
		printf("\n\n\n");
		cout << "请选择:";
		cin >> input;
		if (input == 1)
		{
			Date d1;
			Date d2;
			cout << "请输入第一个日期:";
			cin >> d1;
			cout << "请输入第二个日期:";
			cin >> d2;
			cout << d1 << "和" << d2 << "相差" << (d2 - d1) << "天" << endl;
			printf("\n\n\n");
		}
		else if (input == 2)
		{
			Date d1;
			int day = 0;
			cout << "请输入某日日期:";
			cin >> d1;
			cout << "请输入往后多少天:";
			cin >> day;
			cout << d1 << "往后" << day << "天是" << (d1 + day) << endl;
			printf("\n\n\n");
		}
		else if (input == 3)
		{
			Date d1;
			int day = 0;
			cout << "请输入某日日期:";
			cin >> d1;
			cout << "请输入往前多少天:";
			cin >> day;
			cout << d1 << "往前" << day << "天是" << (d1 - day) << endl;
			printf("\n\n\n");
		}
		else if (input == 0)
		{
			break;
		}
		else
		{
			cout << "请输入正确的选项" << endl;
			printf("\n\n\n");
		}

	} while (input);
}

有需要自取 gitee 链接 :日期计算器------有趣的中国人的gitee

相关推荐
喵叔哟4 分钟前
重构代码之用委托替代继承
开发语言·重构
lzb_kkk10 分钟前
【JavaEE】JUC的常见类
java·开发语言·java-ee
SEEONTIME10 分钟前
python-24-一篇文章彻底掌握Python HTTP库Requests
开发语言·python·http·http库requests
Zfox_11 分钟前
【Linux】进程信号全攻略(二)
linux·运维·c语言·c++
起名字真南29 分钟前
【OJ题解】C++实现字符串大数相乘:无BigInteger库的字符串乘积解决方案
开发语言·c++·leetcode
少年负剑去29 分钟前
第十五届蓝桥杯C/C++B组题解——数字接龙
c语言·c++·蓝桥杯
cleveryuoyuo30 分钟前
AVL树的旋转
c++
tyler_download40 分钟前
golang 实现比特币内核:实现基于椭圆曲线的数字签名和验证
开发语言·数据库·golang
小小小~41 分钟前
qt5将程序打包并使用
开发语言·qt
hlsd#41 分钟前
go mod 依赖管理
开发语言·后端·golang