【c++】日期类的实现-Class Date

代码实现

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date {
public:// 获取某年某月的天数int GetMonthDay(int year, int month){static int monteDays[13] = { 0,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 monteDays[month];/*if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){return 31;}else if (month == 2){if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 29;}else{return 28;}}else{return 30;}*/}// 全缺省的构造函数Date(int year = 1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;}// 拷贝构造函数// d2(d1)Date(const Date& d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}// 赋值运算符重载//d2 = d3 -> d2.operator=(&d2, d3)Date& operator=(const Date& d){if (this != &d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}return *this;}// 析构函数~Date(){;}// 日期+=天数Date& operator+=(int day){_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;}// 日期+天数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;if (tmp._month == 13){++tmp._year;tmp._month = 1;}}return tmp;*/Date tmp(*this);tmp += day;return tmp;}// 日期-=天数Date& operator-=(int day){_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;}// 日期-天数Date operator-(int day){//Date tmp = *this;Date tmp(*this);tmp -= day;return tmp;}// 前置++Date& operator++(){*this += 1;return *this;}// 后置++Date operator++(int){Date tmp = *this;*this += 1;return tmp;}// 后置--Date operator--(int){Date tmp = *this;*this -= 1;return tmp;}// 前置--Date& operator--(){*this -= 1;return *this;}// >运算符重载bool operator>(const Date& d){if (this->_year > d._year){return true;}else if (this->_year == d._year){if (this->_month > d._month){return true;}else if (this->_month == d._month){if (this->_day > d._day){return true;}}}return false;}// ==运算符重载bool operator==(const Date& d){return _year==d._year&& _month==d._month&& _day==d._day;}// >=运算符重载bool operator >= (const Date& d){return (*this > d)||(*this==d);/*if (this->_year > d._year){return true;}else if (this->_year == d._year){if (this->_month > d._month){return true;}else if (this->_month == d._month){if (this->_day >= d._day){return true;}}}return false;*/}// <运算符重载bool operator < (const Date& d){return !(*this >= d);/*if (this->_year < d._year){return true;}else if (this->_year == d._year){if (this->_month < d._month){return true;}else if (this->_month == d._month){if (this->_day < d._day){return true;}}}return false;*/}// <=运算符重载bool operator <= (const Date& d){return !(*this > d);/*if (this->_year < d._year){return true;}else if (this->_year == d._year){if (this->_month < d._month){return true;}else if (this->_month == d._month){if (this->_day <= d._day){return true;}}}return false;*/}// !=运算符重载bool operator != (const Date& d){return !(*this == d);/*if (this->_year == d._year){if (this->_month == d._month){if (this->_day == d._day){return true;}}}*/return false;}// 日期-日期 返回天数int operator-(const Date& d){int flag = 1;Date max = *this;Date min = d;if (*this < d){int flag = -1;max = d;min = *this;}int n = 0;while (min != max){++min;++n;}return n * flag;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;//1900/1/1Date d2(d1);Date d3(2024,1,28);//2024/1/28d2.operator=(d3);//d2=d3Date d4(2000,1,1);Date d5(2000,1,1);cout << d1.operator<(d3) << endl;cout << d3.operator<(d1) << endl;cout << d2.operator==(d3) << endl;cout << d1.operator==(d3) << endl;cout << d1.operator>(d2) << endl;cout << d2.operator>(d1) << endl;cout << d1.GetMonthDay(2000, 1) << endl;cout << d4.operator>=(d5) << endl;cout << d4.operator>(d5) << endl;cout << d4.operator<=(d5) << endl;cout << d4.operator<(d5) << endl;cout << d4.operator!=(d5) << endl;cout << d4.operator!=(d3) << endl;Date d6(2020, 1, 1);d6.operator+=(20);Date d7 = d6.operator+(30);Date d8(2024, 1, 30);d8.operator-=(30);Date d9 = d8.operator-(30);Date d10(2000, 1, 30);//Date D1 = d10.operator++(0);//d10++//Date D2 = d10.operator--(0);//d10--;//Date D3 = d10.operator++();//++d10;//Date D4 = d10.operator--();//--d10;d10++;d10--;++d10;--d10;Date d11(2024, 1, 30);Date d12(2024, 8, 1);cout << d12 - d11 << endl;cout << d11 - d12 << endl;return 0;
}

