在上学的时候,总是在计算还有多少天放假;在上班的时候,总是在计算还有多久发工资?我们一般通过日历得到结果,那自己能不能实现一些基本的功能呢?答案是可以的!

需要实现内容:

1. 日期加减天数

//d1+=50
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month > 12){_year++;_month = 1;//_day -= GetMonthDay(_year, _month); // error - _month已经发生改变}}return *this;
}//d1+50(不会改变d1)
Date Date::operator+(int day)
{//创建临时对象,改变贫道不改变己身Date tmp(*this);tmp += day;return tmp;
}// ++d1
Date& Date::operator++()
{*this += 1;return *this;
}
// d1++
Date Date::operator++(int x)
{Date tmp(*this);*this += 1;return tmp;
}

// d1-=50
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){_month--;if (_month <= 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);;}return *this;
}
// d1-50
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}// --d1
Date& Date::operator--()
{*this -= 1;return *this;
}
// d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}

2. 日期减日期

int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;int count = 0;if (*this < d){max = d;min = *this;flag = -1;}int day1 = max._day;int day2 = min._day;int n = max._year - min._year;if (max._year == min._year){n = 0;}// 2025 1 31while (max._month != 1 || max._day != 1){max._month--;if (max._month <= 0){max._month = 12;max._year--;break;}max._day = GetMonthDay(max._year, max._month);day1 += max._day;}//2024 1 1while (min._month != 1 || min._day != 1){min._month--;if (min._month <= 0){min._month = 12;min._year--;break;}min._day = GetMonthDay(min._year, min._month);day2 += min._day;}int x = min._year;while (x < max._year){count += Getleapyear(min._year + 1);x++;}return day1 - day2 + 365 * n + count+1;
}

第二种就是直接暴力计算,如下:

int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int count = 0;while (min != max){min++;count++;}return count * flag;
}

3. 完整代码

Date.h 文件

#pragma once
#include <iostream>
using namespace std;
#include <assert.h>class Date
{
public:Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}int GetMonthDay(int year, int month){assert(month > 0 && month < 13);// 为了避免每次调用都开辟空间,可以加static存放到全局static int arr[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;}elsereturn arr[month];}int Getleapyear(int year){if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 1;}elsereturn 0;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}void Print(){cout << _year << "年" << _month << "月" << _day << "日" << endl;}// 日期+=dayDate& operator+=(int day);Date operator+(int day)const;Date& operator-=(int day);Date operator-(int day)const;// ++d1Date& operator++();// d1++Date operator++(int);// --d1Date& operator--();// d1--Date operator--(int);int 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;bool operator<=(const Date& d)const;~Date(){}
private:int _year;int _month;int _day;
};

Date.cpp 文件

#include "Date.h"//d1+=50
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month > 12){_year++;_month = 1;//_day -= GetMonthDay(_year, _month); // error - _month已经发生改变}}return *this;
}//d1+50(不会改变d1)
Date Date::operator+(int day) const
{//创建临时对象,改变贫道不改变己身Date tmp(*this);tmp += day;return tmp;
}// d1-=50
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){_month--;if (_month <= 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);;}return *this;
}
//// d1-50
//Date Date::operator-(int day) const
//{
//	Date tmp(*this);
//	tmp._day -= day;
//	while (tmp._day <= 0)
//	{
//		tmp._month--;
//		if (tmp._month <= 0)
//		{
//			tmp._year--;
//			tmp._month = 12;
//		}
//		tmp._day += GetMonthDay(tmp._year, tmp._month);//error -> 这里传的是const Date* this,权限放大
//	}
//	return tmp;
//}// d1-50
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}// d2 = ++d1
Date& Date::operator++()
{*this += 1;return *this;
}
// d2 = d1++
Date Date::operator++(int x)
{Date tmp(*this);*this += 1;return tmp;
}// --d1
Date& Date::operator--()
{*this -= 1;return *this;
}
// d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}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);
}bool Date::operator>(const Date& d) const
{if (_year > d._year){return true;}else if (_year == d._year && _month > d._month){return true;}else if (_year == d._year && _month == d._month && _day > d._day){return true;}elsereturn 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);
}int Date::operator-(const Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int count = 0;while (min != max){min++;count++;}return count * flag;
}//int Date::operator-(const Date& d)
//{
//	Date max = *this;
//	Date min = d;
//	int flag = 1;
//	int count = 0;
//	if (*this < d)
//	{
//		max = d;
//		min = *this;
//		flag = -1;
//	}
//	int day1 = max._day;
//	int day2 = min._day;
//	int n = max._year - min._year;
//	if (max._year == min._year)
//	{
//		n = 0;
//	}
//
//	// 2025 1 31
//	while (max._month != 1 || max._day != 1)
//	{
//		max._month--;
//		if (max._month <= 0)
//		{
//			max._month = 12;
//			max._year--;
//			break;
//		}
//		max._day = GetMonthDay(max._year, max._month);
//		day1 += max._day;
//	}
//	//2024 1 1
//	while (min._month != 1 || min._day != 1)
//	{
//		min._month--;
//		if (min._month <= 0)
//		{
//			min._month = 12;
//			min._year--;
//			break;
//		}
//		min._day = GetMonthDay(min._year, min._month);
//		day2 += min._day;
//	}
//	int x = min._year;
//	while (x < max._year)
//	{
//		count += Getleapyear(min._year + 1);
//		x++;
//	}
//
//	return day1 - day2 + 365 * n + count+1;
//}

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

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

