日期类的c++语言实现

Date.h

  #pragma once
    #include<iostream>
    using std::cout;
    using std::endl;
    typedef int TimeType;
    class Date {
    public:
    	int GetReYearNum(TimeType year1, TimeType year2) {//返回 y1---y2 【y1,y2)的闰年数
    		int x = year1 > year2 ? year2 : year1;
    		int y= year1 > year2 ? year1 : year2;
    		int num = 0;
    		while (x < y) {
    			if ((year1 % 4 == 0 && year1 % 100 != 0)
    				|| (year1 % 400 == 0))
    			{
    				num++;
    			}
    			x++;
    		}
    		return num;
    	}
    		int GetMonthDay(TimeType year, TimeType month) {//返回月份天数
    	static	int array[13]= { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//多次使用,设为静态数组,可以节省时间
    		if (month == 2) {
    			if ((year % 4 == 0 && year % 100 != 0)
    						|| (year % 400 == 0)) {
    				return 29;
    					}
    		}
    		
    		return array[month];
    	}
    	Date(TimeType year=1990,TimeType month=1,TimeType day=1){//构造函数
    		if (year >= 1900
    			&& month > 0 && month < 13
    			&& day>0 && day < GetMonthDay(year, month)) {
    			_year = year;
    			_month = month;
    			_day = day;
    		}
    		else
    		{
    			cout << "非法操作!" << endl;
    		}
    	}
    	~Date(){ //析构函数
    	}
    	Date(const Date &d) {//拷贝构造
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    	}
    	void Print()
    	{
    		cout << _year << "-" << _month << "-" << _day << endl;
    	}
    
    	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++();		// 
    	Date operator++(int);	// 
    	Date operator--();		// 
    	Date operator--(int);	// 
    	Date operator+(int day) const;
    	Date operator-(int day) const;
    	Date& operator+=(int day);
    	Date& operator-=(int day);
    	int operator-(const Date&d);
    private:
    	TimeType _year;
    	TimeType _month;
    	TimeType _day;
    };

Date.cpp

#include"date.h"
bool Date::operator>(const Date& d) const {
	if ((*this)._year > d._year)
		return true;
	else if ((*this)._year == d._year) {
		if ((*this)._month > d._month)
			return true;
		else if ((*this)._month == d._month&&(*this)._day > d._day) {
			return true;
		}
	}
	return false;
	}
bool Date::operator>=(const Date& d) const {
	if ((*this) == d || (*this) > d)
		return true;
	return false;
	}
bool Date::operator<(const Date& d) const {
	if ((*this) >= d)
		return false;
	return true;
	}
bool Date::operator<=(const Date& d) const {
	if ((*this) > d)
		return false;
	return true;
	}
bool Date::operator==(const Date& d) const {
	if ((*this)._year == d._year
		&& (*this)._month == d._month
		&& (*this)._day == d._day)
		return true;
	return false;
	}
bool Date::operator!=(const Date& d) const {
	if ((*this) == d)
		return false;
	return true;
	}

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

	/*ret._day += day;
	while (ret._day > ret.GetMonthDay(ret._year,ret._month)) {
		ret._day -= ret.GetMonthDay(ret._year,ret._month);
		++ret._month;
		if (ret._month == 13) {
			ret._month = 1;
			++ret._year;
		}
	}*/
	return ret;
}
Date Date::operator-(int day) const {
	Date ret;
	ret = *this;
	ret += -day;
	return ret;
}

Date& Date:: operator+=(int day) {
	_day += day;
	while ((_day > GetMonthDay(_year, _month))
		|| (_day <=0)) {
		if (_day > GetMonthDay(_year, _month)) {
			_day -= GetMonthDay(_year, _month);
					++_month;
					if (_month == 13) {
						_month = 1;
						++_year;
					}
		}
		else if (_day <= 0) {
			--_month;
			if (_month == 0) {
				_month = 12;
				--_year;
			}
			_day += GetMonthDay(_year, _month);					
		}
		
	}
	return *this;
}
Date& Date::operator-=(int day) {
	day = -day;
	*this += day;
	return *this;
}

int Date::operator-(const Date&d) {
	int day = 0;
	for (int i = 1; i < d._month; ++i) {
		day += GetMonthDay(d._year, i);
	}
	day += d._day;
	day = GetReYearNum(this->_year, d._year)+(this->_year-d._year)*365-day;
	for (int i = 1; i < this->_month; ++i) {
		day += GetMonthDay(this->_year, i);
	}
	day += this->_day;
	return day;
}

test.cpp

#include"date.h" 
int main() {
	//Date a(1000, 1, 1);
	Date b(2019, 1,3);
	Date c(2018, 5,11);
	Date d(2017, 2, 15);
	b = c ;
	b.Print();
	c.Print();
	d.Print();
	b += -3;
	b.Print();
	//b = b + 1000;
	//b.Print();
	//Date c(2019, 5, 12);
	//cout << "tianshu :" << c-b<< endl;
	//b.Print();
	/*if (b != c) {
		cout << "abcd" << endl;
	}
	else {
		cout << "dcba" << endl;
	}*/
	getchar();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值