C++日期类的实现

日期加减天数思路图解:

 

代码:

test.c

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include"Date.h"
using namespace std;
void TestData1()
{
	Date d1;
	Date d2(2022, 4, 6);
	Date d3(2000, 4, 6);
	d1.Print();
	d2.Print();
	d3.Print();
}
void TestData2()
{
	Date d1(2022, 4, 6);
	//这里要注意一点:+=改变的是本身,+不改变
	Date ret = d1 + 100;
	ret.Print();

	d1 += 100;
	d1.Print();

	Date ret1 = d1++;//d1.operator(&d1,0);
	ret1.Print();

	Date ret2 = ++d1;//d1.operator(&d1);
	ret2.Print();
}
void TestData3()
{
	Date d1(2022, 1, 17);

	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 TestData4()
{
	Date today(2022, 4, 7);
	Date offerday(2022, 7, 10);

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

	today.PrintGetWeekDay();
}
int main()
{
	//TestData1();
	//TestData2();
	//TestData3();
	TestData4();
	return 0;
}

 Date.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"Date.h"

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1);
	void Print();
	int GetMonthDay(int year, int month);

	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);
	//这六个成员函数只需要实现两个其他的实现就可以复用
	//不仅仅是Date类可以这样实现,所有的类实现比较,都可以用这种复用的方式

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

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

	//offerday-today
	int operator-(const Date& d);

	//打印日期是星期几
	void PrintGetWeekDay();
private:
	int _year;
	int _month;
	int _day;
};

Date.c

#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"


int Date::GetMonthDay(int year, int month)
{
	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;//如果是闰年并且是二月,那么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();
	}
}//自行设计一个构造函数来判断用户输入的日期是否合法
void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
bool Date::operator>(const Date& d)//bool operator>(Date* const this,const Date& d)
{
	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)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
//d1>=d2
bool Date::operator>=(const Date& d)
{
	return *this > d || *this == d;
}
//d1<d2
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
//d1<=d2
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
//d1!=d2
bool Date::operator!=(const Date& d)
{
	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++;//_month要先减了当月的天数再++
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;//这里返回的是d1
}
//d1+100
Date Date::operator+(int day)
{
	Date ret(*this);//运用拷贝构造函数拷贝一个ret
	ret += day;//ret.operator+=(day)

	return ret;//ret出了作用域被销毁了,不能用引用返回
}
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)//天数小于等于0说明不合法需要再处理
	{
		_month--;//注意这里要先将month--
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);//最后再处理day
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date ret(*this);//运用拷贝构造函数拷贝一个ret
	ret -= day;//复用-=

	return ret;
}
//++d1.自定义类型++推荐使用前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;//返回++之后的值
}
//d1++;后置++为了跟前置++进行区分,增加了一个参数占位,跟前置++构成函数重载
Date Date::operator++(int)
{
	Date ret(*this);//运用拷贝构造函数拷贝一个ret
	*this += 1;
	return ret;//返回++之前的值
}
Date& Date::operator--()
{
	*this -= 1;
	return *this;//返回--之后的值
}
Date Date::operator--(int)
{
	Date ret(*this);//运用拷贝构造函数拷贝一个ret
	*this -= 1;
	return ret;//返回++之前的值
}
//offerday-today
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;//默认假设为第一个大第二个小
	int flag = 1;//设定一个flag为正数1,第一个大于第二个时相减为正数
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;//如果第一个小第二个大时第一个和第二个相减时结果为负数,将flag置为1
	}
	int count = 0;//利用计数器去不断++,最终count就是相差的天数
	while (min != max)
	{
		++min;
		++count;
	}
	return count * flag;
}
void Date::PrintGetWeekDay()
{
	const char* arr[] = { "星期一","星期二","星期三", "星期四", "星期五", "星期六", "星期日" };
	Date start(1900, 1, 1);
	int count = *this - start;
	cout << arr[count % 7] << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~|Bernard|

你的鼓励是我写下去最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值