溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++日期類計算器怎么實現

發布時間:2023-04-26 15:46:21 來源:億速云 閱讀:131 作者:iii 欄目:開發技術

這篇文章主要介紹“C++日期類計算器怎么實現”,在日常操作中,相信很多人在C++日期類計算器怎么實現問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++日期類計算器怎么實現”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

    1.獲取某年某月的天數

    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.構造函數

    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.拷貝構造函數

    // 拷貝構造函數 形參加const 防止寫反了 問題就可以檢查出來了
    Date(const Date& d)
    {
    	_year = d._year;
    	_month = d._month;
    	_day = d._day;
    }

    4.賦值運算符重載

    //d1 = d2 
    //注:1.要注意兩個參數的順序 2.這里面參數不加引用不會導致無窮遞歸 但為了避免拷貝構造最好加引用
    Date& operator=(const Date& d)
    {
    	//為了支持鏈式賦值 if是為了避免自己給自己賦值 d1 = d1
    	if (this != &d)
    	{
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    	}
    	return *this;
    }

    5.析構函數

    ~Date()//可不寫
    {
        ;
    }

    日期類因為沒有申請資源,所以無需寫析構函數,編譯器默認生成的析構函數就可以。

    6.日期+=天數

    //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.日期+天數

    //d1 + 100
    Date operator+(int day) const
    {
    	Date ret(*this);
    	ret += day;//ret.operator+=(day)
    	return ret;
    }

    8.日期-天數

    //d1 - 100
    Date operator-(int day) const
    {
    	Date ret(*this);
    	ret -= day;
    	return ret;
    }

    9.日期-=天數

    //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++()
    {
    	//會調用 operator+=(int day)
    	*this += 1;
    	return *this;
    }

    11.后置++的運算符重載

    //后置++ —多一個int參數主要是為了和前置++進行區分 構成函數重載
    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.<<的運算符重載

    //內聯函數和靜態成員一樣 調用處展開 不進符號表
    inline ostream& operator<<(ostream& out, const Date& d)
    {
    	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    	return out;
    }

    21.>>的運算符重載

    //cin >> d1 編譯器轉化成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)
      //總結:凡是內部不改變成員變量 也就是不改變*this數據的 這些成員函數都應該加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;
    		}
    	}
    	// 拷貝構造函數 形參加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.要注意兩個參數的順序 2.這里面參數不加引用不會導致無窮遞歸 但為了避免拷貝構造最好加引用
    	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;
    	//++的操作數只有一個 不傳參
    	//前置++
    	Date& operator++();
    	//編譯器為了區分前置++和后置++ 規定在后置的函數上加了一個參數
    	//后置++
    	Date operator++(int);
    	//允許成員函數加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編譯器會轉化成d1.operator<<(cout) this指針搶了左操作數d1的位置
    	//<<和>>的重載一般不寫成成員函數 因為this默認搶了第一個參數的位置 Date類對象就是左操作數 不符合使用習慣和可讀性
    	/*void operator<<(ostream& out)
    	{
    		out << _year << "年" << _month << "月" << _day << "日" << endl;
    	}*/
    	//取地址重載
    	Date* operator&()
    	{
    		return this;
    	}
    	//const成員取地址重載
    	const Date* operator&() const
    	{
    		return this;
    	}
    	//取地址重載和const成員取地址重載不實現 編譯器會默認生成
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    //結論:對于自定義類型,盡量用前置,減少拷貝,提高效率
    //全局函數調用:cout << d1轉化成operator<<(cout,d1)
    //全局函數的定義和全局變量不能放在.h文件中 因為函數的定義在Date.cpp和test.cpp都會展開 函數地址進入符號表 鏈接器鏈接兩個.cpp文件時相同的函數地址會報錯
    //解決方法:1.改成靜態 2.聲明和定義分離
    //static修飾函數只在當前文件可見 不會進入符號表
    //static void operator<<(ostream& out,const Date& d)
    //{
    //	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    //}
    //ostream& operator<<(ostream& out, const Date& d);
    //內聯函數和靜態成員一樣 調用處展開 不進符號表
    inline ostream& operator<<(ostream& out, const Date& d)
    {
    	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    	return out;
    }
    //cin >> d1 編譯器轉化成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++()
    {
    	//會調用 operator+=(int day)
    	*this += 1;
    	return *this;
    }
    //后置++ —多一個int參數主要是為了和前置++進行區分 構成函數重載
    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)
      //總結:凡是內部不改變成員變量 也就是不改變*this數據的 這些成員函數都應該加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;
    //}

    到此,關于“C++日期類計算器怎么實現”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    c++
    AI

    亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女