相关文章

百度网盘基于Flink的实时计算实践

01 概览 随着数字化转型的来临&#xff0c;企业对于数据服务的实时化需求日益增长&#xff0c;在大规模数据和复杂场景的情况下&#xff0c;Flink在实时计算数据链路中扮演着极为重要的角色&#xff0c;本文介绍了网盘如何通过 Flink 构建实时计算引擎&#xff0c;从而提供高性…

【CMake】策略

目录 一.CMake策略简要理解 1.1.第一阶段&#xff1a;童年时期&#xff08;旧行为&#xff0c;The "Old Way"&#xff09; 1.2.第二阶段&#xff1a;成长与改进&#xff08;引入新行为&#xff0c;The "New Way"&#xff09; 1.3.第三阶段&#xff1a;…

LLM中的function call

1. 概念 **Function Call&#xff08;函数调用&#xff09;**是指在编程中&#xff0c;程序可以通过调用预定义的函数来执行特定的操作。在LLM中&#xff0c;函数调用的概念扩展了模型的能力&#xff0c;使其不仅能够生成文本&#xff0c;还能与外部系统进行交互。通过函数调用…

【系统架构设计(13)】项目管理上:盈亏平衡分析与进度管理

文章目录零、核心思想&#xff1a;经济性与时效性的动态平衡一、盈亏平衡分析&#xff1a;项目的经济生命线1、核心公式与决策逻辑二、进度管理&#xff1a;项目的时效生命线1. **工作分解结构&#xff08;WBS&#xff09;**2. 进度管理流程3、关键路径法关键路径法&#xff08…

【SuperSocket 】利用 TaskCompletionSource 在 SuperSocket 中实现跨模块异步处理客户端消息

利用 TaskCompletionSource 在 SuperSocket 中实现跨模块异步处理客户端消息 在使用 SuperSocket 构建 TCP 服务时&#xff0c;我们经常会遇到这样的需求&#xff1a; 服务端接收到客户端数据后&#xff0c;需要将数据交给其他模块处理处理完成后再将结果返回给调用模块或客户端…

《IC验证必看|semaphore与mailbox的核心区别》

月薪30K验证工程师必答&#xff1a;SystemVerilog中semaphore与mailbox的核心区别&#xff0c;及必须用semaphore的场景深度解析 在验证工程师的技能体系里&#xff0c;线程同步与资源管控是区分“基础会用”&#xff08;20K水平&#xff09;和“精通工程化”&#xff08;30K水…

Spring线程池ThreadPoolTaskExecutor‌详解

ThreadPoolTaskExecutor‌写法Bean(name "taskExecutor") public ThreadPoolTaskExecutor taskExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(8); // 8核CPU服务器建议值executor.setMaxPoolSize(…

Unity之安装教学

UnityHub下载 下载官网地址&#xff1a;Unity Hub下载地址 打开网址右上角&#xff0c;登录/注册账号 登录完毕后&#xff0c;点击下载 安装Unity Hub 双击傻瓜式安装 安装完成 启动UnityHub 双击启动 左上角设置 设置中文 左上角登录账号 添加免费许可证 设置-许可证-添加 安装…

Redis 集群模式与高可用机制

最近在准备面试&#xff0c;正把平时积累的笔记、项目中遇到的问题与解决方案、对核心原理的理解&#xff0c;以及高频业务场景的应对策略系统梳理一遍&#xff0c;既能加深记忆&#xff0c;也能让知识体系更扎实&#xff0c;供大家参考&#xff0c;欢迎讨论。在分布式环境下&a…

Flutter + Web:深度解析双向通信的混合应用开发实践

