C++日期类实现

#pragma once

#include
using namespace std;

class Date
{
	//友元函数
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

//日期成员函数
public:
	//无参默认构造函数
	Date()
		:_year(1)
		, _month(1)
		, _day(1)
	{}

	//有参构造
	Date(int year, int month, int day);

	//拷贝构造函数
	Date(const Date& d);

	//日期显示
	void Print_Date();

	//每月天数获取
	int GetMonthDay(int year,int month);

	//判断相等
	bool operator==(Date d);

	//日期加等天数
	Date& operator+=(int day);

	//日期加天数
	Date operator+(int day);

	//前置++
	Date& operator++();

	//后置++
	Date operator++(int);

	//<
	bool operator<(const Date& d);

	//!=
	bool operator!=(const Date& d);

	//日期减天数
	Date& operator-=(int day);

	//日期减天数
	Date operator-(int day);

	//日期减日期
	int operator-(const Date& d);

//日期成员变量
private:		
	int _year;
	int _month;
	int _day;
};
#define _CRT_SECURE_NO_WARNINGS 1

#include"Date.h"

//获取月份天数
int Date::GetMonthDay(int year, int month)
{
	int marr[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 marr[month] + 1;
	}
	return marr[month];
}

//日期输入
Date::Date(int year, int month, int day)
{
	if (year > 0 && (month > 0 && month < 13)
		&& (day > 0 && day <= GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期错误" << endl;
	}
}

//拷贝构造
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

//输出
void Date::Print_Date()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

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

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

//+
Date Date::operator+(int day)
{
	Date temp(*this);
	temp += day;
	return temp;
}

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

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

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << '/' << d._month << '/' << d._day << endl;
	return out;
}
istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

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

//-
Date Date::operator-(int day)
{
	Date temp(*this);
	temp -= day;
	return temp;
}

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

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

//-
int Date::operator-(const Date& d)
{
	Date Max(*this);
	Date Min(d);
	int flag = 1;

	if (Max < Min)
	{
		Max = d;
		Min = *this;
		flag = -1;
	}

	int day = 0;

	while (Max != Min)
	{
		++Min;
		++day;
	}
	return day*flag;
	int numday[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
}

你可能感兴趣的:(c++)