C++實(shí)現(xiàn)日期計(jì)算器詳細(xì)代碼示例
概要
本篇主要探究C++ 實(shí)現(xiàn)日期計(jì)算器
Date類的規(guī)劃
class Date { public: int GetMonthDay(int year, int month); Date(int year = 2024 , int month = 2, int day = 6); Date(const Date& d); Date& operator=(const Date& d); ~Date(); Date& operator+=(int day); Date operator+(int day); Date operator-(int day); Date& operator-=(int day); Date& operator++(); Date operator++(int); Date operator--(int); Date& operator--(); 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; int operator-(const Date& d) const; void Print(); private: int _year; int _month; int _day; };
Date類的實(shí)現(xiàn)
Date 構(gòu)造函數(shù)
Date::Date(int year, int month, int day) :_year(year) , _month(month) , _day(day) { }
構(gòu)造函數(shù),他是在創(chuàng)建類的時(shí)候調(diào)用進(jìn)行初始化操作,我們這里聲明與定義分離,所以它的參數(shù)不需要填寫缺省值,缺省值在聲明的時(shí)候定義即可。
Date 拷貝構(gòu)造函數(shù)
Date::Date(const Date& x) :_year(x._year) , _month(x._month) , _day(x._day) { }
拷貝構(gòu)造函數(shù)和構(gòu)造函數(shù)作用相似,拷貝構(gòu)造函數(shù)是將已有的類的對(duì)象拷貝給新創(chuàng)建的類的對(duì)象,如上所示。
~Date 析構(gòu)函數(shù)
Date::~Date() { _year = _month = _day = 0; }
構(gòu)析函數(shù)是在對(duì)象即將銷毀前所調(diào)用的函數(shù),常用來清理數(shù)據(jù)空間,這里我們的內(nèi)存都在棧上申請(qǐng)的,所以影響不大。
GetMonthDay 求某年某月的天數(shù)
int Date::GetMonthDay(int year,int month) { int a[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 29; } return a[month]; }
該函數(shù)是用來求某年某月的月份天數(shù),通過創(chuàng)建一個(gè)大小為13的數(shù)組a, 再對(duì)二月份進(jìn)行判斷閏年來確定是否為特殊情況,否則返回已設(shè)定好的月份天數(shù)。
operator= 賦值操作符重載
Date& Date::operator=(const Date& d) { _year=d._year; _month=d._month; _day=d._day; return *this; }
該函數(shù)用于對(duì)象與對(duì)象之間的賦值操作,在對(duì)象的年月日進(jìn)行逐一復(fù)制后并返回this指針的解引用。
operator+= 加等操作符重載
Date& Date::operator+=(int day) { _day+=day; int time=GetMonthDay(_year,_month); while(_day>time) { _day-=time; _month++; if(_month>12) { _month-=12; _year++; } time = GetMonthDay(_year,_month); } return *this; }
該函數(shù)用于日期與天數(shù)的相加,在實(shí)現(xiàn)該函數(shù)時(shí)先將this->_day加上想加的day。然后再通過月份的天數(shù)以及月與年的關(guān)系進(jìn)行調(diào)整,如上所示。
operator+ 加號(hào)操作符重載
Date Date::operator+(int day) { Date newdate = *this; newdate+=day; return newdate; }
通過復(fù)用加等操作符重載,在不改變*this的情況下,返回相加后的對(duì)象
operator-= 減等操作符重載
Date& Date::operator(int day) { _day-=day; int time =GetMonthDay(_year,_month) while(_day>=0) { _day+=time; _month--; if(_month<=1) { _month+=12; _year--; } time=GetMonthDay(_year,_month); } return *this; }
該函數(shù)和加等操作符重載類似,先將this->_day減去day,然后進(jìn)行月份的天數(shù)和年與月之間的調(diào)整即可。
operator- 減法操作符重載 (日期 - 天數(shù))
Date Date::operator-(int day) { Date newdate=*this; newdate-=day; return newdate; }
通過復(fù)用減等操作符重載,在不改變*this的情況下,返回相加后的對(duì)象
operator++ 前置自增操作符重載
Date& Date::operator++() { (*this)+=1; return *this; }
operator++ 后置自增操作符重載
Date Date::operator--(int) { Date a = *this; (*this)+=1; return a; }
operator-- 前置自減操作符重載
Date& Date::operator--() { (*this)-=1; return *this; }
operator-- 后置自減操作符重載
Date Date::operator--(int) { Date a=*this; (*this)-=1; return a; }
operator< 小于操作符重載
bool Date::operator<(const Date& d) { if(_year<d._year) { return 1; } else if(_year==d._year) { if(_month<d._month) { return 1; } else if(_month==d._month) { if(_day<d._day) { return 1; } } } return 0; }
operator== 相等操作符重載
bool Date::operator==(const Date& d) { return _day==d._day&&_month==d._month&&_year==d._year; }
operator!= 不等操作符重載
bool Date::operator!=(const Date& d) { return !(*this==d); }
operator<= 小于或等于操作符重載
bool Date::operator<=(const Date& d) { return (*this<d)||(*this==d); }
operator> 大于操作符重載
bool Date::operator>(const Date& d) { return !(*this<=d); }
operator>= 大于或等于操作符重載
bool Date::operator>=(const Date& d) { return !(*this<d); }
operator- 減法操作符重載(日期 - 日期)
int operator-(const Date& date) const { Date max = *this,min = date; int flag=(max>=min); if(!flag) { max=date; min=*this; flag=-1; } int count=0; while(max>min) { min++; count++; } return flag*count; }
通過比較選出最大的日期max與最小的日期min,讓最小的日期min和記錄數(shù)的count進(jìn)行循環(huán)自增,直到max與min相等為止,此時(shí)刻的count就是相差的天數(shù),而flag先前用于比較的就是它的符號(hào),所以返回flag*count。
Print 打印函數(shù)
void Date::Print() { printf("%d %d %d\n",_year,_month,_day); }
Date 類的測(cè)試
測(cè)試操作符重載(+,+=,- ,-=,x++,++x,x–,–x)
void Test_1() { std::cout << "Test_1:\n"; Date a(2024, 2, 6); std::cout << "a ||"; a.Print(); Date b = a; std::cout << "b ||"; b.Print(); b += 30; std::cout << "b += 30 ||"; b.Print(); a -= 20; std::cout << "a -= 20 ||"; a.Print(); Date c = b - 30; std::cout << "c 或 b - 30 ||"; c.Print(); Date d = a + 20; std::cout << "d 或 a + 20 ||"; d.Print(); d++; std::cout << "++d ||"; d.Print(); c--; std::cout << "++c ||"; c.Print(); Date e = a++; std::cout << "e = a++ -> e||"; e.Print(); std::cout << "e = a++ -> a||"; a.Print(); Date f = a++; std::cout << "f = b-- -> f||"; f.Print(); std::cout << "f = b-- -> b||"; b.Print(); std::cout << "\n"; }
運(yùn)行結(jié)果如下:
測(cè)試操作符重載(>,>=,==,<,<=,!=)
void Test_2() { std::cout << "Test_2:\n"; Date a(2024, 2, 6); std::cout << "a ||"; a.Print(); Date b = a + 999; std::cout << "b ||"; b.Print(); Date c = a - 999; std::cout << "c ||"; c.Print(); Date d = a; std::cout << "d ||"; d.Print(); std::cout << "a < b ->" << (a < b) << std::endl; std::cout << "a > b ->" << (a > b) << std::endl; std::cout << "a == b ->" << (a == b) << std::endl; std::cout << "a != b ->" << (a != b) << std::endl; std::cout << "a < c ->" << (a < c) << std::endl; std::cout << "a > c ->" << (a > c) << std::endl; std::cout << "a == c ->" << (a == c) << std::endl; std::cout << "a != c ->" << (a != c) << std::endl; std::cout << "a == d ->" << (a == d) << std::endl; std::cout << "a != d ->" << (a != d) << std::endl; std::cout << "a >= d ->" << (a >= d) << std::endl; std::cout << "a <= d ->" << (a <= d) << std::endl; std::cout << "a < d ->" << (a < d) << std::endl; std::cout << "a > d ->" << (a > d) << std::endl; std::cout << "\n"; }
運(yùn)行結(jié)果如下:
測(cè)試操作符重載(日期 - 日期)
void Test_3() { std::cout << "Test_3:\n"; Date a(2024, 2, 6); Date b(2025, 2, 6); Date c = a + 99; std::cout << "c ||"; c.Print(); std::cout << "a ||"; a.Print(); std::cout << "b ||"; b.Print(); std::cout << "a - b ||" << (a - b) << std::endl; std::cout << "b - b ||" << (b - b) << std::endl; std::cout << "b - a ||" << (b - a) << std::endl; std::cout << "c - a ||" << (c - a) << std::endl; std::cout << "a - c ||" << (a - c) << std::endl; std::cout << "\n"; }
運(yùn)行結(jié)果如下:
測(cè)試完畢,綜合上述代碼及運(yùn)行結(jié)果,可得代碼正確,可以正常模擬日期計(jì)算器。
結(jié)語
到此這篇關(guān)于C++實(shí)現(xiàn)日期計(jì)算器的文章就介紹到這了,更多相關(guān)C++日期計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言算法練習(xí)之?dāng)?shù)組求素?cái)?shù)
這篇文章主要為大家介紹了C語言算法練習(xí)中數(shù)組求素?cái)?shù)的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下2022-09-09Qt QWidget實(shí)現(xiàn)圖片旋轉(zhuǎn)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了如何使用了Qt和QWidget實(shí)現(xiàn)圖片旋轉(zhuǎn)動(dòng)畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12C++ vector擴(kuò)容解析noexcept應(yīng)用場(chǎng)景
這篇文章主要介紹了C++ vector擴(kuò)容解析noexcept應(yīng)用場(chǎng)景,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09C++中IO多路復(fù)用(select、poll、epoll)的實(shí)現(xiàn)
I/O多路復(fù)用是一種并發(fā)處理多個(gè)I/O操作的機(jī)制,本文主要介紹了C++中IO多路復(fù)用(select、poll、epoll)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03C++ 讀文件 將文件內(nèi)容讀入到字符串string中的方法
今天小編就為大家分享一篇C++ 讀文件 將文件內(nèi)容讀入到字符串string中的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07C++中std::construct()與std::destroy()的使用
std::construct()和std::destroy()是C++ STL中的函數(shù)模板,用于在已分配的存儲(chǔ)區(qū)域中構(gòu)造或銷毀對(duì)象,本文主要介紹了C++中std::construct()與std::destroy()的使用,感興趣的可以了解一下2024-02-02