C++日期類計算器的模擬實現(xiàn)舉例詳解
更新時間:2023年04月26日 14:38:36 作者:EnticE152
兩個日期之間相隔天數(shù)的計算網(wǎng)上有許多的軟件,這里主要介紹如何使用C/C++語言來完成這樣的功能,下面這篇文章主要給大家介紹了關(guān)于C++日期類計算器的模擬實現(xiàn),需要的朋友可以參考下
日期類計算器的模擬實現(xiàn)::
1.獲取某年某月的天數(shù)
int GetMonthDay(int year, int month)
{
static int monthDayArray[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;
}
else
{
return monthDayArray[month];
}
}2.構(gòu)造函數(shù)
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
//檢查日期是否合法
if (!((year >= 1)
&& (month >= 1 && month <= 12)
&& (day >= 1 && day <= GetMonthDay(year, month))))
{
cout << "非法日期" << endl;
}
}3.拷貝構(gòu)造函數(shù)
// 拷貝構(gòu)造函數(shù) 形參加const 防止寫反了 問題就可以檢查出來了
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}4.賦值運算符重載
//d1 = d2
//注:1.要注意兩個參數(shù)的順序 2.這里面參數(shù)不加引用不會導致無窮遞歸 但為了避免拷貝構(gòu)造最好加引用
Date& operator=(const Date& d)
{
//為了支持鏈式賦值 if是為了避免自己給自己賦值 d1 = d1
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}5.析構(gòu)函數(shù)
~Date()//可不寫
{
;
}日期類因為沒有申請資源,所以無需寫析構(gòu)函數(shù),編譯器默認生成的析構(gòu)函數(shù)就可以。
6.日期+=天數(shù)
//d1 += 100
//天滿了進月 月滿了進年
Date& operator+=(int day)
{
//避免 d1 += -1000的情形
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;
}7.日期+天數(shù)
//d1 + 100
Date operator+(int day) const
{
Date ret(*this);
ret += day;//ret.operator+=(day)
return ret;
}8.日期-天數(shù)
//d1 - 100
Date operator-(int day) const
{
Date ret(*this);
ret -= day;
return ret;
}9.日期-=天數(shù)
//d1 -= 100
Date& operator-=(int day)
{
//避免 d1 -= -1000
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}10.前置++的運算符重載
//前置++
Date& operator++()
{
//會調(diào)用 operator+=(int day)
*this += 1;
return *this;
}11.后置++的運算符重載
//后置++ —多一個int參數(shù)主要是為了和前置++進行區(qū)分 構(gòu)成函數(shù)重載
Date operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}12.前置--的運算符重載
//前置--
Date& operator--()
{
//復用運算符重載-=
*this -= 1;
return *this;
}13.后置--的運算符重載
//后置--
Date operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}14.>的運算符重載
//d1 > d2
bool 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;
}
return false;
}15.<的運算符重載
//d1 < d2
bool operator<(const Date& d) const
{
return !(*this >= d);
}16.==的運算符重載
//d1 == d2
bool operator==(const Date& d) const
{ return _year == d._year
&& _month == d._month
&& _day == d._day;
}17.>=的運算符重載
//d1 >= d2
bool operator>=(const Date& d) const
{
return *this > d || *this == d;
}18.<=的運算符重載
//d1 <= d2
bool operator<=(const Date& d) const
{
return !(*this > d);
}19.!=的運算符重載
//d1 != d2
bool operator!=(const Date& d) const
{
return !(*this == d);
}20.<<的運算符重載
//內(nèi)聯(lián)函數(shù)和靜態(tài)成員一樣 調(diào)用處展開 不進符號表
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}21.>>的運算符重載
//cin >> d1 編譯器轉(zhuǎn)化成operator(cin,d1) 形參中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}22.日期-日期
//日期-日期
int operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
//總結(jié):凡是內(nèi)部不改變成員變量 也就是不改變*this數(shù)據(jù)的 這些成員函數(shù)都應該加const
//if (d > *this)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++n;
//復用++ ++到和d1日期相等 就是相差多少天
++min;
}
return n * flag;
}Date.h
#pragma once
#include <iostream>
using namespace std;
class Date
{
//友元聲明(類的任意位置)聲明友元時可以不用加inline
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
int GetMonthDay(int year, int month)
{
static int monthDayArray[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;
}
else
{
return monthDayArray[month];
}
}
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
//檢查日期是否合法
if (!((year >= 1)
&& (month >= 1 && month <= 12)
&& (day >= 1 && day <= GetMonthDay(year, month))))
{
cout << "非法日期" << endl;
}
}
// 拷貝構(gòu)造函數(shù) 形參加const 防止寫反了 問題就可以檢查出來了
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//d1 == d2
bool operator==(const Date& d) const;
//d1 > d2
bool operator>(const Date& d) const;
//d1 >= d2
bool operator>=(const Date& d) const;
//d1 <= d2
bool operator<=(const Date& d) const;
//d1 < d2
bool operator<(const Date& d) const;
//d1 != d2
bool operator!=(const Date& d) const;
//d1 += 100
Date& operator+=(int day);
//d1 + 100
Date operator+(int day) const;
//d1 = d2 注:1.要注意兩個參數(shù)的順序 2.這里面參數(shù)不加引用不會導致無窮遞歸 但為了避免拷貝構(gòu)造最好加引用
Date& operator=(const Date& d)
{
//為了支持鏈式賦值 if是為了避免自己給自己賦值 d1 = d1
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//d1 -= 100
Date& operator-=(int day);
//d1 - 100
Date operator-(int day) const;
//++的操作數(shù)只有一個 不傳參
//前置++
Date& operator++();
//編譯器為了區(qū)分前置++和后置++ 規(guī)定在后置的函數(shù)上加了一個參數(shù)
//后置++
Date operator++(int);
//允許成員函數(shù)加const 此時this指針的類型為:const Date* const this
void Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
//前置--
Date& operator--();
//后置--
Date operator--(int);
//日期-日期
int operator-(const Date& d) const;
//流插入
//d1 << cout編譯器會轉(zhuǎn)化成d1.operator<<(cout) this指針搶了左操作數(shù)d1的位置
//<<和>>的重載一般不寫成成員函數(shù) 因為this默認搶了第一個參數(shù)的位置 Date類對象就是左操作數(shù) 不符合使用習慣和可讀性
/*void operator<<(ostream& out)
{
out << _year << "年" << _month << "月" << _day << "日" << endl;
}*/
//取地址重載
Date* operator&()
{
return this;
}
//const成員取地址重載
const Date* operator&() const
{
return this;
}
//取地址重載和const成員取地址重載不實現(xiàn) 編譯器會默認生成
private:
int _year;
int _month;
int _day;
};
//結(jié)論:對于自定義類型,盡量用前置,減少拷貝,提高效率
//全局函數(shù)調(diào)用:cout << d1轉(zhuǎn)化成operator<<(cout,d1)
//全局函數(shù)的定義和全局變量不能放在.h文件中 因為函數(shù)的定義在Date.cpp和test.cpp都會展開 函數(shù)地址進入符號表 鏈接器鏈接兩個.cpp文件時相同的函數(shù)地址會報錯
//解決方法:1.改成靜態(tài) 2.聲明和定義分離
//static修飾函數(shù)只在當前文件可見 不會進入符號表
//static void operator<<(ostream& out,const Date& d)
//{
// out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//}
//ostream& operator<<(ostream& out, const Date& d);
//內(nèi)聯(lián)函數(shù)和靜態(tài)成員一樣 調(diào)用處展開 不進符號表
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
//cin >> d1 編譯器轉(zhuǎn)化成operator(cin,d1) 形參中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}Date.cpp
#include"Date.h"
//d1 == d2
bool Date::operator==(const Date& d) const
{ return _year == d._year
&& _month == d._month
&& _day == d._day;
}
//d1 > d2
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;
}
return false;
}
//d1 >= d2
bool Date::operator>=(const Date& d) const
{
return *this > d || *this == d;
}
//d1 <= d2
bool Date::operator<=(const Date& d) const
{
return !(*this > d);
}
//d1 < d2
bool Date::operator<(const Date& d) const
{
return !(*this >= d);
}
//d1 != d2
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
//d1 += 100
//天滿了進月 月滿了進年
Date& Date::operator+=(int day)
{
//避免 d1 += -1000的情形
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;
}
//d1 + 100
Date Date::operator+(int day) const
{
Date ret(*this);
ret += day;//ret.operator+=(day)
return ret;
}
//d1 -= 100
Date& Date::operator-=(int day)
{
//避免 d1 -= -1000
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
//d1 - 100
Date Date::operator-(int day) const
{
Date ret(*this);
ret -= day;
return ret;
}
//前置++
Date& Date::operator++()
{
//會調(diào)用 operator+=(int day)
*this += 1;
return *this;
}
//后置++ —多一個int參數(shù)主要是為了和前置++進行區(qū)分 構(gòu)成函數(shù)重載
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//前置--
Date& Date::operator--()
{
//復用運算符重載-=
*this -= 1;
return *this;
}
//后置--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
//日期-日期
int Date::operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
//總結(jié):凡是內(nèi)部不改變成員變量 也就是不改變*this數(shù)據(jù)的 這些成員函數(shù)都應該加const
//if (d > *this)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++n;
//復用++ ++到和d1日期相等 就是相差多少天
++min;
}
return n * flag;
}
//為了支持鏈式流插入 cout<< d1 <<d2 返回cout類對象
//ostream& operator<<(ostream& out,const Date& d)
//{
// out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
// return out;
//}總結(jié)
到此這篇關(guān)于C++日期類計算器的模擬實現(xiàn)的文章就介紹到這了,更多相關(guān)C++日期類計算器模擬實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)順序表中的增刪改(頭插頭刪)教程示例詳解
這篇文章主要為大家介紹了C語言數(shù)據(jù)結(jié)構(gòu)順序表中增刪改關(guān)于頭插頭刪的教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02
基于matlab實現(xiàn)DCT數(shù)字水印嵌入與提取
數(shù)字水印技術(shù)是將一些標識信息直接嵌入數(shù)字載體當中,?或間接表示在信號載體中,?且不影響原載體的使用價值。本文主要為大家介紹了基于matlab如何實現(xiàn)數(shù)字水印的嵌入與提取,感興趣的可以學習一下2022-01-01
C++基于reactor的服務器百萬并發(fā)實現(xiàn)與講解
這篇文章主要介紹了C++基于reactor的服務器百萬并發(fā)實現(xiàn)與講解,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

