C++_实现日期类

✨✨ 欢迎大家来到小伞的大讲堂✨✨

🎈🎈养成好习惯,先赞后看哦~🎈🎈

所属专栏:C++学习
小伞的主页:xiaosan_blog

1.日期类的实现接口(date.h)

对于多次调用的函数,我们会实现在头文件中,以提高效率(我会包含在其次标题中); 

class Date

{

public:

// 获取某年某月的天数

int GetMonthDay(int year, int month);

  // 全缺省的构造函数

Date(int year = 1900, int month = 1, int day = 1);

  // 拷贝构造函数

// d2(d1)

Date(const Date& d);

  // 赋值运算符重载

// d2 = d3 -> d2.operator=(&d2, d3)

Date& 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--(int);

  // 前置--

Date& operator--();

  // >运算符重载

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);

  // 日期-日期 返回天数

int operator-(const Date& d);

private:

int _year;

int _month;

int _day;

};

1.1 GetMonthDay

int GetMonthDay(int year, int month)
{

    //静态变量
    static int monthDayArray[13] = { -1, 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 29;
    }
    return monthDayArray[month];
}

1.2 swap

注:在往后的C++使用中,多以引用代替指针,以防对NULL指针的解引用 

1.2.1 Date类的交换函数 

void swap(Date& A) {
	Date tmp = *this;
	*this = A;
	A = *this;
}

 1.2.2 年月日的交换函数

void swap(int* x, int* y) {
	int tmp = *x;
	*x = *y;
	*y = tmp;
}

2.日期类的实现接口(date.cpp)

获取某年某月的天数 

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

年月日的打印

void Date::Print() {
	cout << _year << '/' << _month << '/' << _day << endl;
}

2.1 重载运算符(operate)

2.1.1 operate+=

//+=
Date& Date::operator+=(int day) {
	_day += day;
	//当day大于该月的日期
	while (_day>GetMonthDay(_year,_month)) {
		_day -= GetMonthDay(_year, _month);
		_month++;
		//判断是否大于12月
		if (_month = 13) {
			_year++;
			_month = 1;
		}
	}
	return *this;
}

2.1.2 operate+

Date Date::operator+(int day) {
	Date tmp = *this;
	tmp += day;
	//operator+=(int day)

	return tmp;
}

2.1.3 operate<(<=,>,>=以此类推)

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

2.1.4 operate-=

Date& Date::operator-=(int day)
{
 //如果day为负号,则+=或者+
 if (day < 0)
 {
	 return *this += (-day);
 }
 //先减值
 _day -= day;
 while (_day <= 0)
 {
	 --_month;
	 //防止month过0
	 if (_month == 0)
	 {
		 _month = 12;
		 --_year;
	 }
	 //先-month是因为取天数,是在上个月中取,并非当前月
	 _day += GetMonthDay(_year, _month);
 }
 return *this;
}

2.1.5 operate-

int Date::operator-(const Date& d) {
 int y1, m1, d1, y2, m2, d2;
 int arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 y1 = this->_year;
 m1 = this->_month;
 d1 = this->_day;
 y2 = d._year;
 m2 = d._month;
 d2 = d._day;
 int sum2 = 0;
 int sum1 = 0;
 if (y1 < y2) {
	 swap(&y1, &y2);
	 swap(&m1, &m2);
	 swap(&d1, &d2);
 }
 if (y1 == y2) {
	 if (m1 < m2) {
		 swap(&m1, &m2);
		 swap(&d1, &d2);
	 }
 }
 if (y1 == y2) {
	 if (m1 == m2) {
		 if (d1 < d2) {
			 swap(&d1, &d2);
		 }
	 }
 }
 //差的年数
 int x = y1 - y2;
 //记录2年的天数
 if (((y2 % 4 == 0) && (y2 % 100 != 0)) || y2 % 400 == 0) {
	 //判断是否为闰年
	 arr[1] = 29;
 }
 for (int i = 0; i < m2 - 1; i++) {
	 sum2 += arr[i];
 }
 sum2 += d2;
 //记录1年的天数
 if (((y1 % 4 == 0) && (y1 % 100 != 0)) || y1 % 400 == 0) {
	 //判断是否为闰年
	 arr[1] = 29;
 }
 for (int i = 0; i < m1 - 1; i++) {
	 sum1 += arr[i];
 }
 sum1 += d1;
 for (int i = 0; i < x; i++) {
	 if (((y1 % 4 == 0) && (y1 % 100 != 0)) || y1 % 400 == 0) {
		 sum1 += 366;
	 }
	 else {
		 sum1 += 365;
	 }
 }
 return sum1 - sum2;
}

