C/C++ — 实现日期计算器

实现一个日期计算器




问题介绍:



今天突然看到一个问题看起来蛮有趣的,跟大家分享一下. 给定任意日期对该日期进行加减天数,最后得出加减后出现

的日期.以及 两个日期你可以得出他们两个之间相隔多少天.(需要考虑闰年,每个月天数不同,我们需要写一个我们

直接可以使用的日期加减 器) 因为时间比较仓促,我也没有写界面,只有其中几个主要的函数的架构思想以及简单的调

试就发出来了.






代码实现:


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

class Date
{

public:

	Date(int year = 1997,int month = 1,int day = 1)
	:years(year)
	, months(month)
	, days(day)
	{
		assert(IScorrect());
	}

	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			years = d.years;
			months = d.months;
			days = d.days;
		}
		return *this;
	}

	Date& operator + (int day)
	{
		while (day > 365)
		{
			if (ISleapyear() && day > 366)
			{
				years++;
				day = day - 366;
			}
			else
			{
				years++;
				day = day - 365;
			}
		}
		while (day >= Getmonthsday())
		{	
			//注意这里的次序问题,一定先减 再加 最后再判断. 如果顺序错了会出BUG的.
			day = day - Getmonthsday(); 
			months++;
			if (months > 12)
			{
				years++;
				months = 1;
			}
		}

		while (day > 0)
		{	
			DateAdvance();
			day = day - 1;
			days++;
		}
		return *this;
	}

	Date& operator - (int day) //先减去一年,然后在使用加的重载,所以你只需要写一个无懈可击的加算法就够了.
	{
		while (day > 365)
		{
			if (ISleapyear() && day > 366)
			{			
				day = day - 366;
				years--;
			}
			else
			{
				day = day - 365;
				years--;
			}
		}
		if (ISleapyear())
		{
			day = 366 - day;
			years--;
		}
		else
		{
			day = 365 - day;
			years--;
		}
		operator+(day);
		return *this;
	}

	void DateAdvance() //用于出现可以进化的情况
	{
		if (days > Getmonthsday())
		{
			months++;
			days = 1;
		}
		if (months > 12)
		{
			years++;
			months = 1;
		}
	}
	
	int operator - (Date D)
	{
		int count = 0;
		if (*this > D)
		{
			while (*this != D)
			{
				D.operator+(1);
				count++;
			}
		}
		else
		{
			while (*this != D)
			{
				operator+(1);
				count++;
			}
		}
		return count;
	}

	bool ISleapyear()
	{
		if ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0))
		{
			return true;
		}
		return false;
	}
	int Getmonthsday()
	{
		int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (ISleapyear() && months == 2)
		{
			return 29;
		}
		return monthDays[months];
	}

	void print()
	{
		cout << "目前的时间为";
		cout << years << "." << months << "." <<days<< endl;
	}

	bool IScorrect()
	{
		if (years > 0 && ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) && days < 367)//闰年
		{
			if (months >0 && months < 13)
			{
				if (days > 0 && days <= Getmonthsday())
				{
					return true;
				}
			}
		}
		else if (years >0 && days < 366) //非闰年
		{
			if (months >0 && months < 13)
			{
				if (days > 0 && days <= Getmonthsday())
				{
					return true;
				}
			}
		}
		return false;
	}

	Date operator += (int day)
	{
		*this = *this + 100;
		return *this;
	}
	Date operator -= (int day)
	{
		return *this = *this - day;
	}
	inline Date& operator++()
	{
		*this += 1;
		return *this;
	}
	inline Date operator++(int)
	{
		Date tmp(*this);
		*this = *this + 1;
		return tmp;
	}

	bool operator == (const Date& d)
	{
		return (years == d.years&& months == d.months&&days == d.days);
	}

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

	bool operator >(const Date& d)
	{
		if (years > d.years ||
			(years == d.years&&months > d.months)
			|| (years == d.years&&months == d.months && days > d.days))
		{
			return true;
		}
		return false;
	}

	bool operator < (const Date& d)
	{
		return !(*this > d);
	}

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

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

private:
	int years;
	int months;
	int days;
};

void Test()
{
	Date d1(2012, 4, 5);
	Date d2(2013, 4, 5);
	d1.print();
 	/*d1 = d1 - 400;*/
	d1.print();
	cout << d1 - d2 << endl;
	d1.print();
	system("pause");
}




总结:


日期类对我们掌握面向对象这里还是一个蛮重要的知识,你至少要能很熟练很正确地自己快速写出这个整个框架,然后

一个一个实 现函数,我只能说很重要,很重要,很重要大家一定要掌握.

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值