运算符重载实现日期推算

程序的功能及代码

此程序实现了三个功能:

  1. 通过输入一个日期和一个天数,计算此日期加上天数(向后推算)是哪个日期。
  2. 通过输入一个日期和一个天数,计算此日期减去天数(向前推算)是哪个日期。
  3. 通过输入两个日期,计算两个日期之间差几天。
    程序如下所示:(测试结果在代码后边,附程序压缩包)
#include<iostream>
using namespace std;

class Date
{
public:
    int GetMonthDay(int year, int month)
    {
        static int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int day = days[month];
        if((month == 2)&&(((year % 4 == 0)&&(year % 100 != 0))||(year%400 ==0)))
        {
            day+=1;
        }
        return day;
    }
    Date(int year = 2000, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

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

    Date& operator+=(int day)//+=运算符重载
    {
        if(day < 0)
        {
            return *this -= -day;
        }
        _day += day;
        while(_day > GetMonthDay(_year,_month))
        {
            _day -=GetMonthDay(_year,_month);
            _month++;
            if(_month == 13)
            {
                _year++;
                _month = 1;
            }
        }
        return *this;
    }
    
    Date& operator-=(int day)//-=运算符重载
    {
        if(day < 0)
        {
            return *this += -day;
        }
        _day -= day;
        while(_day < 0)
        {
            
            _month--;
            if(_month == 0)
            {
                _year--;
                _month = 12;
            }
            _day +=GetMonthDay(_year,_month);
        }
        return *this;
    }

    Date& operator=(const Date& d)//赋值运算符重载
    {
        if(this !=&d)
        {
            _year = d._year;
            _month = d._month;
            _day = d._day;
        }
    }
    
    Date& operator++()//前置++运算符重载
    {
        *this+=1;
        return *this;
    }
     Date operator++(int)//后置++运算符重载
    {
        Date ret(*this);
        *this +=1;
        return ret;
    }
    
    bool operator<(const Date& d)
    {
        if(_year < d._year)
        {
            return 1;
        } 
        else if (_year == d._year)
        {
            if(_month < d._month)
            {
                return 1;
            }
            else if(_month == d._month)
            {
                if(_day < d._day)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }

    int operator-(const Date&d)//-运算符重载,用于计算日期差
    {
       Date max = *this;
       Date min = d;
       if(*this < d)
       {
        max = d;
        min = *this;
       }
       int day =0;
       while(min <max)
       {
        ++(min);
        ++day;
       }
       return day;
    }
    int _year;
    int _month;
    int _day;
};

int main()
{
    //Date d1(2024,9,5);
    int i,j,k;
    
    cout <<"请输入初始日期的年"<<'\n';
    cin>>i;
    cout <<"请输入初始日期的月"<<'\n';
    cin>>j;
    cout <<"请输入初始日期的日"<<'\n';
    cin>>k;
    Date d1(i,j,k);
    cout <<"您输入的日期为:"<<'\t'<< d1._year << '-' << d1._month << '-' << d1._day <<endl;
    int select;
    cout <<"请输入需要进行的操作"<<'\n';
    cout <<"1.日期加运算"<<'\n';
    cout <<"2.日期减运算"<<'\n';
    cout <<"3.计算日期差"<<'\n';
    cin >>select;
    if(select == 1)
    {
        int day1;
        cout <<"请输入需要加的天数"<<endl;
        cin >> day1;
        d1+=day1;
        cout << d1._year << '-' << d1._month << '-' << d1._day <<endl;
    }
    else if (select ==2)
    {
        int day2;
        cout <<"请输入需要减的天数"<<endl;
        cin >> day2;
        d1-=day2;
        cout << d1._year << '-' << d1._month << '-' << d1._day <<endl;
    }
    else if(select == 3)
    {
        int i1,j1,k1;
        
        cout <<"请输入目标日期年"<<'\n';
        cin>>i1;
        cout <<"请输入目标日期月"<<'\n';
        cin>>j1;
        cout <<"请输入目标日期日"<<'\n';
        cin>>k1;
        Date d2(i1,j1,k1);
        cout <<"您输入的日期为:"<<'\t'<< d2._year << '-' << d2._month << '-' << d2._day <<endl;
        int ret = d1-d2;
        cout <<"两个日期只差为:"<<' ' << ret << "天" <<endl;
    }
    else
    {
        cout << "您的输入有误"<<endl;
    }
    return 0;
}

日期加测试

在这里插入图片描述
首先测试从今天(2024-9-5)向后推算200天,得到结果为2025-3-24,通过验证所测试结果正确。
在这里插入图片描述
其次测试了当天数为-200天时,即向前推算200天,得到结果为2024-2-18,经过验证结果正确。

日期减测试

在这里插入图片描述
日期减去200天,即为日期加-200天,得到结果为2024-2-18,结果正确。
在这里插入图片描述
日期减-200天,即对应日期加200天,得到结果为2025-3-24,结果正确。

两日期的天数差测试

在这里插入图片描述
由上述两个结果可以得到2024-9-5到2025-3-24之间差200天,通过程序测试得到相差200天,则结果正确。

在这里插入图片描述
在这里插入图片描述
通过验证,结果正确。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪好像下整夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值