【C++】日期类

用C++实现一个日期类,完成以下接口

class Date
{
public:
    Date();
    Date(const Date& d);
    Date& operator=(const Date& d);
    Date operator+(int day);
    Date& operator++();
    Date operator++(int);
    Date operator-(int day);
    int operator-(const Date& d);
    Date& operator--();
    Date operator--(int);
    bool operator>(const Date& d);
    bool operator<(const Date& d);
    bool operator==(const Date& d);
    bool operator!=(const Date& d);
    bool operator>=(const Date& d);
    bool operator<=(const Date& d);

    friend ostream& operator<<(ostream& _cout, const Date& d);
    friend istream& operator>>(istream& _cin, Date& d);

private:
    int year;
    int month;
    int day;
    static int op[13];
};

这里面涉及到了运算符重载,构造以及拷贝构造函数

注意输出和输入运算符需要重载成友元函数,这一点在很久之前就讲解过~

实现代码

#include<iostream>
using namespace std;

class Date
{
public:
	Date(int _year = 1900, int _month = 1, int _day = 1)
		:year(_year), month(_month), day(_day)
	{}
	Date(const Date& d)
	{
		this->day = d.day;
		this->month = d.month;
		this->year = d.year;
	}
	Date& operator=(const Date& d);
	Date operator+(int day);
	Date& operator++();
	Date operator++(int);
	Date operator-(int day);
	int operator-(const Date& d);
	Date& operator--();
	Date operator--(int);
	bool operator>(const Date& d);
	bool operator<(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);
	bool operator>=(const Date& d);
	bool operator<=(const Date& d);

	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend istream& operator>>(istream& _cin, Date& d);

private:
	int year;
	int month;
	int day;
	static int op[13];
};

int Date::op[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

bool IsLeapYear(int year)
{
	if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
	{
		return true;
	}
	return false;
}

Date& Date::operator=(const Date& d)
{
	this->day = d.day;
	this->month = d.month;
	this->year = d.year;
	return *this;
}


Date Date::operator+(int day)
{
	int add_year = 0;
	int add_month = 0;
	int tmp_month = this->month;
	while ((day + this->day)>(op[tmp_month] + (tmp_month == 2 && IsLeapYear(this->year + add_year))))//(20+40)>30
	{
		add_month++;
		if (add_month + this->month>12)
		{
			add_month -= 12;
		}

		day -= (op[month] + (tmp_month == 2 && IsLeapYear(this->year + add_year)));
		if (tmp_month != 12)
		{
			tmp_month++;
		}
		else
		{
			add_year++;
			tmp_month = 1;
		}
	}
	this->day += day;
	this->month += add_month;
	this->year += add_year;

	return (*this);
}

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

Date Date::operator++(int)//后置++
{
	Date old(*this);
	++(*this);
	return old;
}

Date Date::operator-(int day)
{
	int sub_year = 0;
	int tmp_month = this->month;
	while (day - this->day >= 0)//1>1
	{
		tmp_month--;
		if (tmp_month == 0)
		{
			tmp_month = 12;
			sub_year++;
		}
		day -= (op[tmp_month] + (tmp_month == 2 && IsLeapYear(this->year - sub_year)));
		if ((tmp_month == 2 && IsLeapYear(this->year - sub_year)) == true)
		{
			day--;
		}
	}
	this->year -= sub_year;
	this->month = tmp_month;
	this->day -= day;
	return (*this);
}

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

Date Date::operator--(int)//后置--
{
	Date old(*this);
	--(*this);
	return old;
}

int Date::operator-(const Date& d)
{
	size_t count = 0;
	Date Temp = d;
	if (Temp>*this)
	{
		while (Temp != *this)
		{
			--Temp;
			count++;
		}
	}
	else
	{
		while (Temp != *this)
		{
			++Temp;
			count++;
		}
	}
	return count;
}

bool Date::operator>(const Date& d)
{
	if (this->year>d.year)
	{
		return true;
	}
	else if (this->year == d.year && this->month > d.month)
	{
		return true;
	}
	else if (this->year == d.year && this->month == d.month && this->day > d.day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator<(const Date& d)
{
	if (this->year<d.year)
	{
		return true;
	}
	else if (this->year == d.year && this->month < d.month)
	{
		return true;
	}
	else if (this->year == d.year && this->month == d.month && this->day < d.day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator==(const Date& d)
{
	if (this->year == d.year && this->month == d.month && this->day == d.day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator!=(const Date& d)
{
	if (this->year != d.year || this->month != d.month || this->day != d.day)
	{
		return true;
	}
	else
	{
		return false;
	}

}

bool Date::operator>=(const Date& d)
{
	if (this->year >= d.year)
	{
		return true;
	}
	else if (this->year == d.year && this->month >= d.month)
	{
		return true;
	}
	else if (this->year == d.year && this->month == d.month && this->day >= d.day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator<=(const Date& d)
{
	if (this->year <= d.year)
	{
		return true;
	}
	else if (this->year == d.year && this->month <= d.month)
	{
		return true;
	}
	else if (this->year == d.year && this->month == d.month && this->day <= d.day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

ostream& operator<<(ostream &_cout, const Date &d)
{
	_cout << d.year << "-" << d.month << "-" << d.day;

	return _cout;
}

istream& operator>>(istream& _cin, Date& d)
{
	_cin >> d.year >> d.month >> d.day;
	return _cin;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值