C++ —— 日期计算器


1. 头文件

#pragma once
#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1);int GetMonthDay();bool operator>(const Date& d) const;bool operator>=(const Date& d)const;bool operator<(const Date& d)const;bool operator<=(const Date& d)const;bool operator==(const Date& d)const;bool operator!=(const Date& d)const;Date& operator+=(int day);Date operator+(int day) const;Date& operator-=(int day);Date operator-(int day) const;Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);int operator-(const Date& d) const;friend ostream& operator<<(ostream& _out, const Date& d);friend istream& operator>>(istream& _in, Date& d);void Print() const;private:int _year;int _month;int _day;
};


2. 主函数的实现

#include "Date.h"Date::Date(int year, int month, int day): _year(year), _month(month), _day(day)
{}int Date::GetMonthDay()
{int day[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 day[2] + 1;}return day[_month];
}bool Date::operator>(const Date & d)const
{if (_year > d._year){return true;}else if (_year == d._year){if (_month > d._month){return true;}else if (_month == d._month){return _day > d._day;}}return false;
}bool Date::operator>=(const Date& d)const
{return *this > d || *this == d;
}bool Date::operator<(const Date& d)const
{return !(*this >= d);
}bool Date::operator<=(const Date& d)const
{return !(*this > d);
}bool Date::operator==(const Date& d)const
{return _year == d._year &&_month == d._month &&_day == d._day;
}bool Date::operator!=(const Date& d)const
{return !(*this == d);
}Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){_month = 1;++_year;}}return *this;
}Date Date::operator+(int day) const
{Date tmp = *this;tmp += day;return tmp;
}Date& Date::operator-=(int day)
{_day -= day;while (_day < 1){--_month;if (_month < 1){_month = 12;--_year;}_day += GetMonthDay();}return *this;
}Date Date::operator-(int day) const
{Date tmp = *this;tmp -= day;return tmp;
}Date& Date::operator++()
{*this += 1;return *this;
}Date Date::operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}Date& Date::operator--()
{*this -= 1;return *this;
}Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}int Date::operator-(const Date& d) const
{int flag = 1;Date max = *this;Date min = d;if (*this < d){flag = -1;max = d;min = *this;}int n = 0;while (min != max){++min;++n;}return (n * flag);
}void Date::Print() const
{cout << _year << "/" << _month << "/" << _day << endl;
}ostream& operator<<(ostream& _out, const Date& d)
{_out << d._year << "/" << d._month << "/" << d._day;return _out;
}istream& operator>>(istream& _in, Date& d)
{_in >> d._year >> d._month >> d._day;return _in;
}


3. 目录文件

#include "Date.h"
int main()
{int input = 0;do {cout << "                                **********  欢迎进入日期计算器  ****************" << endl;cout << "                                **********  1.计算两日期差几天  ****************" << endl;cout << "                                **********  2.计算某日往后n天   ****************" << endl;cout << "                                **********  3.计算某日往前n天   ****************" << endl;cout << "                                **********  0.  退出系统       ****************" << endl;printf("\n\n\n");cout << "请选择:";cin >> input;if (input == 1){Date d1;Date d2;cout << "请输入第一个日期:";cin >> d1;cout << "请输入第二个日期:";cin >> d2;cout << d1 << "和" << d2 << "相差" << (d2 - d1) << "天" << endl;printf("\n\n\n");}else if (input == 2){Date d1;int day = 0;cout << "请输入某日日期:";cin >> d1;cout << "请输入往后多少天:";cin >> day;cout << d1 << "往后" << day << "天是" << (d1 + day) << endl;printf("\n\n\n");}else if (input == 3){Date d1;int day = 0;cout << "请输入某日日期:";cin >> d1;cout << "请输入往前多少天:";cin >> day;cout << d1 << "往前" << day << "天是" << (d1 - day) << endl;printf("\n\n\n");}else if (input == 0){break;}else{cout << "请输入正确的选项" << endl;printf("\n\n\n");}} while (input);
}


有需要自取 gitee 链接  :日期计算器——有趣的中国人的gitee

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

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

相关文章

GPU算力池管理工具Determined AI部署与使用教程(2024.03)

