【C++】日期類+日期萬年歷+日期計算器

    對于日期類,我們主要實現(xiàn)一下日期類的基本函數(shù),構(gòu)造,拷貝構(gòu)造,運算符的重載,析構(gòu)。當然這里運算符的重載需要實現(xiàn)的還是挺多的,如:=、<、>、<=、>=、等

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)濱城免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

#include <iostream>
using namespace std;

class Date
{
public:
    Date(int year = 1990, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

    Date(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    ~Date()
    {}

    //萬年歷
    bool operator == (const Date& d)
    {
        return this->_year == d._year
            && this->_month == d._month
            && this->_day == d._day;
    }

    bool operator <(const Date& d)
    {
        if (_year<d._year)
        {
            return true;
        }
        else
        {
            if (_year == d._year)
            {
                if (_month < d._month)
                {
                    return true;
                }
                else
                {
                    if (_month == d._month)
                    {
                        if (_day < d._day)
                        {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    bool operator <=(const Date& d)
    {
        return !(*this > d);
    }

    bool operator >(const Date& d)
    {
        if (_year>d._year)
        {
            return true;
        }
        else
        {
            if (_year == d._year)
            {
                if (_month > d._month)
                {
                    return true;
                }
                else
                {
                    if (_month == d._month)
                    {
                        if (_day > d._day)
                        {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
    bool operator >=(const Date& d)
    {
        return !(*this < d);
    }

    

    對于實現(xiàn)日期計算器,我們主要考慮的是加天數(shù)和減天數(shù),那么問題就來了,對于加法,如果加的日期超過當前月的天數(shù)就需要考慮月的進位,對于年來說,如果月份大于12就需要重置為1,年進位。還需要考慮的一個問題就是,是否為閏年的2月份天數(shù)不同,那么應(yīng)該如何解決呢?我們用一個數(shù)組把每個月的天數(shù)給保存起來,然后寫一個判斷閏年的函數(shù),如果是閏年就在數(shù)組對應(yīng)的2月上加上1天。對于減法,就相當于加上一個負天數(shù),問題和加法一樣。

// 日期計算器
    Date operator+ (int day);
    Date operator+= (int day);

    Date operator- (int day)
    {
        this->_day -= day;
        while (_day < 0)
        {
            _day += GetMonthDay(2016, 3);
            _month -= 1;
            if (_month < 1)
            {
                _month = 12;
                _year -= 1;
            }
        }
        return *this;
    }
    Date operator-= (int day);

    Date operator++();
    Date operator++(int);

    Date operator--();
    Date operator--(int);

    int operator-(const Date& d);

    //計算器
    Date& calendar(int day = 0)
    {
        if (day > 0)//加正天數(shù)
        {

            this->_day += day;

            while (_day > GetMonthDay(2016, 2))
            {
                _day -= GetMonthDay(2016, 2);
                _month += 1;
                if (_month > 12)
                {
                    _month = 1;
                    _year += 1;
                }
            }
        }
        else//加負天數(shù)
        {
            this->_day -= day;
            while (_day < 0)
            {
                _day += GetMonthDay(2016, 3);
                _month -= 1;
                if (_month < 1)
                {
                    _month = 12;
                    _year -= 1;
                }
            }
        }

        return *this;
    }

private:
    bool IsLeapYear(int year)
    {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
        {
            return true;
        }
        return false;
    }

    int GetMonthDay(int year, int month)
    {
        int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        int day = monthArray[month];

        if (month == 2 && IsLeapYear(year))
        {
            day += 1;
        }

        return day;
    }

private:
    int _year;
    int _month;
    int _day;

本文標題:【C++】日期類+日期萬年歷+日期計算器
本文網(wǎng)址:http://bm7419.com/article4/goepoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站自適應(yīng)網(wǎng)站、微信公眾號、網(wǎng)站排名定制網(wǎng)站、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)