C++_实现日期类

✨✨ 欢迎大家来到小伞的大讲堂✨✨

🎈🎈养成好习惯,先赞后看哦~🎈🎈

所属专栏:C++学习
小伞的主页:xiaosan_blog

1.日期类的实现接口(date.h)

对于多次调用的函数,我们会实现在头文件中,以提高效率(我会包含在其次标题中); 

class Date

{

public:

// 获取某年某月的天数

int GetMonthDay(int year, int month);

  // 全缺省的构造函数

Date(int year = 1900, int month = 1, int day = 1);

  // 拷贝构造函数

// d2(d1)

Date(const Date& d);

  // 赋值运算符重载

// d2 = d3 -> d2.operator=(&d2, d3)

Date& operator=(const Date& d);

  // 析构函数

~Date();

  // 日期+=天数

Date& operator+=(int day);

  // 日期+天数

Date operator+(int day);

  // 日期-天数

Date operator-(int day);

   // 日期-=天数

Date& operator-=(int day);

  // 前置++

Date& operator++();

  // 后置++

Date operator++(int);

  // 后置--

Date operator--(int);

  // 前置--

Date& operator--();

  // >运算符重载

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);

  // 日期-日期 返回天数

int operator-(const Date& d);

private:

int _year;

int _month;

int _day;

};

1.1 GetMonthDay

int GetMonthDay(int year, int month)
{

    //静态变量
    static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31 };
    //判断是否为闰年

    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    {
        return 29;
    }
    return monthDayArray[month];
}

1.2 swap

注:在往后的C++使用中,多以引用代替指针,以防对NULL指针的解引用 

1.2.1 Date类的交换函数 

void swap(Date& A) {Date tmp = *this;*this = A;A = *this;
}

 1.2.2 年月日的交换函数

void swap(int* x, int* y) {int tmp = *x;*x = *y;*y = tmp;
}

2.日期类的实现接口(date.cpp)

获取某年某月的天数 

Date::Date(int year, int month, int day) {_year = year;_month = month;_day = day;
}

年月日的打印

void Date::Print() {cout << _year << '/' << _month << '/' << _day << endl;
}

2.1 重载运算符(operate)

2.1.1 operate+=

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

2.1.2 operate+

Date Date::operator+(int day) {Date tmp = *this;tmp += day;//operator+=(int day)return tmp;
}

2.1.3 operate<(<=,>,>=以此类推)

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

2.1.4 operate-=

Date& Date::operator-=(int day)
{//如果day为负号,则+=或者+if (day < 0){return *this += (-day);}//先减值_day -= day;while (_day <= 0){--_month;//防止month过0if (_month == 0){_month = 12;--_year;}//先-month是因为取天数,是在上个月中取,并非当前月_day += GetMonthDay(_year, _month);}return *this;
}

2.1.5 operate-

int Date::operator-(const Date& d) {int y1, m1, d1, y2, m2, d2;int arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };y1 = this->_year;m1 = this->_month;d1 = this->_day;y2 = d._year;m2 = d._month;d2 = d._day;int sum2 = 0;int sum1 = 0;if (y1 < y2) {swap(&y1, &y2);swap(&m1, &m2);swap(&d1, &d2);}if (y1 == y2) {if (m1 < m2) {swap(&m1, &m2);swap(&d1, &d2);}}if (y1 == y2) {if (m1 == m2) {if (d1 < d2) {swap(&d1, &d2);}}}//差的年数int x = y1 - y2;//记录2年的天数if (((y2 % 4 == 0) && (y2 % 100 != 0)) || y2 % 400 == 0) {//判断是否为闰年arr[1] = 29;}for (int i = 0; i < m2 - 1; i++) {sum2 += arr[i];}sum2 += d2;//记录1年的天数if (((y1 % 4 == 0) && (y1 % 100 != 0)) || y1 % 400 == 0) {//判断是否为闰年arr[1] = 29;}for (int i = 0; i < m1 - 1; i++) {sum1 += arr[i];}sum1 += d1;for (int i = 0; i < x; i++) {if (((y1 % 4 == 0) && (y1 % 100 != 0)) || y1 % 400 == 0) {sum1 += 366;}else {sum1 += 365;}}return sum1 - sum2;
}

2.2 拷贝构造

因为其内部没有空间的开辟,所以编译器默认的拷贝构造(浅拷贝)也可以使用

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

