用C++實現日期類

在上學的時候,總是在計算還有多少天放假;在上班的時候,總是在計算還有多久發工資?我們一般通過日歷得到結果,那自己能不能實現一些基本的功能呢?答案是可以的!

需要實現內容:

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…