C++实现简单的日期类

date.h
/*******************************************************************************************
*Author:MaJing
*Date:2016_12_24
*Word: if you want,you can do it
*******************************************************************************************/

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

class Date
{
public:
	Date(int y = 1900, int m = 1, int d = 1);
	Date(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); //类中被调用函数在调用函数之后可以找到这个函数吗?
	bool operator!=(const Date& d);
	
	//赋值运算符重载
	Date &operator=(const Date & d);   //返回该类型的引用是为了支持连等

	Date & operator++();//前置++
	Date operator++(int); //后置++
	Date & operator--(); // 前置--
	Date operator--(int); //后置--
	int operator-(const Date &d);//计算两个日期之间差几天
	Date operator-(int day); //计算几天之前的日期
	Date operator+(int day); //计算几天之后的日期
	Date operator+=(int day); //计算几天之后的日期
	int operator-(const Date &d); //计算两个日期之间差几天
	Date &operator-=(int day);//计算几天之前的日期

private:
	//内部函数
	bool IsVaild();  //检查传入参数的合法性
	bool IsLeapYear();
	int GetMonthDay(); //
private:
	int _year;
	int _month;
	int _day;
};

date.cpp
#include "date.h"

Date::Date(int y = 1900, int m = 1, int d = 1)
{
	_year = y;
	_month = m;
	_day = d;
	assert(IsVaild());
}

Date::Date(const Date &d)  //复制构造函数看成构造函数的重载, 只有参数不同 
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

//  比较运算符重载
bool Date::operator>(const Date& d)  //前面的日期在后面的日期之后  敲的不多吧,感觉像在默写
{
	if (_year > d._year)
		return true;
	else if (_year < d._year)
		return false;
	else
	{
		if (_month > d._month)
			return true;
		else if (_month < d._month)
			return false;
		else
			return _day > d._day;
	}
}
bool Date::operator<(const Date& d)
{
	return !(*this > d || *this == d);//比较的是*this 
}
bool Date::operator>=(const Date& d)
{
	return ((*this > d) || (*this == d)); //调用的时候是date对象 编译器处理成this指针  return 后面到底用不用加()
}
bool Date::operator<=(const Date& d)
{
	return ((*this < d) || (*this == d));
}
bool Date::operator==(const Date& d) //类中被调用函数在调用函数之后可以找到这个函数吗?
{
	return (_year == d._year && _month == d._month && _day == d._day);
}
bool Date::operator!=(const Date& d) //类中被调用函数在调用函数之后可以找到这个函数吗?
{
	return !(_year == d._year && _month == d._month && _day == d._day);
}

	//赋值运算符重载
Date &Date::operator=(const Date & d)   //返回该类型的引用是为了支持连等
{
	if (this != &d)  // 此处为比较地址
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

Date & Date::operator++()//前置++
{
	_day += 1;
	if (_day > GetMonthDay())
	{
		if (_month == 12)
		{
			_month = 1;
			_year += 1;
		}
		else
		{
			_month++;
		}
		_day = 1;
	}
	return *this;
}

Date Date::operator++(int) //后置++
{
	Date tmp(*this);  //Date 类型
	operator++();
	return tmp;
}

Date & Date::operator--() // 前置--
{
	_day -= 1;
	if (_day < 1)
	{
		--_month;
		if (_month < 1)
		{
			--_year;
			_month = 12;
		}
		_day = GetMonthDay();
	}
	return *this;   //为什么要返回 是要支持连加
}

Date Date::operator--(int) //后置--
{
	Date tmp(*this);
	operator--();
	return tmp;
}

Date Date::operator-(int day) //计算几天之前的日期
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}

Date Date::operator+(int day) //计算几天之后的日期
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}

Date &Date::operator-=(int day) //计算几天之前的日期
{
	_day -= day;
	while (_day < 1)
	{
		--_month;
		if (_month < 1)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay();
	}
	return *this;
}

Date Date::operator+=(int day) //计算几天之后的日期
{
	_day += day;
	while (_day > GetMonthDay())
	{
		_day -= GetMonthDay();
		_month++;
		if (_month > 12)
		{
			_month = 1;
			_year += 1;
		}
	}
	return *this;
}

int Date::operator-(const Date &d)//计算两个日期之间差几天
{
	Date later = (*this) > d ? (*this) : d;
	Date formar = (*this) < d ? (*this) : d;
	int count = 0;
	while (formar != later)
	{
		++formar;
		++count;
	}
	return count;
}

	//内部函数
bool Date::IsVaild()  //检查传入参数的合法性
{
	if (_year > 0
		&& _month > 0 && _month <13
		&& _day > 0 && _day <= GetMonthDay())
		return true;
	else
		return false;
}

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

int Date::GetMonthDay() //
{
	static const int dayTable[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (_month == 2 && IsLeapYear())
	{
		return dayTable[2] + 1;
	}
	else
		return dayTable[_month];
}

test.c

#include "date.h"

int main()
{
	Date d1(2016, 12,1);
	//d1-=10;  //2015 12 27
	Date d2(2016, 12, 23);
    //cout <<(d1.operator>=(d2)) << endl;  // d1.operator>(d2)  d1>d2;  operator>(d1,d2)由于隐式传参不行
	//d2 += 10; // 2017 1 2 想好你要的结果,就能快一些
	int distance = d2 - d1; // 22
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值