测试截图 

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Properties 文件顺序读取问题

我们通常会使用 Properties 去读取 properties 文件的内容&#xff0c;但 Properties 的问题是无法按照 properties 文件的顺序去读取。因为 Properties 继承自Hashtable&#xff0c;是一个基于哈希表的数据结构&#xff0c;元素的存储位置是通过元素的键的哈希值计算出来的&am…

[React源码解析] Fiber (二)

在React15及以前, Reconciler采用递归的方式创建虚拟Dom, 但是递归过程不可以中断, 如果组件的层级比较深的话, 递归会占用线程很多时间, 那么会造成卡顿。 为了解决这个问题, React16将递归的无法中断的更新重构为异步的可中断更新, Fiber架构诞生。 文章目录 1.Fiber的结构2…

Android Studio项目——TCP客户端

目录 一、TCP客户端UI 1、UI展示 2、xml代码 二、TCP客户端数据发送 三、TCP客户端数据接收 一、TCP客户端UI 1、UI展示 2、xml代码 <?xml version"1.0" encoding"utf-8"?> <RelativeLayout xmlns:android"http://schemas.android.…

【LeetCode】617. 合并二叉树(简单)——代码随想录算法训练营Day20

题目链接&#xff1a;617. 合并二叉树 题目描述 给你两棵二叉树&#xff1a; root1 和 root2 。 想象一下&#xff0c;当你将其中一棵覆盖到另一棵之上时&#xff0c;两棵树上的一些节点将会重叠&#xff08;而另一些不会&#xff09;。你需要将这两棵树合并成一棵新二叉树。…

开始一个vue项目

一、新建项目 用cmd命令创建项目 1.1创建文件 以管理员身份打开命令行界面&#xff0c;进入任意一个想要创建项目的文件夹 输入&#xff1a;vue create vue01 1.2创建成功 Successfully created project vue01出现这个说明创建成功1.3项目目录 二、 vue-router 安装和配置的…

C语言-动态内存申请

一、动态分配内存的概述 在数组一章中&#xff0c;介绍过数组的长度是预先定义好的&#xff0c;在整个程序中固定不变&#xff0c;但是在实际的编程中&#xff0c;往往会发生这种情况&#xff0c;即所需的内存空间取决于实际输入的数据&#xff0c;而无法预先确定 。为了解决…

ChatGPT PLUS升级步骤--支付宝、微信

AI伴随着我们已经有一年多了&#xff0c;这一年多里我使用ChatGPT做ppt、生成绘画、写文案、做旅游攻略&#xff0c;还有一些医学知识&#xff0c;医学知识我感觉没有回答的很好&#xff0c;对比于医生给的建议我个人觉得还是医生的比较好&#xff0c;Chat GPT回答的比较官方 …

AWS免费套餐——云存储S3详解

文章目录 前言一、为什么选择S3二、费用估算三、创建S3云存储注册账户登录账户创建存储桶关于官网相关文档 总结 前言 不论个人还是企业&#xff0c;日常开发中经常碰到需要将文档、安装包、日志等文件数据存储到服务器的需求。往常最常用的是云服务器&#xff0c;但是仅仅承担…

leetcode hot100跳跃游戏Ⅱ

本题和上一题还是有不一样的地方&#xff0c;这个题中&#xff0c;我们需要记录我们跳跃的步数并尽可能的满足最小的跳跃步数到达终点。 那么我们还是采用覆盖范围的概念&#xff0c;但是我们需要两个&#xff0c;一个是在当前位置的覆盖范围&#xff0c;另一个是下一步的覆盖…

gerrit 2 升级到 3最新版本(2.16-->3.9.1)--官方升级方式

root用户安装jdk11 和 17 # 11.0.21 支持到gerrit 3.8&#xff0c;gerrit 3.9 得需要 17 [rootlocalhost ~]# cd /usr/local/java/ [rootlocalhost java]# wget https://mirrors.xingyunkeji.com/package/jdk/openjdk-11.0.21%2B9-linux-x64.tar.gz [rootlocalhost java]# wge…

