1. 程式人生 > >C++ 日期(Date)類

C++ 日期(Date)類

一個十分簡單基礎的日期類,主要是運算子過載,不做過多宣告!!!

宣告檔案Date.h

#pragma once

class Date
{
public:
	//建構函式
	Date(int year = 2000, int month = 2, int day = 25);
	//拷貝建構函式
	Date(const Date& d);
	//賦值運算子過載
	Date& operator=(const Date& d);
	//判斷是否為閏年
	bool IsLeapYear(int year);
	//獲取每年中一個月的天數
	int GetDayOfMonth(int year, int month);
	//運算子過載
	Date operator+(int days);//日期+天數
	Date operator-(int days);//日期-天數
	int operator-(const Date& d);//日期-日期
	Date& operator++();//前置++
	Date operator++(int);//後置++
	Date& operator--();//前置--
	Date operator--(int);//後置--
	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;//
private:
	int _year;
	int _month;
	int _day;
};

定義檔案Date.cpp

#define _CRT_SECURE_NO_DEPRECATE 1

#include "Date.h"
#include "assert.h"

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

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

bool Date::IsLeapYear(int year)
{
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
		return true;
	}
	return false;
}

int Date::GetDayOfMonth(int year, int month)
{
	assert(month > 0 && month < 13);
	static int array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (IsLeapYear(year) && month == 2){
		return array[month] + 1;
	}
	else return array[month];
}


Date& Date::operator=(const Date& d)
{
	if (this != &d){
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}


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

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

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

}

bool Date::operator>=(const Date& d)const
{
	return operator>(d) || operator==(d);
}

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

bool Date::operator<=(const Date& d)const
{
	return operator<(d) || operator==(d);
}

Date Date::operator+(int days)
{
	Date ret = *this;
	ret._day += days;
	while (ret._day > GetDayOfMonth(ret._year, ret._month)){
		ret._day -= GetDayOfMonth(ret._year, ret._month);
		ret._month++;
		if (ret._month > 12){
			ret._year++;
			ret._month = 1;
		}
	}
	return ret;
}

Date Date::operator-(int days)
{
	Date ret = *this;
	ret._day -= days;
	while (ret._day < 0){
		ret._day += GetDayOfMonth(ret._year, ret._month);
		ret._month--;
		if (ret._month < 1){
			ret._year--;
			ret._month = 12;
		}
	}
	return ret;
}


int Date::operator-(const Date& d)
{
	int ret = 0;
	Date A = d;
	if (*this > d){
		while (!(this->_year == A._year
			&& this->_month == A._month
			&& this->_day == A._day)){
			A._day++;
			ret++;
			if (A._day > GetDayOfMonth(A._year, A._month)){
				A._month++;
				A._day = 1;
				if (A._month > 12){
					A._year++;
					A._month = 1;
				}
			}
		}
	}
	else{
		while (!(this->_year == A._year
			&& this->_month == A._month
			&& this->_day == A._day)){
			A._day--;
			ret--;
			if (A._day < 1){
				A._month--;
				if (A._month < 1){
					A._year--;
					A._month = 12;
				}
				A._day = GetDayOfMonth(A._year, A._month);
			}
		}
	}
	return ret;
}

Date& Date::operator++()
{
	this->_day++;
	if (this->_day > 
		GetDayOfMonth(this->_year, this->_month)){
		this->_month++;
		if (this->_month > 12){
			this->_year++;
			this->_month = 1;
		}
		this->_day = 1;
	}
	return *this;
}

Date Date::operator++(int)
{
	Date ret = *this;
	/*this->_day++;
	if (this->_day >
		GetDayOfMonth(this->_year, this->_month)){
		this->_month++;
		if (this->_month > 12){
			this->_year++;
			this->_month = 1;
		}
		this->_day = 1;
	}*/
	++*this;
	return ret;
}

Date& Date::operator--()
{
	this->_day--;
	if (this->_day < 1){
		this->_month--;
		this->_day = GetDayOfMonth(this->_year, this->_month);
		if (this->_month < 1){
			this->_year--;
			this->_month = 12;
		}
	}
	return *this;
}

Date Date::operator--(int)
{
	Date ret = *this;
	/*this->_day--;
	if (this->_day < 1){
		this->_month--;
		this->_day = GetDayOfMonth(this->_year, this->_month);
		if (this->_month < 1){
			this->_year--;
			this->_month = 12;
		}
	}*/
	--*this;
	return ret;
}