實現日期類

日期類的實現主要是去學習使用operator
日期類就是計算日期之間的天數,日期與(日期,天數)的相加減
比如日常生活中我們可以計算日期加天數,日期減天數,日期減日期,
沒有日期加日期的說法
在這里插入圖片描述

日期類的實現

  • 1.日期的比較
  • 2.日期的計算
    • 日期的加法
    • 日期的減法
    • 前置后置++,與- -
  • 3.日期的輸入輸出
  • 日期類完整代碼
    • Date.h
    • Date.cpp

1.日期的比較

日期的比較,當寫出大于和等于兩個函數的時候,其他的比較函數就都可以復用這兩個函數了

//先寫大于的
//一定是先比較年,再比較月,再比較日
bool Date::operator>(const Date& d)
{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;}return false;
}bool Date::operator==(const Date& d)
{return _year == d._year && _month == d._month && _day == d._day;
}

剩下的復用這兩個函數就行了

bool Date::operator>=(const Date& d)
{return *this > d || *this == d;
}bool Date::operator<(const Date& d)
{return !(*this >= d);
}bool Date::operator<=(const Date& d)
{return *this < d || *this == d;
} bool Date::operator!=(const Date& d)
{return !(*this == d);
}

2.日期的計算

日期的加法

日期的加減
日期的加天數我們要考慮到是否會進入到下一個月,下一年,當前年是否是閏年,二月有幾天,所以我們還應該有一個計算當前月天數的函數
我們還要考慮代碼運行時的效率,返回值可以用引用就用引用,可以減少調用拷貝構造的次數,+=操作返回的*this在函數結束后沒有被銷毀,所以可以返回引用

int GetMonthDay(int year,int month)
{assert(month > 0 && month < 13);//這個函數會經常調用所以我們可以把數組定義為靜態類形static int MonthDayArr[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;}else{return MonthDayArr[month];}
}//先寫+=,日期只有加天數
//沒有日期加日期,例如2024.5.13+2024.5.1這樣的寫法沒有意義
Date& Date::operate+=(int day)
{_day += day;while(_day > GetMonthDay(_year,_month)){_day -= GetMonthDay(_year,_month);++_month;if(_month == 13){_month = 1;++_year;}}return *this;
}
//+=可以復用+
Date Date::operator+(int day)
{Date tmp = *this;tmp += day;return tmp;
}

日期的減法

日期的減法有兩種一種是日期減天數,一種是日期減日期
第一種需要注意的是如果天數day是負數的情況,我們要加的是上個月的天數

Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){if (_month == 1){--_year;_month = 12;}else{--_month;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}//日期減日期----日期減日期計算的是他們之間相差多少天
int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if(max < min){max = d;min = *this;flag = -1;}int n  =  0;while(max != min){min++;n++;}return n;
}      

前置后置++,與- -

這個不難寫,但要注意的一點四是如何區分前置與后置,c++規定后置++,- -的形參列表要有一個int類型


Date& Date::operator++()
{return *this += 1;
}Date Date::operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}Date& Date::operator--() 
{return *this -= 1;
}Date Date::operator--(int)
{Date tmp = *this;*this += 1;return tmp;
}

3.日期的輸入輸出

首先這個>>,<<不能寫為成員函數,因為我們調用時要寫成,對象.成員函數,所以我們寫在類外面,然后用友員函數在類中聲明

ostream& operator<<(ostream& out, const Date& d)
{cout << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{cout << "請依次輸入年月日";in >> d._year >> d._month >> d._day;return in;
}

日期類完整代碼

Date.h

#pragma once#include<iostream>
#include<assert.h>
using namespace std;class Date
{friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);public:int GetMonthDay(int year, int month){assert(month > 0 && month < 13);static int Month[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;}return Month[month];}//構造函數Date(int year = 1, int month = 1, int day = 1);void Print() 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& 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-(Date& d) const;private:int _year = 1;int _month = 1;int _day = 1;
};ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

Date.cpp