分布式系统面试的秘籍:深入探讨事务、幂等性和补偿事务,掌握二/三阶段提交,了解Sagas事务模型和分布式ID的实战应用!

1、分布式幂等性如何设计&#xff1f;并举例说明 设计分布式系统的幂等性是确保在面对重复请求或操作时系统能够产生相同结果的重要方面。以下是一些设计方法&#xff0c;并结合一个简单的例子说明&#xff1a; 设计方法&#xff1a; 唯一标识符&#xff08;ID&#xff09;:…

网络爬虫详解

网络爬虫&#xff08;Web Crawler&#xff09;是一种自动化程序&#xff0c;用于在互联网上获取和提取数据。它们可以遍历互联网上的网页、收集数据&#xff0c;并进行处理和分析。网络爬虫也被称为网络蜘蛛、网络机器人等。 网络爬虫的工作原理主要是通过模拟浏览器的行为&…

这么复杂的刻度标签怎么绘制?超简单~~

今天我们开始「粉丝要求绘图系列」的第一篇推文 &#xff0c;这个系列我会筛选出需求较多的一类图进行绘制讲解&#xff0c;当然&#xff0c;绘图的数据我们尽可能的全部分享出来(即使涉及一些论文数据&#xff0c;我们也会根据情况进行虚构处理的)&#xff0c;本期的推文重要涉…

安卓之用户数据安全现状以及相关技术优劣分析

一、引言 随着智能手机的普及&#xff0c;安卓平台已成为全球最大的移动操作系统之一。用户数据安全在安卓平台上变得日益重要&#xff0c;因为个人信息、金融交易和企业数据等都存储和传输于这些设备之中。本文将分析安卓平台上用户数据安全的现状&#xff0c;探讨保障数据安全…

亚马逊单账号和关联账号限制导致店铺被封怎么办?

众所周知&#xff0c;亚马逊的规则之一&#xff0c;即禁止卖家在同一个站点拥有两个或多个账号。 亚马逊强制执行“单账号”政策&#xff0c;可以帮助买家减少与不良商家接触。假设卖家绩效下滑、遭到投诉或负面评价&#xff0c;可以随意开设新账号的话&#xff0c;那消费者就…

dfs专题 P1255 数楼梯——洛谷(疑问)

题目描述 楼梯有 &#xfffd;N 阶&#xff0c;上楼可以一步上一阶&#xff0c;也可以一步上二阶。 编一个程序&#xff0c;计算共有多少种不同的走法。 输入格式 一个数字&#xff0c;楼梯数。 输出格式 输出走的方式总数。 输入输出样例 输入 #1复制 4 输出 #1复制…

H5 嵌套iframe并设置全屏

H5 嵌套iframe并设置全屏 上图上代码 <template><view class"mp-large-screen-box"><view class"mp-large-screen-count">// 返回按钮<view class"mp-mini-btn-color mp-box-count" hover-class"mp-mini-btn-hover…

如何在群晖中本地部署WPS Office并实现公网远程访问

文章目录 1. 拉取WPS Office镜像2. 运行WPS Office镜像容器3. 本地访问WPS Office4. 群晖安装Cpolar5. 配置WPS Office远程地址6. 远程访问WPS Office小结 7. 固定公网地址 wps-office是一个在Linux服务器上部署WPS Office的镜像。它基于WPS Office的Linux版本&#xff0c;通过…

C++入门【36-C++ 类构造函数 析构函数】

类的构造函数 类的构造函数是类的一种特殊的成员函数&#xff0c;它会在每次创建类的新对象时执行。 构造函数的名称与类的名称是完全相同的&#xff0c;并且不会返回任何类型&#xff0c;也不会返回 void。构造函数可用于为某些成员变量设置初始值。 下面的实例有助于更好地…

Pandas--安装(2)

安装 pandas 需要基础环境是 Python&#xff0c;Pandas 是一个基于 Python 的库&#xff0c;因此你需要先安装 Python&#xff0c;然后再通过 Python 的包管理工具 pip 安装 Pandas。 使用 pip 安装 pandas: pip install pandas安装成功后&#xff0c;我们就可以导入 pandas …