Arithmetics on calendar dates in C or C++ (add N days to given date)

后端 未结 8 1084
误落风尘
误落风尘 2020-12-06 07:47

I have been given a date, Which I am taking as an input like (day, month, year): 12, 03, 87.

Now I need to find out the date after n days.<

8条回答
  •  温柔的废话
    2020-12-06 07:57

    Can be implemented using C++ operators and in a quite OOP way by representing date as a class.

    #include 
    #include 
    
    using namespace std;
    
    class Date {
    public:
        Date(size_t year, size_t month, size_t day):m_year(year), m_month(month), m_day(day) {}
        ~Date() {}
    
        // Add specified number of days to date
        Date operator + (size_t days) const;
    
        size_t Year()  { return m_year; }
        size_t Month() { return m_month; }
        size_t Day()   { return m_day; }
    
        string DateStr();
    private:
        // Leap year check 
        inline bool LeapYear(int year) const
            { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); }
    
        // Holds all max days in a general year
        static const int MaxDayInMonth[13];
    
        // Private members
        size_t m_year;
        size_t m_month;
        size_t m_day;   
    };
    
    // Define MaxDayInMonth
    const int Date::MaxDayInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    /// Add specified number of days to date
    Date Date::operator + (size_t days) const {
        // Maximum days in the month
        int nMaxDays(MaxDayInMonth[m_month] + (m_month == 2 && LeapYear(m_year) ? 1 : 0));
    
        // Initialize the Year, Month, Days
        int nYear(m_year);
        int nMonth(m_month);
        int nDays(m_day + days);
    
        // Iterate till it becomes a valid day of a month
        while (nDays > nMaxDays) {
            // Subtract the max number of days of current month
            nDays -= nMaxDays;
    
            // Advance to next month
            ++nMonth;
    
            // Falls on to next year?
            if (nMonth > 12) {
                nMonth = 1; // January
                ++nYear;    // Next year
            }
    
            // Update the max days of the new month
            nMaxDays = MaxDayInMonth[nMonth] + (nMonth == 2 && LeapYear(nYear) ? 1 : 0);
        }
    
        // Construct date
        return Date(nYear, nMonth, nDays);
    }
    
    /// Get the date string in yyyy/mm/dd format
    string Date::DateStr() {
        return to_string(m_year) 
            + string("/")
            + string(m_month < 10 ? string("0") + to_string(m_month) : to_string(m_month))
            + string("/")
            + string(m_day < 10 ? string("0") + to_string(m_day) : to_string(m_day)); 
    }
    
    
    int main() {
        // Add n days to a date
        cout << Date(2017, 6, 25).DateStr() << " + 10 days = "
             << (Date(2017, 6, 25) /* Given Date */ + 10 /* Days to add */).DateStr() << endl;
    
        return 0;
    }
    
    Output
    2017/06/25 + 10 days = 2017/07/05
    

提交回复
热议问题