C++日期类的实现

文章详细描述了一个名为Date的C++类,用于表示日期,包括构造函数、赋值重载、比较运算符和日期计算方法。测试代码展示了如何使用这个类进行日期操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Date.h

#pragma once

#include<iostream>
#include<assert.h>

using namespace std;

class Date
{
	friend ostream& operator<<(ostream& out,const Date& x);
	friend istream& operator>>(istream& in, Date& x);
public:

	int MonthDay(int year, int month) const;
	Date(int year = 2023, int month = 10, int day = 16);
	Date(const Date& x);

	Date& operator=(const Date x);
	bool operator==(const Date x) const;
	bool operator!=(const Date x) const;
	bool operator<(const Date x) const;
	bool operator<=(const Date x) const;
	bool operator>(const Date x) const;
	bool operator>=(const Date x) const;

	Date& operator+=(int x);
	Date operator+(int x) const;
	Date& operator-=(int x);
	Date operator-(int x) const;
	int operator-(const Date& x) const;

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

	void Print() const;

private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include"Date.h"

int Date::MonthDay(int year, int month) const
{
	assert(0 < month < 13);

	int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (year % 4 == 0 || (year % 400 == 0 && year % 100 != 0))
	{
		day[2]++;
	}
	return day[month];
}

Date::Date(int year, int month, int day)
{
	if ((0 < month < 13) && (0 <= day <= MonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期格式错误" << endl;
		_year = 0;
		_month = 0;
		_day = 0;
	}
}

Date::Date(const Date& x)
{
	_year = x._year;
	_month = x._month;
	_day = x._day;
}

Date& Date::operator=(const Date x)
{
	if (this != &x)
	{
		_year = x._year;
		_month = x._month;
		_day = x._day;
	}

	return *this;
}

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

bool Date::operator!=(const Date x) const
{
	return _year != x._year
		|| _month != x._month
		|| _day != x._day;
}

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

bool Date::operator<=(const Date x) const
{
	return *this < x || *this == x;
}

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

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

Date& Date::operator+=(int x)
{
	if (x < 0)
	{
		*this -= -x;
		return *this;
	}
	_day += x;
	int monthday = 0;
	while (_day > (monthday = MonthDay(_year, _month)))
	{
		_day -= monthday;
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}


Date Date::operator+(int x) const
{
	Date copy = *this;
	copy += x;
	return copy;
}

Date& Date::operator-=(int x)
{
	if (x < 0)
	{
		*this += -x;
		return *this;
	}

	_day -= x;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += MonthDay(_year, _month);
	}
	return *this;
}

Date Date::operator-(int x) const
{
	Date copy = *this;
	copy -= x;
	return copy;
}

int Date::operator-(const Date& x) const//传统计算差值的实现,不好理解,但效率高
{
	int ret = 0;
	int flag = 1;
	Date max = *this;
	Date min = x;
	
	if (max < min)
	{
		max = x;
		min = *this;
		flag = -1;
	}

	while (max._year > min._year)
	{
		if (max._month > 0)
		{
			max._month--;
			if (max._month == 0)
			{
				max._month = 12;
				max._year--;
			}
			max._day += MonthDay(max._year, max._month);
		}
	}
	while (max._month > min._month)
	{
		max._month--;
		max._day += MonthDay(max._year, max._month);
	}
	if (max._day > min._day)
	{
		ret = max._day - min._day;
		max._day = min._day;
	}

	return ret * flag;
}

Date& Date::operator++()
{
	int monthday = 0;
	_day++;
	if (_day > (monthday = MonthDay(_year, _month)))
	{
		_month++;
		_day -= monthday;
		if (_month == 13)
		{
			_month = 12;
			_year++;
		}
	}
	return *this;
}

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

Date& Date::operator--()
{
	int monthday = 0;
	_day--;
	if (_day == 0)
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day = MonthDay(_year, _month);
	}
	return *this;
}

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

//int Date::operator-(const Date& x) const//追赶的思想,方便理解,但效率不如上面的
//{
//	int ret = 0;
//	int flag = 1;
//	Date max = *this;
//	Date min = x;
//
//	if (max < min)
//	{
//		max = x;
//		min = *this;
//		flag = -1;
//	}
//
//	while (max != min)
//	{
//		++min;
//		++ret;
//	}
//
//	return ret * flag;
//}


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


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

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

test.cpp

#include"Date.h"

void test1()
{
	//Date a;
	//Date b(1,1,1);
	//b = a;
	//a = b + 5000;
	//cout <<( a != b ) << endl;
	//a.Print();
	//b.Print();
	//cout << a - b << endl;
	//a++;
	//a.Print();
	//b.Print();

	Date a(2003, 1, 5);
	Date b(2023, 10, 18);
	cout << a << endl;
	cout << b << endl;

	cout << b - a << endl;
	
	
	cout << (a - -500) << endl;
	cout << b << endl;
	cout << "我真NB" << endl;
}

void test2()
{
	Date a;
	cout << a << endl;

	cin >> a;
	cout << a << endl;
}

int main()
{
	test2();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值