【C++】---日期计算器

【C++】 日期计算器

:可能有时你也会被这样的问题所烦恼,你想要知道自己活了多少天的话,乍一想这该怎么计算呢,捋一捋,要计算平年闰年,每个月多少天等等等苦恼着,下面我们通过C++来实现一个日期计算器,来帮你解决这一切的烦恼。

实现功能

  • 1.年与年,月与月,天与天的加减法。
    2.日期加减一个天数。
    3.计算两个日期相隔多少天。

实现难点

  • 1.引用的正确使用。
    2.不能很好的控制每个月份的天数,我们不妨直接写一个函数接口,把每个月天数存在数组中后面直接很方便的调用就好了。
    3.平年闰年的判断及实现。
  • 总结:这是C++的最基本的一个日期类的实现,可以更好的让我们巩固一下前面所学的默认成员函数,函数重载,缺省参数,以及引用的正确使用,对类和对象有了初步的了解,可以说是入门者的试金石。

代码如下

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#pragma once
using namespace std;


class  Date
{
public:
	//构造函数
	Date(int year = 2018, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	~Date()//析构函数
	{

	}

	//拷贝构造函数
	Date(const Date& d)//必须传引用,否则会出现无限递归
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//赋值重载
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}


	bool IsLeapyear(int year)//判断是否为平年
	{
		return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
	}

	int GetmonthDay(int year, int month)
	{
		static int monthDay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (IsLeapyear(year) && month == 2)
		{
			return 29;
		}
		else
		{
			return monthDay[month];
		}
	}

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

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

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

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

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

	Date& operator+=(int day)
	{
		_day += day;
		while (_day > GetmonthDay(_year, _month))	
		{
			_day =_day- GetmonthDay(_year, _month);
			_month += 1;
			if (_month > 12)
			{
				++_year;
				_month = 1;
			}
		}
			return *this;
	}


	Date operator+(int day)//不会改变原来日期值,日期加上天数
	{
		Date ret= *this;//拷贝this指针
		return ret += day;

	}

	Date& operator-=(int day)
	{
		if (day < 0)
		{
			return *this += (-day);
		}

		_day = _day - day;
		while (_day < 1)
		{
			_month -= 1;
			if (_month == 0)
			{
				_month = 12;
				--_year;
			}

			_day += GetmonthDay(_year, _month);
		}

		return *this;
	}

	Date operator-(int day)
	{
		Date ret = *this;
		return ret -= day;
	}

	int operator-(const Date& d)
	{
		int flag =1;
		int day = 0;
		Date max = *this;
		Date min = d;
		if (*this < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}	

		while (min != max)
		{
				min++;
				++day;
		}

		return day*flag;

	}

	void PrintDate1()
	{
		cout << "原来的日期" << _year << "-" << _month << "-" << _day << endl;
	}

	void PrintDate2()
	{
		cout << "加日期后的日期" << _year << "-" << _month << "-" << _day << endl;
	}

	void PrintDate3()
	{
		cout << "加日期后的日期" << _year << "-" << _month << "-" << _day << endl;
	}

	void PrintDate4()
	{
		cout << "减掉日期后的日期" << _year << "-" << _month << "-" << _day << endl;
	}


	void PrintDate5()
	{
		cout << "日期减掉日期后的日期" << _year << "-" << _month << "-" << _day << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2018, 1, 1);
	d1.PrintDate1();

	Date d2;
	d2=d1 + 66;
	d2.PrintDate2();

	Date d3;
	d3 += 66;
	d3.PrintDate3();

	Date d4 = d1 - 66;
	d4.PrintDate4();

	Date d5=d1-d4;
	d5.PrintDate5();
	system("pause");
	return 0;
}

在这里插入图片描述

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L19002S

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值