关闭

C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)

151人阅读 评论(0) 收藏 举报
分类:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stdlib.h>
using namespace std;

class Date
{
public:
    Date(int year=0, int month=0, int day=0)
    :_year(year)
    , _month(month)
    , _day(day)
    {
        cout << "构造函数" << endl;
    }

    Date( const Date& d)
    {
        cout << "拷贝构造函数" << endl;
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    /*Date& operater = (const Date& d)
    {
        cout << "运算符重载" <<endl;

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

     //大于
    bool operator > (const Date& d)
    {
        if (this->_year > d._year)
        {
            return true;
        }
        else
        {
            if (this->_year == d._year)
            {
                if (this->_month > d._month)
                {
                    return true;
                }
                else
                {
                    if (this->_month == d._month)
                    {
                        if (this->_day > d._day)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
        }
    }

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

     //大于等于
    bool operator >= (const Date& d)
    {
        if (this->_year >= d._year)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //小于等于
    bool operator <= (const Date& d)
    {
        if (this->_year <= d._year)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //Date operator+ (int day);
    Date operator+ (int day)
    {
        
    }
    void Display()
    {
        cout << _year << "-" << _month << "-" << _day<<endl;
    }
    private:
        //判断平年闰年
        bool IsLeapYear(int year)
        {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //判断天数
        int GetMonthDay(int year, int month)
        {
            if (year && 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;
            }
        }

        //加天数,Date operator+ (int day)
        Date operator+ (int day)
        {
            int rest = 0;
            if (day >= 0)
            {
                this->_day += day;
                while (this->_day > GetMonthDay(2016, 5))
                {
                    rest = this->_day - GetMonthDay(2016, 2);
                    if (this->_month != 12)
                    {
                        this->_month++;
                    }
                    else
                    {
                        this->_year++;
                        this->_month = 1;
                    }
                    this->_day = rest;
                }
            }
            else
            {
                Date operator - 50;
            }
            return *this;
        }


    //加天数,Date operator+= (int day);
    Date operator+= (int day )
    {
        Date tmp(*this);
        tmp._day += day;
        int rest = 0;
        if (day > 0)
        {
            while (tmp._day > GetMonthDay(2016, 5))
            {
                rest = tmp._day - GetMonthDay(2016, 2);
                if (tmp._month != 12)
                {
                    tmp._month++;
                }
                else
                {
                    tmp._year++;
                    tmp._month = 1;
                }
                tmp._day = rest;
            }
        }
        else
        {
            Date operator-= 50;
        }
        return tmp;

    }

    //减天数,Date operator- (int day);
    Date operator - (int day)
    {
        int rest = 0;
        if (day <= 0)
        {
            this->_day -= day;
            while (this->_day > GetMonthDay(2016, 5))
            {
                rest = this->_day - GetMonthDay(2016, 2);
                if (this->_month != 1)
                {
                    this->_month--;
                }
                else
                {
                    this->_year--;
                    this->_month = 12;
                }
                this->_day = rest;
            }
        }
        else
        {
            Date operator + 50;
        }
        return *this;
    }

    //减天数,Date operator-= (int day);
    Date operator-= (int day)
    {
        int rest = 0;
        Date tmp(*this);
        if (day <= 0)
        {
            tmp._day -= day;
            while (tmp._day > GetMonthDay(2016, 5))
            {
                rest = tmp._day - GetMonthDay(2016, 2);
                if (tmp._month != 1)
                {
                    tmp._month--;
                }
                else
                {
                    tmp._year--;
                    tmp._month = 12;
                }
                tmp._day = rest;
            }
        }
        else
        {
            Date operator + 50;
        }
        return tmp;
    }

    //前置++
    Date operator++()
    {
        if (day > GetMonthDay(int year, int month))
        {
            if (month != 12)
            {
                ++this->day;
            }
            else
            {
                ++year;
                month = 1;
                day = 1;
            }
        }
        else
        {
            ++day;
        }
        return *this;
    }

    //后置++
    Date operator++(int)
    {
        Date tmp(*this);
        tmp._day;
        if (day > GetMonthDay(int year, int month))
        {
            if (month != 12)
            {
                ++tmp.day;
            }
            else
            {
                ++tmp.year;
                tmp.month = 1;
                tmp.day = 1;
            }
        }
        else
        {
            ++tmp.day;
        }
        return tmp;
    }

    //前置--
    Date operator++()
    {
        if (day > GetMonthDay(int year, int month))
        {
            if (month != 1)
            {
                ++this->day;
            }
            else
            {
                --year;
                month = 12;
                day = 1;
            }
        }
        else
        {
            --day;
        }
        return *this;
    }

    //后置--
    Date operator--(int)
    {
        Date tmp(*this);
        tmp._day;
        if (day > GetMonthDay(int year, int month))
        {
            if (month != 1)
            {
                --tmp.day;
            }
            else
            {
                --tmp.year;
                tmp.month = 12;
                tmp.day = 1;
            }
        }
        else
        {
            --tmp.day;
        }
        return tmp;
    }
    ~Date()
    {
        cout << "析构函数" << endl;
    }

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

void Test1()
{
    Date d;
    d.Display();
    Date ret = operator + (50);
}
int main()
{
    Test1();
    system("pause");
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:49908次
    • 积分:612
    • 等级:
    • 排名:千里之外
    • 原创:174篇
    • 转载:9篇
    • 译文:0篇
    • 评论:2条
    关于博主
    github:https://github.com/hanxiaojing Email: hanjing_1995@163.com 原51.cto: http://10740184.blog.51cto.com 欢迎来访~
    文章分类
    最新评论