日期类 的简单实现

建议和我以下两篇文章结合看
日期类是结合这两篇文章的一个实际应用:
https://blog.csdn.net/sakeww/article/details/115278444
https://blog.csdn.net/sakeww/article/details/122698571

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;

	//实现 > 或 < 其他的复用
	//不仅仅是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);
	Date operator+(int day) const;
	//++d
	Date& operator++();
	// d++;后置为了和前置++,进行区分,增加一下参数占位,跟前置++,构成重载
	Date operator++(int);

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

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

	//今天星期几
	void PrintGetWeekDay() const;


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

private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& out,const Date& d);
istream& operator>>(istream& out,Date& d);

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

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() const {
	cout << _year << "-" << _month << "-" << _day << endl;
}

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];
	if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
		return day += 1;
	return day;
}

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 || * 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;
}
Date Date::operator+(int day) const {
	Date ret(*this);
	ret += day;
	return ret; 
}
Date& Date::operator++(){
	*this += 1;
	return *this;
}
Date Date::operator++(int) {
	Date ret(*this);
	*this += 1;
	return ret;
}

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

		if (_month == 0) {
			_month = 12;
			_year--;
		}
	}
	return *this;
}
Date Date::operator-(int day) const {
	Date ret(*this);
	ret -= day;
	return ret;
}
Date& Date::operator--() {
	*this -= 1;
	return *this;
}
Date Date::operator--(int) {
	Date ret(*this);
	*this -= 1;
	return ret;
}

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::PrintGetWeekDay() const
{
	Date start(1900, 1, 1);
	int count = *this - start;
	cout << "星期" << count % 7 + 1 << endl;
}


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

istream& operator>>(istream& in,Date& d)
{
	cout << "输入日期,空格隔开:" << endl;
	in >> d._year >> d._month >> d._day;
	return in;
} 

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

void Test1()//测试打印
{
	Date d1(2012, 1, 25);
	d1.Print();
}

void Test2()
{
	Date d1(2022, 1, 16);

	Date d2;
	d2 = d1 + 101;
	d2.Print();

	d1 += 100;
	d1.Print();

	Date ret1 = d1++;
	Date ret2 = ++d1;
	ret1.Print();
	ret2.Print();
}

void Test3()
{
	Date d1(2022, 1, 17);

	Date d2;
	d2 = d1 - 101;
	d2.Print();

	d1 -= 100;
	d1.Print();

	Date ret1 = d1--;
	Date ret2 = --d1;
	ret1.Print();
	ret2.Print();
}
void Test4()
{
	Date today(2022, 1, 26);
	Date offerday(2022, 9, 1);

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

}

void Test5()
{
	Date d(2022, 1, 28);
	d.PrintGetWeekDay();
}

int main()
{
	//Test1();//测试打印

	//Test2();//测试+,+=,前置和后置++

	//Test3();//测试-,-=,前置和后置--

	//Test4();//测试日期减日期

	//Test5();//测试星期几

	/*const Date d;
	d.Print();*/

	//Date d1(2022, 1, 19);
	//Date d2(2022, 1, 15);
	//cout << d1 << d2 << endl;

	Date d1;
	cin >> d1;
	d1.Print();


	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值