Date类重载运算符

Date类重载运算符

acm题:
设计日期类Date,成员数据包含年(year)月(month)日(day)。根据给定的main函数设计必要的成员函数。main函数已给定,提交时只需要提交main函数外的代码部分。
下面是要求的主函数:

int main()
{
    Date d1,d2(2008,8,8);
    Date d3,d4;
    int year1,month1,day1,year2,month2,day2,n;
    cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
    while(cin>>year1>>month1>>day1>>year2>>month2>>day2)
    {
        d1.Set(year1,month1,day1);     d2.Set(year2,month2,day2);
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        d3=--d1;   d4=++d2;
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        cout<<"Date 3:"<<d3<<"    Date 4:"<<d4<<endl;
        cin>>year1>>month1>>day1>>year2>>month2>>day2;
        d1.Set(year1,month1,day1);     d2.Set(year2,month2,day2);
        d3=d1--;   d4=d2++;
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        cout<<"Date 3:"<<d3<<"    Date 4:"<<d4<<endl;
        cin>>n;
        d3=d1+n;    d4=d2-n;
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        cout<<"Date 3:"<<d3<<"    Date 4:"<<d4<<endl;
        cin>>n;
        d3=d1+n;    d4=d2-n;
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        cout<<"Date 3:"<<d3<<"    Date 4:"<<d4<<endl;
    }
    return 0;
}

Input
包含多组数据(数据均正确合法)
每组数据包含4行,第1,2行和包括6个正整数,前3个整数表示一个日期的年月日,后面3个整数表示一个日期的年月日;第3,4行各包括1个正整数。
Output
见Sample Output

Sample Input :

2012 3 1 2015 12 31
2012 3 1 2015 12 31
50
500

Sample Output

Date 1:2015520日    Date 2:200888日
Date 1:201231日    Date 2:20151231日
Date 1:2012229日    Date 2:201611日
Date 3:2012229日    Date 4:201611日
Date 1:2012229日    Date 2:201611日
Date 3:201231日    Date 4:20151231日
Date 1:2012229日    Date 2:201611日
Date 3:2012419日    Date 4:20151112日
Date 1:2012229日    Date 2:201611日
Date 3:2013713日    Date 4:2014819

下面是程序:

#include<iostream>
using namespace std;

class Date
{
private:
    int year,month,day;
public:
    Date():year(2015),month(5),day(20)
    {  }
    Date(int y,int m,int d)
    {
        year=y;month=m;day=d;
    }
    Date(const Date &d)
    {
        year=d.year;month=d.month;day=d.day;
    }
    void Set(int y,int m,int d)
    {
        year=y;month=m;day=d;
    }
    int GetYear()
    { return year; }
    int GetMonth()
    { return month; }
    int GetDay()
    { return day; }
    int IsMonth(int y,int m)
    {
        int montharray[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(m==2 && ((y%4==0&&y%100!=0)||y%400==0)) return 29;
        return montharray[m];
    }
    Date &operator--();//前置
    Date operator--(int);//后置
    Date &operator++();//前置
    Date operator++(int);//后置
    Date operator+(int);
    Date operator-(int);
};
Date &Date::operator--()//前置
{
    day--;
    if(day<=0)
    {
        month--;
        if(month<=0)
        {
            month+=12;
            year--;
        }
        day+=IsMonth(year,month);//注意:和前置++不同的地方
    }
    return *this;
}
Date Date::operator--(int)//后置,不可加引用&
{
    Date old=*this;
    --(*this);
    return old;
}
Date &Date::operator++()//前置
{
    day++;
    if(day>IsMonth(year,month))
    {
        day-=IsMonth(year,month);//注意和前置--作比较
        month++;
        if(month>12)
        {
            month-=12;
            year++;
        }
    }
    return *this;
}
Date Date::operator++(int)//后置,不可加引用&
{
    Date old=*this;
    ++(*this);
    return old;
}
Date Date::operator+(int n)
{
    if(n<0)
        return *this-(-n);
    int y=year,m=month,d=day;
    d+=n;
    while(d>IsMonth(y,m))
    {
        d-=IsMonth(y,m);//同前置++类似
        m++;
        if(m>12)
        {
           m=1;
           y++;
        }

    }
    return Date(y,m,d);
}
Date Date::operator-(int n)
{
    if(n<0)
        return *this+(-n);
    int y=year,m=month,d=day;
    d-=n;
    while(d<=0)
    {
        --m;
        if(m==0)
        {
           m=12;
           --y;
        }
        d+=IsMonth(y,m);//同前置--类似
    }
    return Date(y,m,d);
}
//<<只能定义为非成员函数
ostream &operator<<(ostream &out,Date &d)
{
    out<<d.GetYear()<<"年"<<d.GetMonth()<<"月"<<d.GetDay()<<"日";
    return out;
}
int main()
{
    Date d1,d2(2008,8,8);
    Date d3,d4;
    int year1,month1,day1,year2,month2,day2,n;
    cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
    while(cin>>year1>>month1>>day1>>year2>>month2>>day2)
    {
        d1.Set(year1,month1,day1);     d2.Set(year2,month2,day2);
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        d3=--d1;   d4=++d2;
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        cout<<"Date 3:"<<d3<<"    Date 4:"<<d4<<endl;
        cin>>year1>>month1>>day1>>year2>>month2>>day2;
        d1.Set(year1,month1,day1);     d2.Set(year2,month2,day2);
        d3=d1--;   d4=d2++;
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        cout<<"Date 3:"<<d3<<"    Date 4:"<<d4<<endl;
        cin>>n;
        d3=d1+n;    d4=d2-n;
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        cout<<"Date 3:"<<d3<<"    Date 4:"<<d4<<endl;
        cin>>n;
        d3=d1+n;    d4=d2-n;
        cout<<"Date 1:"<<d1<<"    Date 2:"<<d2<<endl;
        cout<<"Date 3:"<<d3<<"    Date 4:"<<d4<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值