日期类的实现

Dast.h

cpp 复制代码
#pragma once
#define DATE_H

class Date
{
public:
	Date(int year = 0, int month = 1, int day = 1);
	void  Print()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);//后
	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;
	int operator -(const Date& d) const;
private:
	int GetMonthDay(int year, int month) const;
	int _year;
	int _month;
	int _day;
};

Dast.cpp(包含测试代码)

cpp 复制代码
#include"Date.h"
#include<iostream>
using namespace std;

Date::Date(int year, int month, int day)
{
	if (year >= 0
		&& month >= 1 && month <= 12
		&& day >= 1  && day <= GetMonthDay(year, month) )
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "非法日期" << endl;
		_year = 0;
		_month = 1;
		_day = 1;
	}
}
void Date::Print() const
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
int  Date::GetMonthDay(int year, int month) const
{
	static int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = days[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		day += 1;
	}
	return day;
}
Date& Date::operator+=(int day)
{
	//1 加日期 2 判断日期是否大于该月 减该月日期月++ 3判断月份 大于12 年++
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;//返回当前对象本身的引用
}
Date Date::operator+(int day) const
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}
Date& Date::operator-=(int day)
{
	//1 减出天 2 循环若天一直小于1 月-- 
	// 3判断月小于1 小于则年-- 月= 12  没循环一次就加上一个月的天数 
	_day -= day;
	while (_day < 1)
	{
		_month--;
		if (_month < 1)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	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 = tmp + 1;
	return tmp;
}
Date& Date::operator-- ()
{
	*this -= 1;
	return *this;
}
Date Date::operator-- (int)//返回临时对象的值而非引用
{
	Date tmp(*this); //保存当前状态
	*this = tmp - 1;
	return tmp;
}
//年小则小 年等月 小则小 年月等 天小则小
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)
		{
			if (_day > d._day)
				return true;
		}
	}
	return false;
}

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 || *this == d;
}
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);
}
//日期-日期
int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int DayCount = 0;
	while (min != max)
	{
		++min;
		++DayCount;
	}
	return DayCount * flag;
}


void TestDate() {
	// 测试构造函数
	Date d1(2023, 10, 1);
	Date d2(2024, 2, 29);  // 2024是闰年,合法日期
	Date d3(2023, 2, 29);  // 2023不是闰年,非法日期

	cout << "d1: ";
	d1.Print();  // 2023-10-1
	cout << "d2: ";
	d2.Print();  // 2024-2-29
	cout << "d3: ";
	d3.Print();  // 非法日期,输出默认值0-1-1

	// 测试+=和+运算符
	Date d4(2023, 12, 31);
	d4 += 1;
	cout << "d4 += 1: ";
	d4.Print();  // 2024-1-1

	Date d5 = d4 + 31;
	cout << "d4 + 31: ";
	d5.Print();  // 2024-2-1

	// 测试-=和-运算符
	Date d6(2024, 1, 1);
	d6 -= 1;
	cout << "d6 -= 1: ";
	d6.Print();  // 2023-12-31

	Date d7 = d6 - 30;
	cout << "d6 - 30: ";
	d7.Print();  // 2023-11-30

	// 测试前置++和后置++
	Date d8(2023, 12, 30);
	++d8;
	cout << "++d8: ";
	d8.Print();  // 2023-12-31

	Date d9 = d8++;
	cout << "d8++后d8: ";
	d8.Print();  // 2024-1-1
	cout << "d8++返回值: ";
	d9.Print();  // 2023-12-31

	// 测试前置--和后置--
	Date d10(2024, 1, 2);
	--d10;
	cout << "--d10: ";
	d10.Print();  // 2024-1-1

	Date d11 = d10--;
	cout << "d10--后d10: ";
	d10.Print();  // 2023-12-31
	cout << "d10--返回值: ";
	d11.Print();  // 2024-1-1

	// 测试比较运算符
	Date d12(2023, 10, 1);
	Date d13(2023, 12, 31);
	Date d14(2024, 1, 1);

	cout << "d12 > d13: " << (d12 > d13 ? "true" : "false") << endl;  // false
	cout << "d13 < d14: " << (d13 < d14 ? "true" : "false") << endl;  // true
	cout << "d12 == d13: " << (d12 == d13 ? "true" : "false") << endl;  // false
	cout << "d12 != d13: " << (d12 != d13 ? "true" : "false") << endl;  // true
	cout << "d13 <= d14: " << (d13 <= d14 ? "true" : "false") << endl;  // true

	// 测试日期-日期
	Date d15(2023, 1, 1);
	Date d16(2023, 12, 31);
	int days = d16 - d15;
	cout << "2023-12-31 与 2023-1-1 相差: " << days << "天" << endl;  // 364天

	Date d17(2024, 1, 1);
	days = d17 - d15;
	cout << "2024-1-1 与 2023-1-1 相差: " << days << "天" << endl;  // 365天
}

int main() {
	TestDate();
	return 0;
}
相关推荐
大翻哥哥3 小时前
Python 2025:AI工程化与智能代理开发实战
开发语言·人工智能·python
会开花的二叉树3 小时前
彻底搞懂 Linux 基础 IO:从文件操作到缓冲区,打通底层逻辑
linux·服务器·c++·后端
在下雨5993 小时前
项目讲解1
开发语言·数据结构·c++·算法·单例模式
再努力"亿"点点3 小时前
Sklearn(机器学习)实战:鸢尾花数据集处理技巧
开发语言·python
清朝牢弟3 小时前
Win系统下配置PCL库第一步之下载Visual Studio和Qt 5.15.2(超详细)
c++·qt·visual studio
Jayyih3 小时前
嵌入式系统学习Day36(简单的网页制作)
学习·算法
深耕AI3 小时前
【MFC视图和窗口基础:文档/视图的“双胞胎”魔法 + 单文档程序】
c++·mfc
饭碗的彼岸one3 小时前
C++ 并发编程:异步任务
c语言·开发语言·c++·后端·c·异步
脑洞代码4 小时前
20250909的学习笔记
算法