### 题目描述

#include <iostream>
using namespace std;
class Date
{
public:
void input(int y,int m,int d);
friend Date operator+(Date &c,int &day);
void display();
private:
int year;
int month;
int day;
};

void Date::input(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
Date operator+(Date &c,int &day)
{
/*********************/

填空部分

/*********************/
}

void Date::display()
{
cout<<year<<"/"<<month<<"/"<<day<<endl;
}
int main()
{
Date date1,date2;
int y,m,d;
int day;
cin>>y>>m>>d;
date1.input(y,m,d);
cin>>day;
date2=date1+day;
date2.display();
return 0;
}

2008 2 29
140

2008/7/18

### 代码如下

``````#include <iostream>
using namespace std;
class Date
{
public:
void input(int y,int m,int d);
friend Date operator+(Date &c,int &day);
void display();
private:
int year;
int month;
int day;
};

void Date::input(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
Date operator+(Date &c,int &day)
{
Date date;
date=c;
date.day+=day;
while(date.month>12||date.day>28)
{
switch(date.month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(date.day>31)
{
date.month++;
date.day-=31;
}
break;
case 4:
case 6:
case 9:
case 11:
if(date.day>30)
{
date.month++;
date.day-=30;
}
break;
case 2:
if((date.year%4==0&&date.year%100!=0)||date.year%400==0)
{
if(date.day>29)
{
date.month++;
date.day-=29;
}
}
else
{
if(date.day>28)
{
date.month++;
date.day-=28;
}
}
break;
}
if(date.month>12)
{
date.year++;
date.month-=12;
}
if(((date.year%4==0&&date.year%100!=0)||date.year%400==0)&&date.month==2&&date.day<=29)
break;
else if(date.month==2&&date.day<=28)
break;
else if((date.month==4||date.month==6||date.month==9||date.month==11)&&date.day<=30)
break;
else if(date.day<=31)
break;
}
return date;
}

void Date::display()
{
cout<<year<<"/"<<month<<"/"<<day<<endl;
}
int main()
{
Date date1,date2;
int y,m,d;
int day;
cin>>y>>m>>d;
date1.input(y,m,d);
cin>>day;
date2=date1+day;
date2.display();
return 0;
}``````