【C++---5】日期类的实现


class-date.h

#pragma once

#include <iostream>

using namespace std;

构造、析构、拷贝构造函数:


class Date
{
public:
		Date(int year = 1900, int month = 1, int day = 1)											 
		{
			//参数合法性校验
			if (_year >= 1900 && _month > 0 && _month < 13 && _day
				>0 && _day < GetMonthDay(year, month))
			{
				this->_year = year;
				this->_month = month;
				this->_day = day;
			}
		}

		~Date()
		{
				
		}

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

赋值运算符的重载:

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

运算符的重载:

+号:

		Date operator+(int days)
		{
			Date tmp(*this);
			tmp._day += days;
			return tmp;
		}

-号:

		Date operator-(int days)
		{
			Date tmp(*this);
			tmp._day -= days;
			return tmp;
		}

前置++:

		Date& operator++()
		{
			*this=*this+1;
			return *this;
		}

后置++:

		Date operator++(int)
		{
			Date tmp(*this);
			*this = *this + 1;
			return tmp;
		}

+=运算:

		Date& operator+=(int days)//5.25 6.35 6.4
		{
			_day += days;

			while (_day > GetMonthDay(_year, _month))
			{
				_day -= GetMonthDay(_year, _month);
				_month += 1;

				if (_month > 12)
				{
					_year += 1;
					_month = 1;
				}
			}
			return *this;
		}

-=运算:

		Date& operator-=(int days)//6.17 6.7
		{
			//5.11 -20 4.41 4.21
			//5.11-11=4.30 4.30-9=4.21
			while (_day < days)
			{
				_month -= 1;

				if (_month == 0)
				{
					_year -= 1;
					_month = 12;
				}
				_day += GetMonthDay(_year, _month);
			}
			_day -= days;
			return *this;
		}

比较运算符的重载:

>:

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

<:

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

>=:

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

<=:

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

==:

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

!=:

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

得出日期相差天数:

		int operator-(const Date& d)
		{
			Date min(*this);
			Date max(d);
			int flag = -1;

			if (*this > d)
			{
				max = *this;
				min = d;
				flag = 1;
			}

			int count = 0;
			while (min != max)
			{
				count++;
				min+=1;
			}
			return flag*count;
		}

打印日期:

		void PrintDate()
		{
			cout << _year << "-" << _month << "-" << _day << endl;
		}

获取当前月份天数:

		int GetMonthDay(int year, int month)
		{
			int array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

			if (month == 2 && isYear(year) == true)
			{
				++array[2];
			}
			return array[month];
		}

判断是否是闰年:

		bool isYear(int year)
		{
			if (year % 400 == 0)
			{
				return true;
			}
			else if (year % 100 != 0 && year % 4 == 0)
			{
				return true;
			}
			return false;
		}

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

class-date.c

#include "class-date.h"

int main()
{
		Date d1(2019, 6, 18);
		cout << "d1:";
		d1.PrintDate();
		cout << endl;

		Date d2;
		d2 = d1 - 1;
		cout << "d2=d1-1:";
		d2.PrintDate();
		cout << endl;

		Date d3(2019, 6, 17);
		cout << "d3:";
		d3.PrintDate();
		cout << endl;

		if (d3 == d2)
		{
			cout << "d3 is the same as d2 !" << endl;
		}
		cout << endl;

		cout << "d3-=40:";
		d3 -= 40;
		d3.PrintDate();
		cout << endl;

		int days = d3 - d2;
		cout << "d3与d2相差:";
		cout << days << endl;
		cout << endl;

		if (d3 < d2)
		{
			cout << "d3 is earlier than d2 !" << endl;
		}
		cout << endl;

		cout << "出生日期:";
		Date d4(1996, 9, 10);
		d4.PrintDate();
		cout << endl;

		d4 += 10000;
		cout << "出生10000天纪念日:";
		d4.PrintDate();
		cout << endl;

		system("pause");
		return 0;
}

代码运行测试图:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值