Flutter Web&#xff1a;深度解析双向通信的混合应用开发实践 前言 在当今快速发展的移动应用开发领域&#xff0c;开发者们始终在寻求一种能够平衡开发效率、跨平台能力和用户体验的完美方案。原生开发性能卓越&#xff0c;但双平台&#xff08;iOS/Android&#xff09;开发…

如何查看Linux系统中文件夹或文件的大小

在日常运维和开发工作中&#xff0c;了解文件夹和文件占用的磁盘空间是非常重要的。尤其是当你在服务器上部署应用&#xff08;如 Jenkins&#xff09;时&#xff0c;合理监控磁盘使用情况可以避免磁盘空间不足导致的各种问题。在 Linux 系统中&#xff0c;我们可以使用一些简单…

豪华酒店品牌自营APP差异对比分析到产品重构

一、万豪国际集团旗下豪华酒店品牌及统一APP 万豪旗下奢华品牌均整合于 「万豪旅享家(Marriott Bonvoy)」APP,会员可通过该平台预订、管理积分及享受跨品牌服务。以下为核心豪华品牌: 1. 经典奢华品牌 丽思卡尔顿酒店(The Ritz-Carlton) 定位:顶级奢华,以管家服务、历…

ESLint 相关

no-unused-vars 等常见报错提醒关闭 1. no-unused-vars 报错示例&#xff1a; useMemo is defined but never used no-unused-vars解决方式 方法一&#xff1a;局部禁用某一行 // eslint-disable-next-line no-unused-vars const result useMemo(() > {}, []);方法二&…

1分钟生成爆款相声对话视频!Coze智能体工作流详细搭建教程,小白也能轻松上手

最近看到一个账号&#xff0c;用AI将传统相声对话做成趣味短视频&#xff0c;单条播放量轻松破百万。这种视 频看似复杂&#xff0c;其实用Coze智能体工作流1分钟就能搞定&#xff0c;完全不需要剪辑基础。工作流功能 用Coze一键生成爆款相声对话视频&#xff0c;无需剪辑直接发…

pinia状态管理工具

pinia状态管理工具Pinia 是 Vue.js 官方推荐的新一代状态管理库&#xff0c;可以看作是 Vuex 的替代品。1. 什么是 Pinia&#xff1f; Pinia 是 Vue 的专属状态管理库&#xff0c;它允许你跨组件或页面共享状态。由 Vue.js 核心团队维护&#xff0c;并且对 TypeScript 有着极其…

【初始web3】什么是web3

前言你是否还记得&#xff0c;曾经在社交媒体上发布精彩内容&#xff0c;平台却随意封禁你的账号&#xff1f;你是否曾疑惑&#xff0c;为什么你创造的数据价值亿万&#xff0c;而你自己却一无所获&#xff1f;这&#xff0c;就是Web2时代的痛。而Web3的到来&#xff0c;正试图…

构建下一代互联网:解码Web3、区块链、协议与云计算的协同演进

我们正站在互联网历史性变革的门口。从只能读取信息的Web1&#xff0c;到可以读写、高度中心化的Web2&#xff0c;我们即将迈入一个价值可以直接传递的Web3时代。这个新时代并非由单一技术驱动&#xff0c;而是由区块链、去中心化协议和云计算等一系列技术的融合与协同所构建。…

小迪安全v2023学习笔记(七十六讲)—— Fuzz模糊测试口令爆破目录爆破参数爆破Payload爆破

文章目录前记WEB攻防——第七十六天Fuzz模糊测试篇&JS算法口令&隐藏参数&盲Payload&未知文件目录Fuzz知识含义Fuzz的核心思想Fuzz应用场景Fuzz应用Fuzz字典项目Fuzz技术 - 用户口令-常规&模块&JS插件常规模块JS插件JsEncrypterBurpCryptoFuzz技术 - 目…

在windows server 2022搭建gitlab……但是失败了

在windows server 2022搭建gitlab……但是失败了1. 前言2. 安装ubuntu环境2. 安装docker3. 映射3.1 端口映射3.2 路径映射1. 前言 上一篇&#xff1a;在windows本地机搭建gitlab 本来按理来说没必要另起一篇&#xff0c;但是没想到&#xff0c;在新机器的windows server 2022…

蓝桥杯算法之基础知识(4)

目录 Ⅰ.sorted排序 Ⅱ.排序具体的方法 &#xff08;1&#xff09;sort的神方法&#xff08;注意是sort&#xff09; &#xff08;2&#xff09;sorted的神方法&#xff08;注意这里是sorted&#xff09; 常见场景 1. 单关键字排序 2. 多关键字排序 3.按倒序字符串排序&#xf…