淘寶網(wǎng)站頁面設(shè)計找廣告商的平臺
目錄
一、前言
?二、日期類計算器
?三、日期計算器的實現(xiàn)
🍎日期計算器各個接口的實現(xiàn)
🍐日期計算器的需求?
?🍉打印當(dāng)前日期(并檢查日期是否合理)
?💦檢查日期是否合理
?💦日期類構(gòu)造函數(shù)(用于日期的初始化)
??效果展示
?🍓日期之間的大小比較
?💦<??運算符重載
?💦== 運算符重載
?💦<= 運算符重載
?💦>??運算符重載
?💦>= 運算符重載
?💦!= 運算符重載
??效果展示
🍌日期的加減天數(shù)計算?
?💦日期 + 天數(shù)
?💦日期 + = 天數(shù)
💦?日期 -= 天數(shù)
💦日期 - 天數(shù)?
?💦日期 前置++
??💦日期 后置++
?💦日期 前置--
?💦日期 后置--
??效果展示
?🍊日期與日期之間的減法運算
?💦日期 - 日期
??效果展示
?四、日期計算器的完整代碼
🍇Date.h?
🍋Date.cpp?
🥝Test.cpp
?🍍代碼運行的界面
?五、共勉
一、前言
? ? ? ? 在之前的博客學(xué)習(xí)中,我們已經(jīng)詳細(xì)的講解了C++中的引用、缺省函數(shù)、this指針、構(gòu)造函數(shù)、析構(gòu)函數(shù)、拷貝構(gòu)造函數(shù)、運算符重載等非常重要的知識,但是對于這些知識如何如何的使用還沒有進(jìn)行講解,所以本次博客將以日期計算器為例,將以上知識融合起來講解,幫助大家更好的理解。
?二、日期類計算器
? ? ? ? 在我們的日常生活中,我們可能需要計算幾天后的日期,或計算日期差等,現(xiàn)如今計算日期的方式有很多,簡單粗暴的直接查看日歷,快捷點的直接使用日期計算器來求得,先給一個網(wǎng)絡(luò)上的日期計算器截圖:
? ? ? ? 現(xiàn)在,就讓我們用代碼來實現(xiàn)其工作原理吧。
?三、日期計算器的實現(xiàn)
🍎日期計算器各個接口的實現(xiàn)
?這里先建立三個文件:
1?? :Date.h文件,用于類中---函數(shù)聲明
2?? :Date.cpp文件,用于類中---函數(shù)的定義
3?? :Test.cpp文件,用于測試函數(shù)
建立三個文件的目的: 將日期計算器作為一個項目來進(jìn)行書寫,方便我們的學(xué)習(xí)與觀察。
🍐日期計算器的需求?
這里我們需要考慮,我們實現(xiàn)的這個日期計算器,需要實現(xiàn)怎樣的需求呢?
- 需要打印當(dāng)前的日期(請檢查日期是否合理)
- 實現(xiàn)日期之間的大小比較
- 實現(xiàn)日期的加減天數(shù)計算
- 實現(xiàn)日期與日期之間的減法運算
?🍉打印當(dāng)前日期(并檢查日期是否合理)
? ? ? ? 實現(xiàn)日期類首先就得檢查日期的合法性,這其中就包括大小月,閏年的2月有29天,一年只有12個月等等細(xì)節(jié)都要考慮到。
?💦檢查日期是否合理
//判斷是否為閏年 bool Date::isLeaveYear(int year) {// (四年一潤,百年不潤) 或者 四百年一潤return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }// 獲取每個月的天數(shù) int Date::GetMonthDay(int year, int month) {int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && isLeaveYear(year)){return 29;}else{return monthday[month];} }
?💦日期類構(gòu)造函數(shù)(用于日期的初始化)
//判斷是否為閏年 bool Date::isLeaveYear(int year) {// (四年一潤,百年不潤) 或者 四百年一潤return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }// 獲取每個月的天數(shù) int Date::GetMonthDay(int year, int month) {int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && isLeaveYear(year)){return 29;}else{return monthday[month];} }
??效果展示
1??:錯誤的日期演示? ?------? -1 / -1 / -1 (年/月/日)
// 測試初始化 void Test() {Date d1(-1,-1,-1);d1.Printf(); } int main() {Test();return 0; }
1??:正確的日期演示? --------? 2023 / 10 / 28 (年/月/日)// 測試初始化 void Test() {cout << "請輸入今日的日期" << endl;Date d1(2023,10,28);d1.Printf(); } int main() {Test();return 0; }
?🍓日期之間的大小比較
?💦<??運算符重載
- 思路:
< 運算符重載在我上一篇博文已經(jīng)詳細(xì)講解過,主要是先把大于的情況全部統(tǒng)計出來,就比如我要比較實例化對象d1是否小于實例化對象d2,只需考慮如下三種滿足的情況:
- d1的年小于d2的年
- d1與d2年相等,d1的月小于d2的月
- d1與d2年相等月相等,d1的天小于d2的天
這三種全是小于的情況,返回true,其余返回false
- 代碼如下:
// "<" 運算符重載 bool Date::operator<(const Date& d) {if (_year < d._year ||_year == d._year && _month < d._month ||_year == d._year && _month == d._month && _day < d._day){return true;}else{return false;} }
?💦== 運算符重載
- ?思路:
?== 運算符重載其實非常簡單,只需要判斷d1和d2的年、月、天是否對應(yīng)相等即可:
- 代碼如下:
// "==" 運算符重載 bool Date::operator==(const Date& d) {return _year == d._year && _month == d._month && _day == d._day; }
?💦<= 運算符重載
- 思路: --??復(fù)用 (這里復(fù)用的意思是:用之前寫好的運算符重載,可以減少我們的代碼量)
<= 的運算符重載,這里要仔細(xì)想一想 <= 成立的條件是啥。不就是?要么 < 要么 =?嗎?我們只需要復(fù)用先前寫的?< 運算符重載和?<=運算符重載,無需自己費老大勁推導(dǎo)其內(nèi)部原理。
- 代碼如下:
// "<=" 運算符重載 bool Date::operator<=(const Date& d) {// 運算符重載的復(fù)用return *this < d || *this == d; }
?💦>??運算符重載
- 思路: --??復(fù)用
> 的反義就是 <=,所以我們只需要復(fù)用?<= 運算符重載,再對其取反即可解決此問題。
- 代碼如下:
// ">" 運算符重載 bool Date::operator>(const Date& d) {return !(*this <= d); }
?💦>= 運算符重載
- 思路: --??復(fù)用
>= 的反義就是 <,所以我們只需要復(fù)用?< 運算符重載,再對其取反即可。
- 代碼如下:
// ">="運算符重載、 bool Date::operator>=(const Date& d) {return !(*this < d); }
?💦!= 運算符重載
- 思路: --??復(fù)用
有了前面的基礎(chǔ),寫個 != 也很簡單,對 == 取反即可
- 代碼如下:
//"!="運算符重載 bool Date::operator!=(const Date& d) {return !(*this == d); }
??效果展示
此時我們拿? ?"<"運算符? ?舉例子
// 測試"<"運算符 void Test1() {Date d1(2023,10,28);cout << "d1的日期為:";d1.Printf();cout << "d2的日期為:";Date d2(2023, 10, 27);d2.Printf();cout << endl;if (d2 < d1){cout << "d1的日期 < d2的日期" << endl;}else{cout << "d2的日期 > d1的日期" << endl;} }int main() {Test1();return 0; }
🍌日期的加減天數(shù)計算?
?💦日期 + 天數(shù)
- 思路:
對于日期 + 天數(shù),我們得到的還是一個日期。特別需要注意進(jìn)位的問題(天滿了往月進(jìn),月滿了往年進(jìn)),主要考慮如下幾個特殊點:
- 加過的天數(shù)超過該月的最大天數(shù),需要進(jìn)位
- 當(dāng)月進(jìn)位到13時,年進(jìn)位+1,月置為1
- 法一:
Date Date::operator+(int day) {Date ret(*this); //拷貝構(gòu)造,拿d1去初始化retret._day += day;while (ret._day > GetMonthDay(ret._year, ret._month)){ret._day -= GetMonthDay(ret._year, ret._month);ret._month++;if (ret._month == 13){ret._year++;ret._month = 1;}}return ret; }
? ? ? ? 出了作用域,對象ret不在,它是一個局部對象,我們這里不能用引用,用了的話,返回的就是ret的別名,但是ret又已經(jīng)銷毀了,訪問野指針了,所以出了作用域,如果對象不在了,就不能用引用返回,要用傳值返回
- 法二:復(fù)用日期+=天數(shù)
此法是建立在日期+=天數(shù)的基礎(chǔ)上完成的,這里各位可以先看下文日期+=天數(shù),然后我們進(jìn)行復(fù)用:
Date Date::operator+(int day) {//法二:復(fù)用日期 += 天數(shù)Date ret(*this);ret += day;return ret; }
- 法一和法二熟優(yōu)?
答案:法二更好,也就是用+去復(fù)用+=,具體原因在下文會解釋。
?
?💦日期 + = 天數(shù)
?這里實現(xiàn) += 其實有兩種方案
- 法一:
前面我實現(xiàn)的日期+天數(shù),仔細(xì)觀察我的代碼,函數(shù)的第一行,我就調(diào)用了一個拷貝構(gòu)造:
Date ret(*this); //拷貝構(gòu)造,拿d1去初始化ret
? ? ? ? 這里調(diào)用拷貝構(gòu)造,是為了不在*this本身上做變動,只在ret上進(jìn)行操作,其理由是日期+天數(shù)得到的是另一個日期,而不用拷貝構(gòu)造直接在*this上做改動只會導(dǎo)致原有的日期也變化,而這個變化正是我日期 += 天數(shù)的需求
? ? ? ? 仔細(xì)想想:+=天數(shù)就是在原有的日期上再加一定的天數(shù),直接對*this做手腳即可,因此只需對日期+天數(shù)的代碼進(jìn)行小改動即可:
Date& Date::operator+=(int day) //傳引用返回 {//如果day小于0,要單獨處理if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this; }
注意這里是傳引用返回,原因就在于我返回的*this是全局的,出了作用域還在
?
- 法二:復(fù)用日期 +天數(shù)
Date& Date::operator+=(int day) {//法二:復(fù)用* this = *this + day; //讓d1+過天數(shù)后再返回給自己從而實現(xiàn)+=return *this; }
- 法一和法二熟優(yōu)?
答案:法一。其實討論這個問題就是在討論用+去復(fù)用+=好還是用+=復(fù)用+好,答案是用+=去復(fù)用+好,因為+有兩次拷貝,而+=沒有拷貝,所以實現(xiàn)+,并且用+=去復(fù)用+效率更高
?
💦?日期 -= 天數(shù)
- 思路:
日期-=天數(shù)得到的還是一個日期,且是在原日期的基礎(chǔ)上做改動。合法的日期減去天數(shù)后的day只要>0就沒問題,若小于0就要借位了。要注意當(dāng)減去的天數(shù)<0時單獨討論。具體步驟如下:
- 當(dāng)減的天數(shù)為負(fù)數(shù),則為+=,直接調(diào)用
- 若減后的day<0,月-1
- 若月 = 0,則年-1,月置為12
?
- 代碼如下:
// 日期-= 天數(shù) :d1 d1 - 100 Date& Date::operator-=(int day) {if (day < 0){return *this += (-day);}_day -= day;while (_day <= 0){_month--;if (_month == 0){_month = 12;_year--;}_day += GetMonthDay(_year, _month);}return *this; }
💦日期 - 天數(shù)?
有了先前日期+和+=的基礎(chǔ),這里實現(xiàn)日期 - 天數(shù)直接復(fù)用日期 -= 天數(shù)即可:
// 日期-天數(shù) : d1 - 100 Date Date::operator-(int day) {// 調(diào)用拷貝構(gòu)造Date temp(*this);// 復(fù)用 "-="temp -= day;return temp; }
?💦日期 前置++
- 思路:
? ? ? ? C++里有前置++和后置++,這就導(dǎo)致一個巨大的問題,該如何區(qū)分它們,具體實現(xiàn)過程不難(直接復(fù)用+=即可),難的是如何區(qū)分前置和后置。因此C++規(guī)定,無參的為前置,有參的為后置。
- 代碼如下:
// 前置++ (無參的為前置,有參的為后置) Date& Date::operator++() {// 直接復(fù)用 +=*this += 1;return *this; }
??💦日期 后置++
- 思路:
? ? ? ? 有參的即為后置,后置++拿到的返回值應(yīng)該是自己本身未加過的,因此要先把自己保存起來,再++*this,隨后返回自己。
- 代碼如下:
// 后置++(無參的為前置,有參的為后置) // int i 這里的形參可以寫,可以不寫 Date Date::operator++(int i) {Date temp(*this);*this += 1;return temp; }
?💦日期 前置--
- 思路:
前置--和前置++沒啥區(qū)別,只不過內(nèi)部復(fù)用的是-=
- 代碼如下:
// 前置-- Date& Date::operator--() {*this -= 1;return *this; }
?💦日期 后置--
- 思路:
后置--和后置++類似,只不過內(nèi)部復(fù)用的是-=,不再贅述
- 代碼如下:
//后置-- Date Date::operator--(int i) {Date temp(*this);*this -= 1;return temp; }
??效果展示
?舉例:將日期 + 100 天? :2023 / 10 / 28? + 100
// d1+=100 測試 void Test3() {Date d1(2023, 10, 28);cout << "d1的日期為:";d1.Printf();cout << endl;d1 += 100;cout << "d1的日期+100為:";d1.Printf(); }int main() {Test3();return 0; }
?🍊日期與日期之間的減法運算
?💦日期 - 日期
- 思路:
? ? ? ?日期 - 日期得到的是天數(shù),首先我們得判斷兩個日期的大小,用min和max代替小的和大的,隨后,算出min和max之間的差距,若min!=max,則min就++,隨即定義變量n也自增++,最后返回n(注意符號)
- 代碼如下:
// 日期 - 日期 int Date::operator-(const Date& d) {// 方便后續(xù)計算正負(fù)int flag = 1;Date max = *this;Date min = d;// 確保max是大的 min是小的if (*this < d){min = *this;max = d;flag = -1; //計算正負(fù)}int n = 0;// 計算min和max之間的絕對值差距while (min != max){min++;n++;}return n * flag; }
??效果展示
?舉例:計算 2023 / 10 /28? -------------? ?2023? / 11 / 11? 的天數(shù)差距?
// 日期 - 日期 void Test4() {Date d1(2023, 11, 11);cout << "d1的日期為:";d1.Printf();cout << "d2的日期為:";Date d2(2023, 10, 28);d2.Printf();cout << endl;cout << "d1與d2之間的天數(shù)差距為:";int ret = d1 - d2;cout << ret << endl;}int main() {Test4();return 0; }
?四、日期計算器的完整代碼
🍇Date.h?
#pragma once #include <iostream> #include <stdio.h> #include <assert.h> #include <stdbool.h> using std::cout; using std::cin; using std::endl;class Date { public:// 構(gòu)造函數(shù)用于初始化 -- 聲明 // 在聲明中缺省函數(shù)需要 寫清楚 在定義中缺省函數(shù)就不要寫了 防止編譯器分不清Date(int year = 1, int month = 1, int day = 1);//判斷是否為閏年bool isLeaveYear(int year);// 獲取每個月的天數(shù)int GetMonthDay(int year, int month);// 打印void Printf();// "<" 運算符重載bool operator<(const Date& d);// "==" 運算符重載bool operator==(const Date& d);// "<=" 運算符重載bool operator<=(const Date& d);// ">" 運算符重載bool operator>(const Date& d);// ">="運算符重載、bool operator>=(const Date& d);//"!="運算符重載bool operator!=(const Date& d);// 日期+= 天數(shù): d1 = d1 + 100Date& operator+= (int day);// 日期 + 天數(shù) d1+100 d1不變Date operator+(int day);// 日期-= 天數(shù) :d1 = d1 - 100Date& operator-=(int day);// 日期-天數(shù) : d1 - 100Date operator-(int day);// 前置++ (無參的為前置,有參的為后置)Date& operator++();// 后置++(無參的為前置,有參的為后置)Date operator++(int i);// 前置--Date& operator--();//后置--Date operator--(int i);// 日期 - 日期int operator-(const Date& d); private:int _year;int _month;int _day; };
🍋Date.cpp?
#define _CRT_SECURE_NO_WARNINGS 1 #include "Date.h"// 構(gòu)造函數(shù)用于初始化 Date::Date(int year, int month, int day) {_year = year;_month = month;_day = day;if (_year < 1 || _month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)){assert(false);} }//判斷是否為閏年 bool Date::isLeaveYear(int year) {// (四年一潤,百年不潤) 或者 四百年一潤return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }// 獲取每個月的天數(shù) int Date::GetMonthDay(int year, int month) {int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && isLeaveYear(year)){return 29;}else{return monthday[month];} }void Date::Printf() {cout << _year << " / " << _month << " / " << _day << endl; }// "<" 運算符重載 bool Date::operator<(const Date& d) {if (_year < d._year ||_year == d._year && _month < d._month ||_year == d._year && _month == d._month && _day < d._day){return true;}else{return false;} }// "==" 運算符重載 bool Date::operator==(const Date& d) {return _year == d._year && _month == d._month && _day == d._day; }// "<=" 運算符重載 bool Date::operator<=(const Date& d) {// 運算符重載的復(fù)用return *this < d || *this == d; }// ">" 運算符重載 bool Date::operator>(const Date& d) {return !(*this <= d); }// ">="運算符重載、 bool Date::operator>=(const Date& d) {return !(*this < d); }//"!="運算符重載 bool Date::operator!=(const Date& d) {return !(*this == d); }// 日期+= 天數(shù): d1 = d1 + 100 Date& Date::operator+=(int day) {// 防止傳入的天數(shù)為 負(fù)數(shù)(-100)if (day < 0){return *this -= (-day);}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this; }// 日期 + 天數(shù) d1+100 d1不變 Date Date::operator+(int day) {// 調(diào)用拷貝構(gòu)造Date temp(*this);temp += day;return temp; }// 日期-= 天數(shù) :d1 d1 - 100 Date& Date::operator-=(int day) {if (day < 0){return *this += (-day);}_day -= day;while (_day <= 0){_month--;if (_month == 0){_month = 12;_year--;}_day += GetMonthDay(_year, _month);}return *this; }// 日期-天數(shù) : d1 - 100 Date Date::operator-(int day) {// 調(diào)用拷貝構(gòu)造Date temp(*this);// 復(fù)用 "-="temp -= day;return temp; }// 前置++ (無參的為前置,有參的為后置) Date& Date::operator++() {// 直接復(fù)用 +=*this += 1;return *this; }// 后置++(無參的為前置,有參的為后置) // int i 這里的形參可以寫,可以不寫 Date Date::operator++(int i) {Date temp(*this);*this += 1;return temp; }// 前置-- Date& Date::operator--() {*this -= 1;return *this; }//后置-- Date Date::operator--(int i) {Date temp(*this);*this -= 1;return temp; }// 日期 - 日期 int Date::operator-(const Date& d) {// 方便后續(xù)計算正負(fù)int flag = 1;Date max = *this;Date min = d;// 確保max是大的 min是小的if (*this < d){min = *this;max = d;flag = -1; //計算正負(fù)}int n = 0;// 計算min和max之間的絕對值差距while (min != max){min++;n++;}return n * flag; }
🥝Test.cpp
#define _CRT_SECURE_NO_WARNINGS 1 #include "Date.h"// 測試初始化 void Test() {cout << "請輸入今日的日期" << endl;Date d1(2023,10,28);d1.Printf(); }// 測試"<"運算符 void Test1() {Date d1(2023,10,28);cout << "d1的日期為:";d1.Printf();cout << "d2的日期為:";Date d2(2023, 10, 27);d2.Printf();cout << endl;if (d2 < d1){cout << "d1的日期 < d2的日期" << endl;}else{cout << "d2的日期 > d1的日期" << endl;} }// d1 + 100 測試 void Test2() {Date d1(2023,10,27);cout << "d1的日期為:";d1.Printf();Date d2;d2 = d1 + 100;cout << "d1的日期+100為:";d2.Printf(); }// d1+=100 測試 void Test3() {Date d1(2023, 10, 28);cout << "d1的日期為:";d1.Printf();cout << endl;d1 += 100;cout << "d1的日期+100為:";d1.Printf(); }// 日期 - 日期 void Test4() {Date d1(2023, 11, 11);cout << "d1的日期為:";d1.Printf();cout << "d2的日期為:";Date d2(2023, 10, 28);d2.Printf();cout << endl;cout << "d1與d2之間的天數(shù)差距為:";int ret = d1 - d2;cout << ret << endl;}void Test5() {cout << " *********** 歡迎來到 日期計算器 ***********" << endl<<endl;cout << "輸入今日的日期: >" ;int y, m, d;cin >> y >> m >> d;cout << endl;Date d1(y, m, d);//d1.Printf();cout << "輸入需要對比的日期: >";int y1, m1, dd;cin >> y1 >> m1 >> dd;cout << endl;Date d2(y1, m1, dd);//d2.Printf();cout << "兩個日期之間的天數(shù)差距為:";int ret = d - dd;cout << ret << endl; }int main() {Test5();return 0; }
?🍍代碼運行的界面
?五、共勉
??以下就是我對C++日期計算器的理解,如果有不懂和發(fā)現(xiàn)問題的小伙伴,請在評論區(qū)說出來哦,同時我還會繼續(xù)更新對C++ 類和對象的理解,請持續(xù)關(guān)注我哦!!!??
?