class Date 日期類

class Date

日期類是一個經典面試題
完成比較運算符重載
完成自增、自減運算符重載(前置、後置)
完成加減、加等減等 int 變量運算符重載
完成日期類相減運算符重載

date.h

#ifndef __CLASS_H__
#define __CLASS_H__

#include <iostream>
using namespace std;

class Date
{
public:
    Date(int, int, int day);

    bool Date::operator==(const Date& d)const;
    bool operator>(const Date& d)const;
    bool operator>=(const Date& d)const;
    bool operator<(const Date& d)const;
    bool operator<=(const Date& d)const;

    Date& operator++();
    Date operator++(int);
    Date& operator--();
    Date operator--(int);
    Date& operator+=(int day);
    Date operator+(int day)const;
    Date& operator-=(int day);
    Date operator-(int day)const;
    int operator-(const Date& d)const;

    //bool IsLeapYear(int year)const;
    //int GetMonthDays(int year, int month)const;
    //對於閏年檢查和月份天數的兩個函數,因爲與成員變量無關,認爲沒有作爲成員函數的必要
    //這裏選擇作爲靜態函數,不提供接口。也可在頭文件中另外聲明,提供接口。

    void display()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

private:
    int _year;
    int _month;
    int _day;
};

#endif __CLASS_H__

class_date.cpp

#include "date.h"

static const int Monthdays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

static bool IsLeapYear(const int year /*= _year*/)
{
    return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0));
}

static int GetMonthDays(const int year, const int month)
{
    if (month == 2 && (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
        return 29;
    return Monthdays[month];
}

Date::Date(int year = 1900, int month = 1, int day = 1)
:_year(year),
_month(month),
_day(day)
{
    if (_day < 1 || _day > GetMonthDays(year, month) ||
        _month < 1 || _month > 12)
    {
        cout << "wrong date" << endl;
        exit(-1);
    }
}

bool Date::operator==(const Date& d)const
{
    return _year == d._year && _month == d._month && _day == d._day;
}

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

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

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

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

Date& Date::operator++()
{
    if (_day + 1 > GetMonthDays(_year, _month))
    {
        if (_month + 1 == 13)
        {
            _year++;
            _month = 1;
            _day = 1;
        }
        else
        {
            _month++;
            _day = 1;
        }
    }
    else
    {
        _day++;
    }
    return *this;
}

Date Date::operator++(int)
{
    Date ret(*this);
    ++(*this);
    return ret;
}

Date& Date::operator--()
{
    if (_day == 1)
    {
        if (_month == 1)
        {
            _year--;
            _month = 12;
            _day = 31;
        }
        else
        {
            _month--;
            _day = GetMonthDays(_year, _month);
        }
    }
    else
    {
        _day--;
    }
    return *this;
}

Date Date::operator--(int)
{
    Date ret(*this);
    --(*this);
    return ret;
}

Date& Date::operator+=(int day)
{
    _day += day;
    if (day > 0)
    {
        while (_day > GetMonthDays(_year, _month))
        {
            _month == 12 ? _month = 1, _year++ : _month++;
            _day -= GetMonthDays(_year, _month);
        }
    }
    if (day < 0)
    {
        while (_day < 1)
        {
            _month == 1 ? _month = 12, _year-- : _month--;
            _day += GetMonthDays(_year, _month);
        }
    }
    return *this;
}

Date Date::operator+(int day)const
{
    Date ret = (*this);
    return ret += day;
}

Date& Date::operator-=(int day)
{
    return (*this) += -day;
}

Date Date::operator-(int day)const
{
    return (*this) + (-day);
}

int Date::operator-(const Date& d)const
{
    if (*this < d)
        return d - *this;
    if (*this == d)
        return 0;

    int ret = 0;
    Date tmp= *this;
    if (tmp._month == 2 && tmp._day == 29)
        tmp._day--, ret++;
    if (tmp._month >2)
    {
        while (tmp._year != d._year)
        {
            (tmp._year % 400 == 0 || (tmp._year % 100 != 0 && tmp._year % 4 == 0)) ?
                ret += 366 : ret += 365;
            tmp._year--;
        }
    }
    else
    {
        while (tmp._year != d._year)
        {
            tmp._year--;
            IsLeapYear(tmp._year) ? ret += 366 : ret += 365;
        }
    }
    while (tmp._month != d._month)
    {
        ret += tmp._day;
        tmp._month--;
        tmp._day = GetMonthDays(tmp._year, tmp._month);
    }
    ret += tmp._day - d._day;
    return ret;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章