2.2 拷贝构造

因为其内部没有空间的开辟,所以编译器默认的拷贝构造(浅拷贝)也可以使用

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

总结:

date.h

#pragma once

#include<iostream>
using namespace std;
#include<assert.h>

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

	int GetMonthDay(int year, int month)
	{
		static int monthDayArray[13] = { -1, 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 29;
		}
		return monthDayArray[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 operator+(int day);
	Date& operator+=(int day);
	Date& operator-=(int day);
	Date operator-(int day);

	// 日期-日期 返回天数
	int operator-(const Date& d);
	void swap(Date& A) {
		Date tmp = *this;
		*this = A;
		A = *this;
	}
	int getDays(const Date& d);

	void swap(int* x, int* y) {
		int tmp = *x;
		*x = *y;
		*y = tmp;
	}

	Date(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};

date.cpp

#define _CRT_SECURE_NO_WARNINGS
#include"date.h"

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

void Date::Print() {
	cout << _year << '/' << _month << '/' << _day << endl;
}

//+=
Date& Date::operator+=(int day) {
	_day += day;
	//当day大于该月的日期
	while (_day>GetMonthDay(_year,_month)) {
		_day -= GetMonthDay(_year, _month);
		_month++;
		//判断是否大于12月
		if (_month = 13) {
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day) {
	Date tmp = *this;
	tmp += day;
	//operator+=(int day)

	return tmp;
}

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

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


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

 bool Date::operator<=(const Date& d) {
	 if (_year > d._year) {
		 return false;
	 }
	 if (_year < d._year) {
		 return true;
	 }
	 //year相同
	 if (_month > d._month) {
		 return false;
	 }
	 if (_month < d._month) {
		 return true;
	 }
	 //month相同
	 if (_day >= d._day) {
		 return false;
	 }
	 else {
		 return true;
	 }
 }


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

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

 Date Date::operator-(int day) {
	 Date tmp = *this;
	 tmp -= day;
	 return tmp;
 }
 
 Date& Date::operator-=(int day)
 {
	 //如果day为负号,则+=或者+
	 if (day < 0)
	 {
		 return *this += (-day);
	 }
	 //先减值
	 _day -= day;
	 while (_day <= 0)
	 {
		 --_month;
		 //防止month过0
		 if (_month == 0)
		 {
			 _month = 12;
			 --_year;
		 }
		 //先-month是因为取天数,是在上个月中取,并非当前月
		 _day += GetMonthDay(_year, _month);
	 }
	 return *this;
 }



 int Date::operator-(const Date& d) {
	 int y1, m1, d1, y2, m2, d2;
	 int arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	 y1 = this->_year;
	 m1 = this->_month;
	 d1 = this->_day;
	 y2 = d._year;
	 m2 = d._month;
	 d2 = d._day;
	 int sum2 = 0;
	 int sum1 = 0;
	 if (y1 < y2) {
		 swap(&y1, &y2);
		 swap(&m1, &m2);
		 swap(&d1, &d2);
	 }
	 if (y1 == y2) {
		 if (m1 < m2) {
			 swap(&m1, &m2);
			 swap(&d1, &d2);
		 }
	 }
	 if (y1 == y2) {
		 if (m1 == m2) {
			 if (d1 < d2) {
				 swap(&d1, &d2);
			 }
		 }
	 }
	 //差的年数
	 int x = y1 - y2;
	 //记录2年的天数
	 if (((y2 % 4 == 0) && (y2 % 100 != 0)) || y2 % 400 == 0) {
		 //判断是否为闰年
		 arr[1] = 29;
	 }
	 for (int i = 0; i < m2 - 1; i++) {
		 sum2 += arr[i];
	 }
	 sum2 += d2;
	 //记录1年的天数
	 if (((y1 % 4 == 0) && (y1 % 100 != 0)) || y1 % 400 == 0) {
		 //判断是否为闰年
		 arr[1] = 29;
	 }
	 for (int i = 0; i < m1 - 1; i++) {
		 sum1 += arr[i];
	 }
	 sum1 += d1;
	 for (int i = 0; i < x; i++) {
		 if (((y1 % 4 == 0) && (y1 % 100 != 0)) || y1 % 400 == 0) {
			 sum1 += 366;
		 }
		 else {
			 sum1 += 365;
		 }
	 }
	 return sum1 - sum2;
 }

 Date::Date(const Date& d) {
	 _year = d._year;
	 _month = d._month;
	 _day = d._day;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值