#define _CRT_SECURE_NO_WARNINGS#include"Date.h"//構造函數
Date::Date(int year,int month, int day)
{if (month > 0 && month < 13&& day > 0 && day <= GetMonthDay(year, month)){_year = year;_month = month;_day = day;}else{cout << "非法日期" << endl;assert(false);}
}void Date::Print() const
{cout << _year << " " << _month << " " << _day << endl;
}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 _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
{return *this < d || *this == d;
} bool Date::operator!=(const Date& d) const
{return !(*this == d);
}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 Date::operator+(int day) const
{Date tmp = *this;tmp += day;return tmp;
}Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){if (_month == 1){--_year;_month = 12;}else{--_month;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day) const
{Date tmp = *this;tmp -= day;return tmp;
}Date& Date::operator++()
{return *this += 1;
}Date Date::operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}Date& Date::operator--() 
{return *this -= 1;
}Date Date::operator--(int)
{Date tmp = *this;*this += 1;return tmp;
}int Date::operator-(Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (max < min){max = d;min = *this;flag = -1;}int n = 0;while (max > min){min++;n++;}return n * flag;
}ostream& operator<<(ostream& out, const Date& d)
{cout << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{cout << "請依次輸入年月日";in >> d._year >> d._month >> d._day;return in;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/11662.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/11662.shtml
英文地址,請注明出處:http://en.pswp.cn/web/11662.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

M-有效算法

在賽場上&#xff0c;腦子就兩個字“二分”&#xff0c;一點思路都沒&#xff0c;完全不知道二分誰&#xff0c;怎么二分&#xff0c;從哪入手。隱隱約約也知道要變換公式&#xff0c;可惜沒堅持這個想法。腦子里全是把k分離出來&#xff0c;賽后看了題解才知道&#xff0c;應該…

LeetCode 力扣題目:買賣股票的最佳時機 IV

?????? 歡迎來到我的博客。希望您能在這里找到既有價值又有趣的內容&#xff0c;和我一起探索、學習和成長。歡迎評論區暢所欲言、享受知識的樂趣&#xff01; 推薦&#xff1a;數據分析螺絲釘的首頁 格物致知 終身學習 期待您的關注 導航&#xff1a; LeetCode解鎖100…

MQTT學習(二)

訂閱主題和訂閱確認 SUBSCRIBE——訂閱主題 之前的CONNECT報文&#xff0c;分為 固定報頭&#xff1a;必須存在&#xff0c;用于描述報文信息。里面有指出什么類型的報文&#xff0c;報文的等級。可變報頭&#xff1a;不一定存在。主要看什么樣子類型的報文。有效載荷部分&a…

LoRA Land: 310個經微調的大語言模型可媲美GPT-4

摘要 低秩自適應 (LoRA) 已成為大語言模型 (LLM) 參數有效微調 (PEFT) 中最廣泛采用的方法之一。LoRA 減少了可訓練參數的數量和內存使用,同時達到了與全面微調相當的性能。該研究旨在評估在實際應用中訓練和服務使用 LoRA 微調的 LLM 的可行性。首先,該研究測量了在 10 個基礎…

js基礎-數組-事件對象-日期-本地存儲

一、大綱 一、獲取元素位置 在JavaScript中&#xff0c;獲取一個元素在頁面上的位置可以通過多種方法實現。以下是一些常見的方法&#xff1a; getBoundingClientRect() getBoundingClientRect() 方法返回元素的大小及其相對于視口的位置。它提供了元素的left、top、right和bo…

Vue的學習 —— <vue響應式基礎>

目錄 前言 正文 單文件組件 什么是單文件組件 單文件組件使用方法 數據綁定 什么是數據綁定 數據綁定的使用方法 響應式數據綁定 響應式數據綁定的使用方法 ref() 函數 reactive()函數 toRef()函數 toRefs()函數 案例練習 前言 Vue.js 以其高效的數據綁定和視圖…

探索大語言模型代理(Agent):研究背景、通用框架與未來展望

引言 近年來&#xff0c;隨著人工智能技術的飛速發展&#xff0c;大語言模型&#xff08;Large Language Models, LLMs&#xff09;在智能代理&#xff08;Agent&#xff09;領域中的應用已成為研究的熱點。這些代理不僅能夠模擬人類的認知過程&#xff0c;還能在復雜的社會環…

CNN/TCN/LSTM/BiGRU-Attention到底哪個模型效果最好?注意力機制全家桶來啦!

? 聲明&#xff1a;文章是從本人公眾號中復制而來&#xff0c;因此&#xff0c;想最新最快了解各類智能優化算法及其改進的朋友&#xff0c;可關注我的公眾號&#xff1a;強盛機器學習&#xff0c;不定期會有很多免費代碼分享~ 目錄 數據介紹 效果展示 原理簡介 代…

數字人解決方案——AniTalker聲音驅動肖像生成生動多樣的頭部說話視頻算法解析

1.概述 AniTalker是一款先進的AI驅動的動畫生成工具&#xff0c;它超越了簡單的嘴唇同步技術&#xff0c;能夠精準捕捉并再現人物的面部表情、頭部動作以及其他非言語的微妙動態。這不僅意味著AniTalker能夠生成嘴型精準同步的視頻&#xff0c;更重要的是&#xff0c;它還能夠…

使用Dockerfile配置Springboot應用服務發布Docker鏡像-16

創建Docker鏡像 springboot-docker模塊 這個應用可以隨便找一個即可&#xff0c;這里不做詳細描述了。 pom.xml 依賴版本可參考 springbootSeries 模塊中pom.xml文件中的版本定義 <dependencies><dependency><groupId>com.alibaba.cloud</groupId>…

linux開機啟動配置文件

在Linux系統中&#xff0c;開機啟動配置文件通常位于/etc/init.d目錄下&#xff0c;并且是一個腳本文件&#xff0c;該腳本可以通過service命令或systemctl命令來啟動、停止、重啟服務。 1、創建一個服務腳本 /etc/init.d/ruoyi.sh #!/bin/bashCURRENT_PATH$(pwd) JAR_NAME&q…

企業開發基礎-JDBC(SQL注入)

JDBC概論 1、JDBC是什么&#xff1f; Java DataBase Connectivity&#xff08;Java語言連接數據庫&#xff09; 2、JDBC的本質是什么&#xff1f; JDBC是SUN公司制定的一套接口&#xff08;interface&#xff09; java.sql.*; (這個軟件包下有很多接口。) 接…

[數據集][圖像分類]雜草分類數據集17509張9類別

數據集格式&#xff1a;僅僅包含jpg圖片&#xff0c;每個類別文件夾下面存放著對應圖片 圖片數量(jpg文件個數)&#xff1a;17509 分類類別數&#xff1a;9 類別名稱:["chineseapple","lantana","negatives","parkinsonia","part…

48-Qt控件詳解:Buttons Containers2

一 Group Box:組合框 #include "widget.h"#include<QGroupBox> #include<QRadioButton> #include<QPushButton> #include<QVBoxLayout>//可以在水平方向和垂直方向進行排列的控件&#xff0c;QHBoxLayout/QVBoxLayout #include <QGridLa…

vue2 el-tree樹形下拉框

由于element-vue2 中沒有el-tree-select組件&#xff0c;所以樹形下拉需要結合el-selet完成 <el-form-item label"上級部門&#xff1a;" prop"pidName"> <el-select ref"select" v-model"dialogForm.pidName" placeholder&…

Backend - 數據分析 Numpy

目錄 一、作用 二、基礎環境 &#xff08;一&#xff09;執行虛擬環境的終端命令 &#xff08;二&#xff09;代碼中導包 三、數組操作 &#xff08;一&#xff09;創建數組 1. 創建一維數組 &#xff08;1&#xff09;基本建立 &#xff08;2&#xff09;建立后&…

揚州知識付費系統招聘,你知道在線教育平臺推廣有什么技巧?

在線教育的模式有各種各樣&#xff0c;不管是哪種模式&#xff0c;在線教育的課程都有顛覆和創新性。互聯網在線教育課程可以要大家在家就可以利用碎片化時間學習&#xff0c;那在線教育平臺怎么推廣呢&#xff1f; 1、與校園和企業合作 在線教育平臺不僅能給校園的老師提供更好…

解決寶塔Nginx和phpMyAdmin配置端口沖突問題

問題描述 在對基于寶塔面板的 Nginx 配置文件進行端口修改時&#xff0c;我注意到 phpMyAdmin 的端口配置似乎也隨之發生了變化&#xff01; 解決方法 官方建議在處理 Nginx 配置時&#xff0c;應避免直接修改默認的配置文件&#xff0c;以確保系統的穩定性和簡化后續的維護…

大數據可視化實驗三——數據可視化工具使用

目錄 一、實驗目的... 1 二、實驗環境... 1 三、實驗內容... 1 1. 下載并安裝Tableau軟件.. 1 2. 使用HTML5繪制Canvas圖形.. 2 3. 使用HTML5編寫SVG 圖形... 5 4. 使用R 語言編寫可視化實例.. 7 四、總結與心得體會... 7 五、思考問題... 8 一、實驗目的 1&#xff…

C++-Linux工程管理

1 Makefile和CMake實踐 1.1 Makefile 參考 簡介&#xff1a; Makefile是一種用于自動化構建和管理程序的工具。它通常用于編譯源代碼、鏈接對象文件以生成可執行文件或庫文件。Makefile以文本文件的形式存在&#xff0c;其中包含了一系列規則和指令&#xff0c;用于描述程序的…