⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 date.h

📁 《标准C++宝典》源码
💻 H
字号:
#ifndef DATE_H
#define DATE_H

#include <iostream>
using namespace std;

class Date  {
protected:
    static const int dys[];
    long int ndays; // days inclusive since Jan 1,1 (1/1/1 == 1)
public:
    // --- default and initializing constructor
    Date(int mo = 0, int da = 0, int yr = 0)
        { SetDate(mo, da, yr); }
    // --- copy constructor
    Date(const Date& dt)
        { *this = dt; }
    // --- destructor
    virtual ~Date() {}
    // --- overloaded assignment operator
    Date& operator=(const Date& dt)
        { ndays = dt.ndays; return *this; }
    // --- overloaded arithmetic operators
    Date  operator+(int n) const
        { Date dt(*this); dt += n; return dt; }
    Date  operator-(int n) const
        { Date dt(*this); dt -= n; return dt; }
    Date& operator+=(int n)
        { ndays += n; return *this; }
    Date& operator-=(int n)
        { ndays -= n; return *this; }
    Date& operator++()	   // prefix
        { ++ndays; return *this; }
    Date  operator++(int)  // postfix
        { Date dt(*this); dt.ndays++; return dt; }
    Date& operator--()	   // prefix
        { --ndays; return *this; }
    Date  operator--(int)  // postfix
        { Date dt(*this); dt.ndays--; return dt; }
    long int operator-(const Date& dt) const
        { return ndays-dt.ndays; }
    // --- overloaded relational operators
    bool operator==(const Date& dt) const
        { return ndays == dt.ndays; }
    bool operator!=(const Date& dt) const
        { return ndays != dt.ndays; }
    bool operator< (const Date& dt) const
        { return ndays <  dt.ndays; }
    bool operator> (const Date& dt) const
        { return ndays >  dt.ndays; }
    bool operator<=(const Date& dt) const
        { return ndays <= dt.ndays; }
    bool operator>=(const Date& dt) const
        { return ndays >= dt.ndays; }
    // --- getter and setter functions
    void SetDate(int mo, int da, int yr);
    void SetMonth(int mo)
        { *this = Date(mo, GetDay(), GetYear()); }
    void SetDay(int da)
        { *this = Date(GetMonth(), da, GetYear()); }
    void SetYear(int yr)
        { *this = Date(GetMonth(), GetDay(), yr); }
    void GetDate(int& mo, int& da, int& yr) const;
    int  GetMonth() const;
    int  GetDay()   const;
    int  GetYear()  const;
    // --- test for leap year
    bool IsLeapYear(int yr) const
        { return ((yr % 4)==0 && (yr % 1000)!=0); }
    bool IsLeapYear() const
        { return IsLeapYear(GetYear()); }
};
// ----- overloaded (int + Date)
inline Date operator+(int n, const Date& dt)
{
    return dt + n;
}

inline ostream& operator<<(ostream& os, const Date& dt)
{
    os << dt.GetMonth() << '/'
       << dt.GetDay()   << '/'
       << dt.GetYear();
    return os;
}
inline istream& operator>>(istream& is, Date& dt)
{
    int mo, da, yr;
    is >> mo >> da >> yr;
    dt.SetDate(mo, da, yr);
    return is;
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -