日期类的完整实现

头文件内容:Date.h

#pragma once
#include<iostream>
using namespace std;

class Date 
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1);
	void Print() const;
	int GetMonthDay(int year, int month) 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;
	bool operator!=(const Date& d) const;

	//d1+=100
	Date& operator+=(int day);

	//d1+100
	Date operator+(int day) const;

	//d1-=100
	Date& operator-=(int day);
	Date operator-(int day) const;

	//++d1
	Date& operator++();

	//d1++
	Date operator++(int);

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

	//两个日期之间的差
	int operator-(const Date& d) const;

	void PrintWeekDay() const;

private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

源文件 Date.cpp

#include"Date.h"

int Date::GetMonthDay(int year, int month) const
{
	static int monthDayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = monthDayArray[month];
	// 365天 5小时+
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		day += 1;
	}

	return day;
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (!(_year >= 0
		&& (month > 0 && month < 13)
		&& (day > 0 && day <= GetMonthDay(year, month))))
	{
		cout << "非法日期->";
		// this->Print();
		Print();
	}
}

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

bool Date::operator>(const Date& d)  const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

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

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

// d1 >= d2
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);
}

// d1 += 100 
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}

	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;

		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}

	return *this;
}

// d1 + 100
Date Date::operator+(int day)  const
{
	Date ret(*this);
	//ret.operator+=(day);
	ret += day;

	return ret;
}

// d1 -= 10
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}

	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}

// d1 - 10
Date Date::operator-(int day)  const
{
	Date ret(*this);
	ret -= day;

	return ret;
}

// ++d1;
Date& Date::operator++()
{
	*this += 1;

	return *this;
}

// d1++; 后置为了跟前置++,进行区分
// 增加一下参数占位,跟前置++,构成函数重载
Date Date::operator++(int)
{
	Date ret(*this);
	*this += 1;

	return ret;
}

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

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

// offerDay - today
int Date::operator-(const Date& d)  const
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int count = 0;
	while (min != max)
	{
		++min;
		++count;
	}

	return count * flag;
}


void Date::PrintWeekDay() const
{
	const char* arr[] = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天" };
	
	int count = *this - Date(1900, 1, 1);

	cout << arr[count % 7] << endl;
}

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;
	in >> d._month;
	in >> d._day;

	return in;
}

源文件 Test.cpp

#include"Date.h"

void TestDate1()
{
	Date d1;
	Date d2(2022, 1, 16);
	d1.Print();
	d2.Print();

	Date d3(2022, 13, 15);
	d3.Print();

	Date d4(2022, 2, 29);
	d4.Print();

	Date d5(2000, 2, 29);
	d4.Print();
}

void TestDate2()
{
	Date d1(2022, 1, 16);
	Date ret = d1 + 100;
	ret.Print();

	d1 += 100;
	d1.Print();
}

void TestDate3()
{
	Date d1(2022, 2, 2);

	Date ret1 = d1 - 10;
	ret1.Print();

	Date ret2 = d1 - 17;
	ret2.Print();

	Date ret3 = d1 - 30;
	ret3.Print();

	Date ret4 = d1 - 400;
	ret4.Print();

	Date ret5 = d1 - 1500;
	ret5.Print();

	Date ret6 = d1 - -100;
	ret6.Print();
}
void TestDate4()
{
	Date today(2022, 1, 17);
	Date offerDay(2022, 9, 1);

	cout << (offerDay - today) << endl;
	cout << (today - offerDay) << endl;

	today.PrintWeekDay();

	Date today1(2022, 1, 17);
	today1.PrintWeekDay();

	Date(2022, 1, 17).PrintWeekDay();
}


int main()
{
	/*Date d;
	cin >> d;
	cout << d << endl;*/

	TestDate3();
	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值