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,一经查实,立即删除!

相关文章

单例模式:双重效验锁的懒汉实现方式

单例模式&#xff1a;双重效验锁的懒汉实现方式 单例模式饿汉模式实现懒汉模式实现改进1 (创建多个实例)改进2 (性能比较低)改进3 (volatile禁止指令重排序) 单例模式 单例模式顾名思义就是指实例化一个对象&#xff0c;通过把构造方法私有化来禁止创建实例。 饿汉模式实现 饿汉…

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

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

大模型开发中使用prompt提示最佳实践

在大型模型开发中&#xff0c;使用prompt&#xff08;提示&#xff09;是一种指导模型生成所需输出的方法。以下是在使用prompt时的一些最佳实践&#xff1a; 1、明确的提示&#xff1a;确保prompt提供了明确、清晰的指导&#xff0c;以便模型理解所需生成的内容。避免模棱两可…

IPP-7010 表面贴装 90 度混合耦合器

IPP-7010 表面贴装 90 度混合耦合器 IPP-7010 是一款表面贴装 90 度混合耦合器&#xff0c;工作频率为 800 至 2500 MHz&#xff08;0.8 至 2.5 GHz&#xff09;&#xff0c;平均额定功率为 200 瓦。IPP-7010 采用 0.40 x 1.80 英寸表面贴装封装。IPP-7010的幅度平衡小于0.6dB&…

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

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

HTTP Header Fields

HTTP&#xff08;超文本传输协议&#xff09;中包含多种类型的头部字段&#xff08;Header Fields&#xff09;&#xff0c;以下是常见的HTTP头部字段及其作用&#xff1a; ### 通用头字段&#xff08;General Header Fields&#xff09; - **Cache-Control**: 控制缓存行为&a…

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…

centos7 安装php82

安装epel扩展源 yum -y install epel-release vim 镜像地址: https://mirrors.aliyun.com/remi/ #这个阿里源专门提供了php的各种版本 下载镜像&#xff08;如果epel扩展源包含php8.2版本&#xff0c;可不需要下载此镜像。建议下载&#xff0c;百利无一害&#xff09; …

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…

Sphinx使用md文档构建失败

​使用 sphinx 构建文档&#xff0c;有时候已经放置好了文档却无法读入生成。 解决方案 在环境中下载安装好相应的库: pip install sphinx_markdown_tablespip install m2r打开生成的 source目录下的 conf.py&#xff0c;文件&#xff0c;在文件中加入&#xff1a; extensio…

19.C++20中的std::latch和std::barrier

文章目录 线程闩std::latch和线程卡std::barrier线程闩std::latch线程卡std::barrier的使用线程闩std::latch和线程卡std::barrier的区别reference 欢迎访问个人网络日志&#x1f339;&#x1f339;知行空间&#x1f339;&#x1f339; 线程闩std::latch和线程卡std::barrier …

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;重做…