class date
{
public:
date(){};
date(int m,int d,int y)
{
month=m;
day=d;
year=y;
};
void print()
{
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
};
date operator+(int);
private:
int month,day,year;
};
static int days[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int isleap(int year)
{
if(((year%4==0)&&year%100!=0) || (year%400==0))
return 1;
else
return 0;
}
date date::operator + (int n)
{
int leap;
leap=0;
leap=isleap(this->year);
n+=this->day;
while(n>days[leap][this->month-1])
{
n-=days[leap][this->month-1];
if(++(this->month)==13) //判断月份是不是到了13月
{
this->month=1; //将月份重置为1
(this->year)++; //年份加1
leap=isleap(this->year); //判断新的一年是不是闰年
}
}
this->day=n; //剩下的天数赋给day
return *this;
}
void main()
{
date d1(5,20,2014),d2;
d1.print();
int n;cout<<"请输入一个要加的天数:";cin>>n;
d2=d1+n;
cout<<"\n"<<n<<"天后是:";
d2.print();
}