C++逐步介绍日期类的使用

目录
  • 头文件
  • 详细步骤
    • 第一步
    • 第二步
  • 总代码

我们今天实现一个简单的计算日期

我们这里先给出头文件,后面一个一个解释

头文件

#pragma once
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
	//定义构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	//打印日期
	void Print()
	{
		cout << _year <<" " <<_month<<" " << _day << endl;
	}
	//运算符重载
	Date operator+(int day);
	Date operator-(int day);
	Date& operator+=(int day);
	Date& operator-=(int day);
	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++();//前置
	Date operator++(int);//后置
	Date& operator--();//前置
	Date operator--(int);//后置
	int operator-(Date d);//计算两个日期的差值
private:
	int _year;
	int _month;
	int _day;
};

详细步骤

第一步

计算闰年和判断日期是否合法

inline int GetMonthDay(int year, int month)
{
	//多次调用的时候static能极大减小内存消耗,但是也可以删除,并不影响程序运行
	static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = _monthArray[month];
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//判断是否是闰年
	{
		day = 29;
	}
	return day;
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))//看日期是否合法
	{
		cout << "fail" << endl;
        exit(-1);
	}
}

我们将year,month,day赋值之后,首先进行判断,如果出现异常天数直接终结程序(如2022,1,32,这种不存在的日期)

随后一个问题就出来了,因为闰年和非闰年的2月不同,所以我们又使用一个函数getmonthday,来判断闰年的同时获取当月天数;

有些同学可能直接是【0,31,59(31+28 1月+2月),89(1月+2月+3月)......+.....365】

我们稍后再解释为什么要用我这种定义方式。

第二步

各类运算符重载

我们首先来看运算符”+”和”+=”

这是一般情况下的+

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp._day += day;
	//判断天数是否大于当月的天数
		while (tmp._day > GetMonthDay(tmp._year, tmp._month))
		{
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			tmp._month++;
			//判断月数是否大于12
			if (tmp._month > 12)
			{
		        tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;
}

这是一般情况下的+=

Date& Date::operator+=(int day)
{
	Date ret(*this);
		ret._day += day;
		//判断天数是否超越了当月的天数
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			//判断月数是否超过了12
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
	}
	return *this;
}

我们可以发现两者其实都用了同一个while循环来判断日期是否合法。

我们可以选择将while循环打包成一个新的函数,也可以选择直接复用

+复用+=

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;//直接调用operator的+=
		return tmp;
}

总代码

#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"
inline int GetMonthDay(int year, int month)
{
	static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = _monthArray[month];
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
		day = 29;
	}
	return day;
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))
	{
		cout << "!legal" << endl;
	}
}
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this -= -day;
	}
	else {
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
	}
	return *this;
}
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;
	}
	else {
		_day -= day;
		while (_day <= 0)
		{
			if (_month == 1)
			{
				--_year;
				_month = 12;
				_day += GetMonthDay(_year, _month);
			}
			else
			{
				--_month;
				_day += GetMonthDay(_year, _month);
			}
		}
	}
	return *this;
}
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++()//前置
{
	return 	*this += 1;;
}
Date  Date::operator++(int)//后置
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator--()//前置
{
	return *this -= 1;
}
Date  Date::operator--(int)//后置
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
		return true;
	else if (_year < d._year)
		return false;
	else//_year < d._year
	{
		if (_month > d._month)
			return true;
		else if (_month < d._month)
			return false;
		else//_month < d._month
		{
			if (_day > d._day)
				return true;
			else
				return false;
		}
	}
}
bool  Date::operator<(const Date& d)
{
	return !(*this > d || *this == d);
}
bool Date::operator>=(const Date& d)
{
	return  *this > d || *this == d;
}
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
bool  Date::operator==(const Date& d)
{
	if (_year == d._year)
	{
		if (_month == d._month)
		{
			if (_day == d._day)
				return true;
		}
	}
	return false;
}
bool  Date::operator!=(const Date& d)
{
	//复用==
	return !(*this == d);
}
int Date::operator-(Date d)
{
	int count = 0;
	Date tmp(*this);
	if (tmp < d)
	{
		while (tmp != d)
		{
			++count;
			tmp += 1;
		}
	}
	else if (tmp > d)
	{
		while (tmp != d)
		{
			--count;
			tmp -= 1;
		}
	}
	return count;
}

