06-构造和析构-
题目描述
下面是一个日期类的定义,请在类外实现其所有的方法,并在主函数中生成对象测试之。
注意,在判断明天日期时,要加入跨月、跨年、闰年的判断
例如9.月30日的明天是10月1日,12月31日的明天是第二年的1月1日
2月28日的明天要区分是否闰年,闰年则是2月29日,非闰年则是3月1日
输入
测试数据的组数t
第一组测试数据的年 月 日
…
要求第一个日期的年月日初始化采用构造函数,第二个日期的年月日初始化采用setDate方法,第三个日期又采用构造函数,第四个日期又采用setDate方法,以此类推。
输出
输出今天的日期
输出明天的日期
#include <iostream>
#include <iomanip>
using namespace std;
class date
{
int year,month,day;
public:
date()
{year=0;month=0;day=0;}
date(int y,int m,int d)
{year=y;month=m;day=d;}
int gety()
{return year;}
int getm()
{return month;}
int getd()
{return day;}
void setdate(int y,int m,int d)
{year=y;month=m;day=d;}
void print()
{
cout<<year<<"/";
if(month<10)
cout<<"0"<<month<<"/";
else
cout<<month<<"/";
if(day<10)
cout<<"0"<<day<<endl;
else
cout<<day<<endl;
}
void addoneday()
{
if(month==1||month==3||month==5||month==7||month==8||month==10)
{
if(day==31)
{
day=1;month++;
}
else
day++;
}
else if(month==12)
{
if(day==31)
{
day=1;month=1;year++;
}
else
day++;
}
else if(month==4||month==6||month==9||month==11)
{
if(day==30)
{
month++;day=1;
}
else
day++;
}
else if(month==2)
{
if(year%4==0)
{
if(day==29)
{
day=1;month++;
}
else
day++;
}
else
{
if(day==28)
{
month++;day=1;
}
else
day++;
}
}
}
};
int main()
{
int t,i;
cin>>t;
for(i=1;i<=t;i++)
{
int y,m,d;
cin>>y>>m>>d;
if(i%2!=0)
{
date d1(y,m,d);
cout<<"Today is ";
d1.print();
cout<<"Tomorrow is ";
d1.addoneday();
d1.print();
}
else
{
date d1;
d1.setdate(y,m,d);
cout<<"Today is ";
d1.print();
cout<<"Tomorrow is ";
d1.addoneday();
d1.print();
}
}
return 0;
}