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

相关推荐
艾莉丝努力练剑33 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
还债大湿兄2 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
倔强青铜35 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼6 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上6 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc6 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇6 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc