简易日期计算器

实现日期计算器的基本功能:

  • 日期+/-天数=返回日期

         实现日期与天数的这个功能中我们需要考虑以下几点:

         1. 二月的天数受闰年影响

         2.当前日期加上day之后,超过当前月份的最大天数,此时需要month++;

            当month超过12时,year++,month置为1

         3.所加day为负数时应该转化成减法运算 

  • 日期+/-日期=返回天数

         实现日期与日期的功能我们可以找出两个日期中的maxdate和mindate,通过一个计数器不断++,使min++不断逼近max值,              当两日期相等时,计数器的大小即为相差的日期天数。

 

代码实现:

---------------------------------date.h----------------------------------------
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>

#include<iostream>	
using namespace std;
class Date
{
public:
	void Display();   //打印函数
	Date(int year = 1900, int month = 1, int day = 1)   //构造函数
	{
		if (year < 1900 || month < 1 || month > 12 || day<1 || day> GetMonthDay(month, day))
		{
			cout << "非法日期" << endl;
		}
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& date) //拷贝构造
	{
		_year = date._year;
		_month = date._month;
		_day = date._day;
	}
	~Date()
	{
		 //析构函数,这里不需要完成对日期的释放
	}
	Date& operator++()      //引用返回,前置++
	{
		*this = *this + 1;
		return *this;
	}
	Date operator++(int)    //后置++
	{
		Date tmp(*this);
		_day += 1;
		return tmp;
	}
	Date& operator--()     //引用返回,前置--
	{
		*this = *this - 1;
		return *this;
	}
	Date operator--(int)    //后置--
	{
		Date tmp(*this);
		_day -= 1;
		return tmp;
	}
	Date operator+(int day)    //传值返回,加法运算符重载
	{
		if (day < 0)
			return *this - (0 - day);
		Date tmp(*this);
		tmp._day += day;
		while (tmp._day > GetMonthDay(tmp._year, tmp._month))
		{
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			++tmp._month;
			if (tmp._month == 13)
			{
				tmp._year += 1;
				tmp._month = 1;
			}
		}
		return tmp;
	}
	Date& operator+=(int day)
	{
		if (day < 0)
			return *this - (0 - day);	
		_day = _day + day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			++_month;
			if (_month == 13)
			{
				_year = _year + 1;
				_month = 1;
			}
		}
		return *this;
	}
	Date operator-(int day)   //传值返回,减法运算符重载
	{
		if (day < 0)
			return *this + (0 - day);
		Date tmp(*this);
		tmp._day -= day;
		while (tmp._day <= 0)
		{
			--tmp._month;
			if (tmp._month < 1)
			{
				tmp._year -= 1;
				tmp._month = 12;
			}
			tmp._day += GetMonthDay(tmp._year, tmp._month);
		}
		return tmp;
	}
	Date& operator-=(int day)
	{
		if (day < 0)
			return *this + (0 - day);
		_day = _day - day;
		while (_day <= 0)
		{
			--_month;
			if (_month < 1)
			{
				_year = _year - 1;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}

	Date& operator=(const Date& date)   //连续赋值运算符重载
	{
		if (this != &date) //防止自己给自己赋值
		{
			_year = date._year;
			_month = date._month;
			_day = date._day;
		}
		return *this;
	}

	bool operator>(const Date& date)const    //大于运算符重载
	{
		if (_year > date._year)
			return true;
		else if (_year == date._year)
		{
			if (_month > date._month)
				return true;
			else if (_month == date._month)
			{
				if(_day > date._day)
					return true;
			}
		}
		return false;
	}
	bool operator<(const Date& date)const    //小于运算符重载
	{
		if (_year < date._year)
			return true;
		else if (_year == date._year)
		{
			if (_month < date._month)
				return true;
			else if (_month == date._month)
			{
				if (_day < date._day)
					return true;
			}
		}
		return false;
	}
	bool operator==(const Date& date)const    //判断日期是否相等
	{
		if (_year == date._year && _month == date._month && _day == date._day)
			return true;
		return false;
	}
	int operator-(const Date& date)   //两日期的差值
	{
		Date mindate(*this);   //默认当前日期较小
		Date maxdate(date);
		if (*this > date)
		{
			mindate = date;
			maxdate = *this;
		}
		int count = 0;
		while (mindate < maxdate)
		{
			++count;
			mindate += 1;
		}
		return count;
	}

private:
	int _year;
	int _month;
	int _day;

	int GetMonthDay(int year, int month)  //判定某个月的日期
	{
		static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (2 == month && IsLeapYear(year))
		{
			days[month] += 1;
		}

		return days[month];
	}
	bool  IsLeapYear(int year)    //判定是否为闰年
	{
		if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400)
			return true;
		return false;
	}
};
---------------------------------------------------------------------------------

-----------------------------Test.c----------------------------------------------
#define _CRT_SECURE_NO_WARNINGS 1
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>

#include<iostream>	
#include"date.h"
using namespace std;

void Date ::Display()
{
	cout << _year << "-" << _month << "-" << _day<<endl;
}
void Test()
{
	Date d1(2018, 11, 5);
	Date d2(d1);
	//Date d3 = d1;
	//d3.Display();
	//d2 = ++d2; 
	//d2 = d2 + 57;
	//d2 += 2;
	//d2 = d2 - 57;
	d2 -= 2;
	int i = d1 - d2;
	cout <<"相差的天数为"<< i << endl;
	d1.Display();
	d2.Display();
	
}
int main()
{
	Test();
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值