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

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
易通电脑锁可以控制上网时间、控制电脑使用时间、定时多任务(关机/锁机/重启/打开或关闭程序/删除文件等)、记录电脑开关机时间和使用时间、记录文件操作日志(新建/复制/删除等)。各种时间控制方式随意组合,做到了真正意义上的随心所欲的时间控制管理,是您电脑安全和使用管理的理想选择。 易通电脑锁功能: 一、灵活的电脑使用时间和上网时间管理 通过时间分段和累计使用时间控制方式对电脑使用时间和上网时间进行全方位的控制管理。时间分段控制提供了7 X 24小时时间控制表,精确到半小时的时间片段,可随意设置每天电脑使用时间和上网时间段。时间段状态提供了正常使用状态、禁止上网状态、电脑锁定状态、电脑关机状态供用户选择。累计使用时间控制提供了每天累计使用电脑总时间和上网总时间表,可随意设置每天使用电脑的总时间或上网总时间。控制状态提供了禁止上网状态、电脑锁定状态、电脑关机状态供用户选择。 二、全面的定时控制操作 提供了自定、累计、延时、开机、系统无操作五种定时方式。定时任务提供了定时关机、重启、注销、锁定、待机、休眠、提醒、打开或关闭程序、删除文件/文件夹、断开网络拔号连接等操作任务。 三、贴心的定时多任务管理 可随意按每年、每月、每周、每天、每小时等定时方式来制定定时任务。定时任务提供了定时关机、重启、注销、锁定、待机、休眠、提醒、打开或关闭程序、删除文件/文件夹、断开网络拔号连接等操作任务。 四、详细的日志记录与查询 系统开关机日志记录了计算机的开关机时间和使用时间等,可以让你方便详细地了解到您计算机的使用时间情况;文件操作日志记录了文件的新建/复制/删除等操作,可以让你全面的了解到整个系统的文件操作情况。 五、方便的系统辅助功能 方便的一键控制操作,可以设置一键关机/重启/注销/锁定计算机的热键。还有快速、安全、全面的IE/系统常见故障修复功能。 易通电脑锁 v7.8.3.6727更新内容: 改进时间分段控制管理功能。 易通电脑锁截图
天天万年历是一款最全面最实用最具个性的运行于WINDOWS平台下的桌面时钟、月历、日历且集成了中国传统计时老黄历信息功能,阴阳历法对照全面的万年历软件。   天天万年历具体功能: 一.阴阳历日历 1、公历日期,星期,以秒计时、公历节日等; 2、农历日期、当前四柱显示; 3、老皇历功能,显示当年太岁(属相)及犯太岁的属相;显示日属相,且显示日冲属相,当日甲子旬空; 4、节气标识,如果当天为节气,将会在日历中标识; 5、显示在屏幕右下角,可隐藏,隐藏时鼠标移至右下角时间处将自动显示; 6、整点弹出提示; 7、界面图像可任意自定义,在软件的设置中即可设定自己的天天万年历日历风格;  8、显示三九、三伏的当前天数及入梅和出梅标识; 二、传统万年历 1、显示阴阳历日历中所显示的公历和农历日期; 2、农历四柱及四柱的十二宫、地支暗藏、甲子纳音; 3、分析太岁(年上)、月份、节气、日主、时辰的吉凶; 4、集成今日宜忌功能; 5、根据日期显示当日吉神方位; 6、二十八宿及其释义; 7、彭祖百忌功能; 8、集成年、月、日、时四家紫白星(九星); 9、分析日家凶星、日家吉神; 10、集成时家吉神吉时; 11、阴阳历互转互查; 12、显示当前阳历年份的进伏、入梅、出梅、进九具体日期 三、日历界面支持自定义,软件自带(古典、卡通、男性、女性、商务、学习)六大主题供不同型的用户使用; 四、支持在线实时更新; 五、万年历支持年限:1900-2049年; 六、软件使用完全免费;   天天万年历 v7.5.0.0版本更新: 优化了日历显示颜色 增加了月历的年月选择功能; 增加了择吉万年历日期快速选择功能; 优化了快捷桌面功能,速度更快,方便易用,用户拥有本软件可以直接删除桌面上的图标,或者直接隐藏掉桌面图标; 增加了快捷桌面功能的刷新功能,当用户在系统安装了新软件之后,点击此按钮可以刷新快捷桌面使新程序快捷方式直接更新到天天万年历的快捷桌面上来; 增加了快捷桌面右链菜单功能; 增加了程序启动后直接右下角显示; 增加了隐藏按钮,无论在哪个位置,将自动隐藏到系统托盘时间区,当用户鼠标进入即可显示供用户查阅操作; 优化了程序移动到屏幕顶部将自动隐藏在顶部且置顶静候服务; 优化了隐藏时的提示,让用户不再因找不到本程序而着急了; 修复了软件的菜单显示错位的BUG;  修复了数据管理桌面图标删除后,前台快捷桌面不自动更新,且图标显示出错的BUG; 修复了所有已知BUG;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值