总结:

date.h

#pragma once#include<iostream>
using namespace std;
#include<assert.h>class Date
{
public:Date(int year = 1900, int month = 1, int day = 1);void Print();int GetMonthDay(int year, int month){static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31 };if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}return monthDayArray[month];}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+(int day);Date& operator+=(int day);Date& operator-=(int day);Date operator-(int day);// 日期-日期 返回天数int operator-(const Date& d);void swap(Date& A) {Date tmp = *this;*this = A;A = *this;}int getDays(const Date& d);void swap(int* x, int* y) {int tmp = *x;*x = *y;*y = tmp;}Date(const Date& d);
private:int _year;int _month;int _day;
};

date.cpp

#define _CRT_SECURE_NO_WARNINGS
#include"date.h"Date::Date(int year, int month, int day) {_year = year;_month = month;_day = day;
}void Date::Print() {cout << _year << '/' << _month << '/' << _day << endl;
}//+=
Date& Date::operator+=(int day) {_day += day;//当day大于该月的日期while (_day>GetMonthDay(_year,_month)) {_day -= GetMonthDay(_year, _month);_month++;//判断是否大于12月if (_month = 13) {_year++;_month = 1;}}return *this;
}Date Date::operator+(int day) {Date tmp = *this;tmp += day;//operator+=(int day)return tmp;
}bool Date::operator<(const Date& d) {if (_year > d._year) {return false;}if (_year < d._year) {return true;}if (_month > d._month) {return false;}if (_month < d._month) {return true;}if (_day >= d._day) {return false;}else {return true;}
}bool Date::operator>(const Date& d) {if (_year < d._year) {return false;}if (_year > d._year) {return true;}if (_month < d._month) {return false;}if (_month > d._month) {return true;}if (_day <= d._day) {return false;}else {return true;}}bool Date::operator>=(const Date& d) {if (_year < d._year) {return false;}if (_year > d._year) {return true;}if (_month < d._month) {return false;}if (_month > d._month) {return true;}if (_day < d._day) {return false;}else {return true;}}bool Date::operator<=(const Date& d) {if (_year > d._year) {return false;}if (_year < d._year) {return true;}//year相同if (_month > d._month) {return false;}if (_month < d._month) {return true;}//month相同if (_day >= d._day) {return false;}else {return true;}}bool Date::operator==(const Date& d) {return _year == d._year && _month == d._month && _day == d._day;}bool Date::operator!=(const Date& d) {Date tmp = *this;return !(tmp == d);}Date Date::operator-(int day) {Date tmp = *this;tmp -= day;return tmp;}Date& Date::operator-=(int day){//如果day为负号,则+=或者+if (day < 0){return *this += (-day);}//先减值_day -= day;while (_day <= 0){--_month;//防止month过0if (_month == 0){_month = 12;--_year;}//先-month是因为取天数,是在上个月中取,并非当前月_day += GetMonthDay(_year, _month);}return *this;}int Date::operator-(const Date& d) {int y1, m1, d1, y2, m2, d2;int arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };y1 = this->_year;m1 = this->_month;d1 = this->_day;y2 = d._year;m2 = d._month;d2 = d._day;int sum2 = 0;int sum1 = 0;if (y1 < y2) {swap(&y1, &y2);swap(&m1, &m2);swap(&d1, &d2);}if (y1 == y2) {if (m1 < m2) {swap(&m1, &m2);swap(&d1, &d2);}}if (y1 == y2) {if (m1 == m2) {if (d1 < d2) {swap(&d1, &d2);}}}//差的年数int x = y1 - y2;//记录2年的天数if (((y2 % 4 == 0) && (y2 % 100 != 0)) || y2 % 400 == 0) {//判断是否为闰年arr[1] = 29;}for (int i = 0; i < m2 - 1; i++) {sum2 += arr[i];}sum2 += d2;//记录1年的天数if (((y1 % 4 == 0) && (y1 % 100 != 0)) || y1 % 400 == 0) {//判断是否为闰年arr[1] = 29;}for (int i = 0; i < m1 - 1; i++) {sum1 += arr[i];}sum1 += d1;for (int i = 0; i < x; i++) {if (((y1 % 4 == 0) && (y1 % 100 != 0)) || y1 % 400 == 0) {sum1 += 366;}else {sum1 += 365;}}return sum1 - sum2;}Date::Date(const Date& d) {_year = d._year;_month = d._month;_day = d._day;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1547146.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Vulhub TheEther_1.0.1靶机详解

项目地址 https://download.vulnhub.com/theether/theEther_1.0.1.zip实验过程 将下载好的靶机导入到VMware中&#xff0c;设置网络模式为NAT模式&#xff0c;然后开启靶机虚拟机 使用nmap进行主机发现&#xff0c;获取靶机IP地址 nmap 192.168.47.1-254根据对比可知theEthe…

【Linux 报错】vim 保存文件时出现 E45: ‘readonly‘ option is set (add ! to override)

一、错误原因 该错误表明当前你尝试保存的是一个 只读文件&#xff0c;该文件权限设置为只读&#xff0c;具有只读的标识 系统为了防止你意外修改该只读文件&#xff0c;因此会阻止对只读文件的保存&#xff08;他怕你修改了你还保存&#xff0c;破坏了只读属性&#xff09; …

实景三维夯实数字乡村孪生底座

随着数字乡村建设的不断推进&#xff0c;实景三维技术在乡村规划、管理、服务等方面发挥着越来越重要的作用。本文将探讨实景三维技术如何夯实数字乡村的孪生底座&#xff0c;为乡村的可持续发展提供强有力的支撑。 一、数字乡村建设的背景 数字乡村建设是推动乡村全面振兴、…

python实现石头,剪刀,布(turtle库简易版)

三角形为剪刀&#xff0c;红色为石头&#xff0c;圆形为布&#xff08;玩家点击&#xff09; 右边为电脑 运行截图&#xff1a; 写的比较简易&#xff0c;包括鼠标的点击&#xff08;主要想应付一下老师的作业&#xff0c;临时写的&#xff09;&#xff0c;很多都有点偏差&am…

51单片机-AD(模拟信号转数字信号)-实验(电压采集)

介绍AD AD转换&#xff08;Analog to Digital Conversion&#xff0c;模数转换&#xff09;是将连续的模拟信号转换为离散的数字信号的过程。这个过程在各种电子设备中都非常重要&#xff0c;特别是在涉及传感器、音频信号、视频信号等需要进行数字化处理的领域。 个人理解&a…

菱形继承、菱形虚拟继承、菱形继承中多态问题、菱形虚拟继承中多态问题

菱形继承以及菱形继承中的多态问题 一、对象模型&#xff08;一&#xff09;菱形继承 & 菱形虚拟继承&#xff08;一&#xff09;菱形继承中多态 & 菱形虚拟继承中多态 二、总结 本文主要叙述菱形继承、菱形虚拟继承、菱形继承中多态、菱形虚拟继承中多态&#xff0c;这…

超全攻略,教你验证第三方电子合同平台的真伪

不了解电子合同不用担心&#xff0c;通过本篇文章&#xff0c;您可以深入了解电子合同以及第三方平台有效性。 如何辨别第三方电子合同平台的真伪&#xff0c;可以从合法性、技术安全、平台、功能、服务等几个方面入手&#xff1a; 1.合法性方面&#xff1a; 资质认证&#…

Sentence Transformers 教程!

Sentence Transformers专注于句子和文本嵌入&#xff0c;支持超过100种语言。利用深度学习技术&#xff0c;特别是Transformer架构的优势&#xff0c;将文本转换为高维向量空间中的点&#xff0c;使得相似的文本在几何意义上更接近。 语义搜索&#xff1a;构建高效的语义搜索系…

计算1 / 1 - 1 / 2 + 1 / 3 - 1 / 4 + 1 / 5 …… + 1 / 99 - 1 / 100 的值,打印出结果

我们写这道题的时候需要俩变量接受&#xff0c;一个总数一个分母&#xff0c;我们发现分母变化是有规律的从1~100循环。 #include<stdio.h> int main() {int i 0;int tag 1;double sum 0.0;for (i 1; i < 101; i){if (i % 2 0){sum sum - 1.0 / i;}else{sum s…

与转录组结合,开发下一代诊断技术,或许是医学AI领域的下一个热点|个人观点·24-09-21

小罗碎碎念 观点分享&#xff1a;科研本身是一件枯燥的事情&#xff0c;所以我们尽可能的去寻找一些同伴&#xff0c;也许前路的风景又会焕然一新。 今天所有的推文都围绕一个人展开——Faisal Mahmood。说实话&#xff0c;今天的状态并不好&#xff0c;写推文的感觉很不对&…

解决Typora图片复制到CSDN无法查看问题

下载安装picgo 山东大学镜像源&#xff1a;https://mirrors.sdu.edu.cn/github-release/Molunerfinn_PicGo 开通阿里云对象存储oss 选择创建 填入内容 购买资源包 创建AccessKey 配置PicGo 设定bucket填入创建bucket名称 注意&#xff1a;设定存储区域只需要填写到区域前缀即…

数字化AI直播革命:无人直播新纪元,真AI赋能未来!

数字化AI直播革命&#xff1a;无人直播新纪元&#xff0c;真AI赋能未来&#xff01; 在数字化浪潮的汹涌澎湃中&#xff0c;一场前所未有的直播革命正悄然兴起&#xff0c;它以AI为核心驱动力&#xff0c;颠覆了传统直播行业的格局&#xff0c;引领我们步入了一个无人直播的新纪…

即插即用篇 | DenseNet卷土重来! YOLOv8 引入全新密集连接卷积网络 | ECCV 2024

本改进已同步到YOLO-Magic框架! 本文重新审视了密集连接卷积网络(DenseNets),并揭示了其在主流的ResNet风格架构中被低估的有效性。我们认为,由于未触及的训练方法和传统设计元素没有完全展现其能力,DenseNets的潜力被忽视了。我们的初步研究表明,通过连接实现的密集连接…

Keil5安装arm和C51共存环境

一、安装前准备 ①Keil5 mdk安装包 ②Keil5 C51安装包 ③注册机 如下 二、安装步骤 ①建立两个文件夹用于存放Keil5 C51安装程序和keil5 mdk安装程序 如下图 ②点击c51v95a.exe进行安装&#xff0c;一路next&#xff0c;安装在Keil5_C51目录下 ③点击MDK539.ext进行安装…

『功能项目』装备齐全特效【78】

本章项目成果展示 我们打开上一篇77鼠标悬停物品显示信息的项目&#xff0c; 本章要做的事情是实现当穿戴六件装备时主角身上显示一个特效功能 首先在资源文件夹中放置一个预制体 编写脚本&#xff1a;PlayerRayClickNavigation.cs 编写脚本&#xff1a;PlayerRayClickNavigat…

2024.9.26 作业 +思维导图

一、作业 1、什么是虚函数&#xff1f;什么是纯虚函数 虚函数&#xff1a;函数前加关键字virtual&#xff0c;就定义为虚函数&#xff0c;虚函数能够被子类中相同函数名的函数重写 纯虚函数&#xff1a;把虚函数的函数体去掉然后加0&#xff1b;就能定义出一个纯虚函数。 2、基…

STL-map/multimap关联式容器

目录 一、基本概念 1.1 简介 1.2 本质 1.3 库原型 二、使用 2.0 迭代器 2.1 构造函数 2.2 插入删除 2.3 查找统计 2.4 数据修改 三、multimap 一、基本概念 1.1 简介 map中所有元素都是pairpair中第一个元素为key&#xff08;键值&#xff09;&#xff0c;起到索引…

Redis篇(环境搭建)

目录 一、安装包 1. Windows版下载地址 2. Linux版下载地址 二、安装Redis 1. 在Linux中安装Redis 2. 在Windows中安装Redis 3. 细节问题 三、Redis服务启动 1. 默认启动 2. 指定配置启动 3. 开机自启 四、Redis服务停止 1. Linux系统中启动和停止Redis 2. Window…

2024准备去面试软件测试岗,高频面试题预测?

金九银十已经到了&#xff0c;想要找工作&#xff0c;或者是想要跳槽的小伙伴们可要做好准备了&#xff0c;给大家总结了软测面试必问的20到面试题&#xff0c;背完面试不用慌&#xff01; 1.项目测试流程你是怎么开展的&#xff1f; 【参考回答】 首先&#xff0c;需求分析…

tauri程序加载本地图片或者文件在前端页面展示

要想在前端页面中展示本地文件或者文件夹&#xff0c;需要使用convertfilesrc这个api&#xff0c;可以非常方便的展示内容&#xff0c;官方文档&#xff1a;tauri | Tauri Apps convertFileSrc甚至位于invoke之前&#xff0c;但我却一直没有注意到它&#xff0c;一方面是因为&…