C++實現(xiàn)日期計算器詳細代碼示例
概要
本篇主要探究C++ 實現(xiàn)日期計算器
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類的實現(xiàn)
Date 構造函數(shù)
Date::Date(int year, int month, int day)
:_year(year)
, _month(month)
, _day(day)
{ }
構造函數(shù),他是在創(chuàng)建類的時候調(diào)用進行初始化操作,我們這里聲明與定義分離,所以它的參數(shù)不需要填寫缺省值,缺省值在聲明的時候定義即可。
Date 拷貝構造函數(shù)
Date::Date(const Date& x)
:_year(x._year)
, _month(x._month)
, _day(x._day)
{ }
拷貝構造函數(shù)和構造函數(shù)作用相似,拷貝構造函數(shù)是將已有的類的對象拷貝給新創(chuàng)建的類的對象,如上所示。
~Date 析構函數(shù)
Date::~Date()
{
_year = _month = _day = 0;
}
構析函數(shù)是在對象即將銷毀前所調(diào)用的函數(shù),常用來清理數(shù)據(jù)空間,這里我們的內(nèi)存都在棧上申請的,所以影響不大。
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)建一個大小為13的數(shù)組a, 再對二月份進行判斷閏年來確定是否為特殊情況,否則返回已設定好的月份天數(shù)。
operator= 賦值操作符重載
Date& Date::operator=(const Date& d)
{
_year=d._year;
_month=d._month;
_day=d._day;
return *this;
}
該函數(shù)用于對象與對象之間的賦值操作,在對象的年月日進行逐一復制后并返回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ù)的相加,在實現(xiàn)該函數(shù)時先將this->_day加上想加的day。然后再通過月份的天數(shù)以及月與年的關系進行調(diào)整,如上所示。
operator+ 加號操作符重載
Date Date::operator+(int day)
{
Date newdate = *this;
newdate+=day;
return newdate;
}
通過復用加等操作符重載,在不改變*this的情況下,返回相加后的對象
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,然后進行月份的天數(shù)和年與月之間的調(diào)整即可。
operator- 減法操作符重載 (日期 - 天數(shù))
Date Date::operator-(int day)
{
Date newdate=*this;
newdate-=day;
return newdate;
}
通過復用減等操作符重載,在不改變*this的情況下,返回相加后的對象
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進行循環(huán)自增,直到max與min相等為止,此時刻的count就是相差的天數(shù),而flag先前用于比較的就是它的符號,所以返回flag*count。
Print 打印函數(shù)
void Date::Print()
{
printf("%d %d %d\n",_year,_month,_day);
}
Date 類的測試
測試操作符重載(+,+=,- ,-=,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";
}
運行結果如下:

測試操作符重載(>,>=,==,<,<=,!=)
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";
}
運行結果如下:

測試操作符重載(日期 - 日期)
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";
}
運行結果如下:

測試完畢,綜合上述代碼及運行結果,可得代碼正確,可以正常模擬日期計算器。
結語
到此這篇關于C++實現(xiàn)日期計算器的文章就介紹到這了,更多相關C++日期計算器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
深入線性時間復雜度求數(shù)組中第K大數(shù)的方法詳解
本篇文章是對線性時間復雜度求數(shù)組中第K大數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Qt串口通信開發(fā)之QSerialPort模塊簡單使用方法與實例
這篇文章主要介紹了Qt串口通信開發(fā)之QSerialPort模塊簡單使用方法與實例,需要的朋友可以參考下2020-03-03
C語言創(chuàng)建動態(tài)dll和調(diào)用dll(visual studio 2013環(huán)境下)
本篇文章主要介紹了C語言創(chuàng)建動態(tài)dll和調(diào)用dll(visual studio 2013環(huán)境下),非常具有實用價值,需要的朋友可以參考下2017-11-11
C++11 shared_ptr 與 make_shared源碼剖析詳解
這篇文章主要介紹了C++11 shared_ptr 與 make_shared的源碼剖析,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