1. 概念 1.1 什么是Determined&#xff1f; Determined AI 是一个全功能的深度学习平台&#xff0c;兼容 PyTorch 和 TensorFlow。它主要负责以下几个方面&#xff1a; 分布式训练&#xff1a;Determined AI 可以将训练工作负载分布在多个 GPU&#xff08;可能在多台计算机上…

鸿蒙预览报错 Only files in a module can be previewed

HarmonyOS第一课下载的源码无法运行&#xff0c;也无法预览&#xff0c;报错如题。 解决&#xff1a; 1、在预览页如“index.ets”文件下预览。 2、如果在通知栏看到如图提示&#xff0c;可看出是ohos/hvigor-ohos-plugin插件版本的问题&#xff0c;可点击蓝色解决方案同步并导…

python 函数(解包**、互相调用、作用域、函数的封装、内置函数:eval()、zip()、文件处理open())

函数解包 """ 1、函数的注释&#xff1a;参数和返回值 在注释里可以自动添加显示&#xff0c;只需手动加说明。2、函数的解包【拆包】&#xff1a;函数的参数要传递数据有多个值的时候&#xff0c;中间步骤拿到数据 保存在元组或者列表 或者字典里。 - 传递参数…

活用 C语言之union的精妙之用

一、union的基本定义 Union的中文叫法又被称为共用体、联合或者联合体。它的定义方式与结构体相同,但意义却与结构体完全不同。下面是union的定义格式: union 共用体名 {成员列表}共用体变量名;它与结构体的定义方式相同,但区别在于共用体中的成员的起始地址都是相同的,…

【理解机器学习算法】之Clustering算法(DBSCAN)

DBSCAN&#xff08;基于密度的空间聚类应用噪声&#xff09;是数据挖掘和机器学习中一个流行的聚类算法。与K-Means这样的划分方法不同&#xff0c;DBSCAN特别擅长于识别数据集中各种形状和大小的聚类&#xff0c;包括存在噪声和离群点的情况。 以下是DBSCAN工作原理的概述&am…

KubeSphere的基本使用操作

KubeSphere的基本使用操作 基本使用用户角色创建企业空间创建项目 创建应用创建密钥创建MySQL密钥创建WordPress密钥 创建存储卷创建MySQL存储卷创建Wordpress存储卷 添加组件服务类型添加MySQL组件添加WordPress组件 访问Wordpress 基本使用 用户角色 KubeSphere 中的权限控制…

FloodFill算法——岛屿数量

文章目录 题目解析算法解析代码解析 题目解析 岛屿数量 题目依旧是熟悉的配方&#xff0c;熟悉的味道&#xff0c;还是那个0还是那个1还是那个二维矩阵&#xff0c;这时候BFS和DFS闻着味就来了&#xff0c;我们来看一下这个题目&#xff0c;这个题目也很容易理解如下图有一个…

【每日一问】IOS手机上Charles证书过期怎么办?

1、如何查看证书是否过期? 设置>通用>VPN与设备管理 2、在Charles中重置证书 步骤1&#xff1a;重置证书 Help>SSL Proxying>Reset Charles Root Certificate… 步骤2&#xff1a;在浏览器中&#xff0c;下载证书 首先&#xff0c;手机连上代理&#xff0c;然…

qt+ffmpeg 实现音视频播放(三)之视频播放

一、视频播放流程 &#xff08;PS&#xff1a;视频的播放流程跟音频的及其相似&#xff01;&#xff01;&#xff09; 1、打开视频文件 通过 avformat_open_input() 打开媒体文件并分配和初始化 AVFormatContext 结构体。 函数原型如下&#xff1a; int avformat_open_inpu…

Java项目:71 ssm基于ssm+vue的外卖点餐系统+vue

作者主页&#xff1a;舒克日记 简介&#xff1a;Java领域优质创作者、Java项目、学习资料、技术互助 文中获取源码 项目介绍 系统功能 系统分为前台订餐和后台管理&#xff1a; 1.前台订餐 用户注册、用户登录、我的购物车、我的订单、商品列表 2.后台管理 商品管理&#xf…

QT tableWidget横向纵向设置

横向控件 要设置QTabWidget选项卡的字体方向&#xff0c;可以使用QTabWidget的setTabPosition()方法。通过传递Qt枚举值QTabWidget.east或QTabWidget.west作为参数&#xff0c;可以设置选项卡的字体方向为从左到右或从右到左。 myTabWidget QTabWidget() myTabWidget.setTabP…

Grass手机注册使用教程,利用闲置手机WiFi带宽赚钱

文章目录 Grass是什么&#xff1f; 项目介绍Grasss手机使用步骤第一步&#xff1a;下载狐猴浏览器第二步&#xff1a;注册账户&#xff08;已注册直接跳过&#xff09;第三步&#xff1a;安装Grass Chrome插件1、推荐离线安装2、在线安装 第四步&#xff1a;登录第五步&#xf…

MySQL 更新执行的过程

优质博文&#xff1a;IT-BLOG-CN Select语句的执行过程会经过连接器、分析器、优化器、执行器、存储引擎&#xff0c;同样的 Update语句也会同样走一遍 Select语句的执行过程。 但是和 Select最大不同的是&#xff0c;Update语句会涉及到两个日志的操作redo log&#xff08;重做…

欧科云链OKLink:坎昆升级后,Layer2项目是否更具竞争力?

在坎昆升级激活之际&#xff0c;OKLink 上线以太坊坎昆升级 Dencun 专题页 &#x1f449; 从专业链上数据分析角度&#xff0c;带来一场充实且即时的 Layer2 数据盛宴。 在近日由 137Labs 发起&#xff0c;Cointime 主持的 Layer2 生态专场讨论中&#xff0c;OKLink 产品…

InnoDB 缓存

本文主要聊InnoDB内存结构, 先来看下官网Mysql 8.0 InnoDB架构图 MySQL :: MySQL 8.0 Reference Manual :: 17.4 InnoDB Architecture 如上图所示,InnoDB内存主要包含Buffer Pool, Change Buffer, Log Buffer, Adaptive Hash Index Buffer Pool 其实 buffer pool 就是内存中的…

练习实践-进程回收01-找到并清理僵尸进程

参考来源&#xff1a; https://blog.csdn.net/qq_36528114/article/details/71076110 https://blog.51cto.com/u_12083623/2363384 极客时间-性能优化实战-CPU性能篇 进程回收中的孤儿和僵尸进程的特点 演示环境&#xff1a; 操作系统&#xff1a;Ubuntu18.04 查询工具&#x…

Golang案例开发之gopacket抓包三次握手四次分手(3)

文章目录 前言一、理论知识三次握手四次分手二、代码实践1.模拟客户端和服务器端2.三次握手代码3.四次分手代码验证代码完整代码总结前言 TCP通讯的三次握手和四次分手,有很多文章都在介绍了,当我们了解了gopacket这个工具的时候,我们当然是用代码实践一下,我们的理论。本…

Java 基础学习(二十)Maven、XML与WebServer

1 Maven 1.1 什么是Maven 1.1.1 Maven概述 Maven是一种流行的构建工具&#xff0c;用于管理Java项目的构建&#xff0c;依赖管理和项目信息管理。它使用XML文件来定义项目结构和构建步骤&#xff0c;并使用插件来执行各种构建任务。Maven可以自动下载项目依赖项并管理它们的…

PMSM 永磁同步电机滑膜控制 SVPWM矢量控制 matlab simulink 仿真

仿真搭建平台&#xff1a; (1)该模型采用matlab/simulink 2016b版本搭建&#xff0c;使用matlab 2016b及以上版本打开最佳; (2)该模型已经提前转换了各个常用版本&#xff08;最低为matlab2012b&#xff09;&#xff0c;防止出现提示版本过高的情况。 模型截图&#xff1a; 算…

1、goreplay流量回放

目的 在实际项目中&#xff0c;会有大量的回归测试工作&#xff0c;通常会使用自动化代码的手段来实现回归&#xff0c;但是对于一个庞大的系统来说&#xff0c;通过自动化脚本的方式来实现回归测试&#xff0c;又显得很费时费力。并且如果有定期将线上数据同步到测试环境的需求…