到此这篇关于C++逐步介绍日期类的使用的文章就介绍到这了,更多相关C++日期类内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++实现日期类(Date)

    本文实例为大家分享了C++实现日期类的具体代码,供大家参考,具体内容如下 #include<iostream> #include<stdlib.h> using namespace std; class Date { public: //构造函数 Date(int year = 1900, int month = 1, int day = 1) :_year(year) , _month(month) , _day(day) { if (!IsInvalidDate(_year,

  • C++类与对象之日期类的实现

    目录 1. 实现前的铺垫 2. 日期类的实现 2.1 日期+=天数 2.2 日期-=天数 2.3 日期-天数 2.4 日期+天数 2.5 前置++ 2.6 后置++ 2.7 前置– 2.8 后置– 2.9 >运算符重载 2.10 ==运算符重载 2.11 >=运算符重载 2.12 <运算符重载 2.13 <=运算符重载 2.14 !=运算符重载 2.15 日期-日期 返回天数 2.16 输出 1. 实现前的铺垫 在实现前,我们要先把类写好,类中包含成员函数和成员变量. 对于日期类来

  • C++实现日期类(Date类)的方法

    如下所示: #include<iostream> using namespace std; class Date { public: Date(int year = 1900, int month = 1, int day = 1) //构造 :_year(year) , _month(month) , _day(day) { if (!isInvalidDate(_year, _month, _day)) { _year = 1900; _month = 1; _day = 1; } } D

  • C++逐步介绍日期类的使用

    目录 头文件 详细步骤 第一步 第二步 总代码 我们今天实现一个简单的计算日期 我们这里先给出头文件,后面一个一个解释 头文件 #pragma once #include<iostream> using std::cout; using std::endl; using std::cin; class Date { public: //定义构造函数 Date(int year = 1900, int month = 1, int day = 1); //打印日期 void Print() {

  • 详解Java中的日期类

    Java 编程语言中时间的处理类有 Date类与 Calendar类.目前官方不推荐使用 Date类,因为其不利于国际化:而是推荐使用 Calendar类,并使用 DateFormat 类做格式化处理. 一.Date 类介绍 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符串. 不过,这些函数的 API 不易于实现国际化.从 JDK 1.1 开始,应该使用 Calenda

  • Java8新特性之线程安全日期类

    LocalDateTime Java8新特性之一,新增日期类. 在项目开发过程中经常遇到时间处理,但是你真的用对了吗,理解阿里巴巴开发手册中禁用static修饰SimpleDateFormat吗 通过阅读本篇文章你将了解到: 为什么需要LocalDate.LocalTime.LocalDateTime[java8新提供的类] Java8新的时间API的使用方式,包括创建.格式化.解析.计算.修改 可以使用Instant代替 Date,LocalDateTime代替 Calendar,DateTi

  • 新手小白学JAVA 日期类Date SimpleDateFormat Calendar(入门)

    目录 1. Date日期类 1.1 Date的构造函数 1.2 Date的构造函数练习 1.3 Date的常用方法练习 2. SimpleDateFormat 2.1 常用构造函数 2.2 日期转换格式工具类练习 2.3 日期转换综合练习 3.Calendar日历类 3.1 概念 3.2 常用方法 3.3 入门案例 3.4 巩固案例 1. Date日期类 类 Date 表示一个特定的瞬间,精确到毫秒 1.1 Date的构造函数 Date() 分配一个 Date 对象,以表示分配它的时间(精确到毫

  • C++日期类(Date)实现的示例代码

    目录 类的定义 确定某年某月有多少天 构造函数 打印日期 日期+=天数 日期+天数 日期-=天数 日期-天数 前置++ 后置++ 后置– 前置– >运算符重载 ==运算符重载 >=运算符重载 <运算符重载 <=运算符重载 !=运算符重载 计算两个日期之间的间隔天数,日期减去日期 类的定义 #pragma once #include < iostream> using std::cout; using std::cin; using std::endl; class Da

  • 一文搞懂Java中的日期类

    目录 一.日期类 1.1 第一代日期类 1.2 第二代日期类Calendar 1.3 第三代日期类 一.日期类 在程序的开发中我们经常会遇到日期类型的操作,Java对日期类型的操作提供了很好的支持.在最初的版本下,java.lang包中的System.currentTimeMillis();可以获取当前时间与协调时间(UTC)1970年1月1日午夜之间的时间差(以毫秒为单位测量).我们往往通过调用该方法计算某段代码的耗时. public class TestTime { public stati

  • Java中常用的日期类图文详解

    目录 前言 Date 为什么Date的大部分方法被弃用 注释 翻译 目前可用方法的测试示例 可用方法 示例 Date小结 Calendar 简单介绍 常用的方法 获取实例 获取日期里的信息 日期的加减与滚动 日期的设置 测试实例代码 DateFormat与SimpleDateFormat DateFormat 常用方法 测试实例 SimpleDateFormat 主要方法 测试示例 编写一个简单的日期工具类 工具类 测试示例 总结 前言 本文将分析Java中的Date.Calendar.Date

  • C++类中const修饰的成员函数及日期类小练习

    目录 一.const修饰类的成员函数 1.问题引出: 2.问题分析 3.const修饰类的成员函数 二. 类的两个默认的&运算符重载 三. 日期类小练习 总结 一.const修饰类的成员函数 1.问题引出: 给出一段简单的代码 代码段: #include <iostream> using std::cin; using std::cout; using std::endl; class Date1 { public: Date1(int year = 2000) 类的全缺省构造函数(可

  • javascript 封装Date日期类实例详解

    javascript-封装Date日期类 (一)对日期进行格式化 自定义Date日期类的format()格式化方法 方式一:(非原创) // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd HH:mm:ss.

随机推荐