[C++练习]日期类

date.h

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

class Date{
public:
	Date(int year=1, int month=1, int day=1);//缺省参数最好只在声明处设置
	void Print() const;

	int GetMonthDay(int year,int month) const;

	void PrintWeekDay() const {
		const char* arr[] = { "周一","周二","周三","周四","周五","周六","周日" };
		//不使用匿名对象:
		//Date start(1900, 1, 1);//以1900年1月1日周一为参考日期
		//int count = *this - start;
		//使用匿名对象:参考 http://t.csdn.cn/UzrAH
		int count = *this - Date (1900, 1, 1);

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

	//实现>或<,并实现==,剩下的都可以复用
	//不只是Date类,任意类若要实现比较,都可以用这个方式复用,除非该类不互斥,比如!(>=)不是<
	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;

	Date& operator+=(int day);//需要修改成员变量,不能加const
	Date operator+(int day) const;
	Date& operator-=(int day);//需要修改成员变量,不能加const
	Date operator-(int day) const;//日期减天数,返回日期

	int operator-(const Date& d) const;//日期减日期,返回天数

	//++d1;前置++
	Date& operator++();//编译器调用时会转换成d1.operator++(&d1);
	//d1++;语法规定:重载的后置++为了与前置++区分,需要增加一个参数占位以与前置++构成函数重载
	Date operator++(int);//编译器调用时会转换成d1.operator++(&d1,0);//0由编译器传,可能不是0,随便啥int参数都可以,只是为了区分重载
	//--d1
	Date& operator--();
	//d1--
	Date operator--(int);

	//流插入与流提取重载
	//void operator<<(ostream& out);
	
private:
	int _year;
	int _month;
	int _day;
};

date.cpp

#include"Date.h"

int Date::GetMonthDay(int year, int month) const {
	//直接从下标1开始,1月就是下标1.且因不修改,做成静态的比较好
	static int MonthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = MonthDayArray[month];
	//先判断是否为2月再判断是否闰年,提高效率
	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 << "非法日期->";
		Print();//等于this->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;
	}
}

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

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);//复用>
}

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

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;
	//为支持连续 +=  ,需要返回*this	
}

Date Date::operator+(int day) const {
//d1 + 100
	Date ret(*this);//拷贝构造,这里*this 是d1的地址
	ret += day;//自动调用operator+=对ret进行操作
	return ret;//出作用域ret销毁,所以这个+不可以引用返回,而前面的+=可以
}

Date& Date::operator-=(int day) {
	if (day < 0) {
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0) {
			_month = 12;
			--_year;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;//出作用域*this还在,因此使用引用返回
}

Date Date::operator-(int day) const {
	Date ret(*this);//拷贝构造,这里*this 是d1的地址
	ret -= day;//复用-=
	return ret;//出作用域ret销毁,所以这个-不可以引用返回而前面的-++=可以
}

//d2-d1
int Date::operator-(const Date& d) const {
	Date max = *this;//默认d2天数较大
	Date min = d;
	int flag = 1;
	if (*this < d) {//检查d2是否大于d1
		max = d;
		min = *this;
		flag = -1;
	}
	int count = 0;
	while (min!=max){
		++min;
		++count;
	}
	return count*flag;
}

//++d1//前置++返回++后的值,自定义类型推荐优先用前置++,效率较高
Date& Date::operator++() {
	*this += 1;//复用+=
	return *this;//出作用域*this还在,因此使用引用返回
}
//d1++//后置++返回++前的值,自定义类型不推荐优先用后置++,因为需要多次拷贝构造
Date Date::operator++(int) {//这个int仅作为后置一元操作符的标识
	Date ret(*this);//拷贝构造
	*this += 1;//复用+=,原值++
	return ret;//返回拷贝构造,即原值++前的值。局部变量必须传值返回。
}
//--d1
Date& Date::operator--() {
	*this -= 1;
	return *this;
}
//d1--
Date Date::operator--(int) {
	Date ret(*this);
	*this -= 1;
	return ret;
}

//void Date::operator<<(ostream& out) {
	//out << _year << "/" << _month << "/" << _day <<endl;
//}

//void operator<<(ostream& out, const Date& d) {
//	out << d._year << "/" << d._month << "/" << d._day << endl;
//}

Test.cpp

#include"Date.h"

void TestDate1() {
	Date d1(2023, 3, 7);
	Date d2(2023,2, 29);
	Date d3(2000, 2, 29);
	Date d4(2022, 2, 29);
	d1.Print();
	d2.Print();
	d3.Print();
	d4.Print();

	cout <<"\n"<< endl;
}

void TestDate2() {
	Date d1(2023, 3, 9);
	Date ret=d1 + 100;
	ret.Print();
	d1 += 100;
	d1.Print();
	Date ret2 = ++d1;//d1.operator++(&d1);
	Date ret1 = d1++;//d1.operator++(&d1,0);

	cout << "\n" << endl;
}

void TestDate3() {
	Date d1(2023, 3, 9);

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

	Date ret2 = d1 - 9;//检查边界情况
	ret2.Print();

	Date ret3 = d1 - 180;//检查跨年
	ret3.Print();

	Date ret4 = d1 - 1800;//检查跨闰年
	ret4.Print();

	Date ret5 = d1 - -100;//检查负值操作
	ret5.Print();

	cout << "\n" << endl;
}

void TestDate4() {
	Date d1(2023, 3, 30);
	Date d2(2023, 6, 20);
	cout << (d2 - d1) << endl;//<<运算符优先级较高,需使用括号以优先-运算
	cout << (d1 - d2) << endl;

	d1.PrintWeekDay();

	cout << "\n" << endl;
}

int main() {

	TestDate1();
	TestDate2();
	TestDate3();
	TestDate4();

	Date d1(2023, 6, 28);
	//d1.operator<<(cout);
	//d1 << cout;
	//↑不能是cout<<d1,因为双操作数运算符重载,
	//,第一个参数是左操作数,第二个参数是右操作数,
	//,且因为是在类内的成员函数,会无法避免对象在左侧。
	//若要实现cout << d1,会需要放到全局:
	//operator<<(cout, d1);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值