getch();
return0;
}
结果截图:
4.设计一个日期类Date,,要求:
(1)包含年(year)、月(month)和日(day)私有数据成员。
(2)包含构造函数,重载关于一日期加上天数的加法运算符+、重载关于一日期减去天数的减加运算符-、重载输出运算符<<与输入运算符>>等。
提示:
由于各C++编译器对于重载输入/出运算符为友元的兼容性都存在问题,最好重载输入/出运算符不声明为成员函数与友元函数,而声明一般函数,为编程更方便,可增加一些成员函数,比如:
voidSetYear(inty);//设置年
intSetMonth(intm);//设置月
intSetDay(intd);//设置日
intGetYear()const;//返回年
intGetMonth()const;//返回月
intGetDay()const;//返回日
staticintIsLeapyear(inty);//判断年份y是否为润年
staticintGetDays(inty);//年份y的天数
staticintGetDays(constDate&d);//日期d当前月份的天数
staticintDateToNum(constDate&d);//返回从公元1年1月1日起的天数
staticDateNumToDate(intn);//由从公元1年1月1日起的天数返回日期
润年条件:
年份能被4整除,并且年份不能被100整除,或者年份能被400整除
润年天数:
366
平年天数:
365
润年2月份天数:
29
平年2月份天数:
28
(1)程序实现
#include""
#include<>
classDate
{
public:
Date(inta=0,intb=0,intc=0);
voidSet_Date(inta,intb,intc);
voidGet_Date();
Dateoperator+(int);
Dateoperator-(int);
friendostream&operator<<(ostream&,Date&);
friendistream&operator>>(istream&,Date&);
private:
intyear,mounth,date,m;
};
Date:
:
Date(inta,intb,intc)
{
year=a;
mounth=b;
date=c;
m=1;
}
voidDate:
:
Set_Date(inta,intb,intc)
{
year=a;
mounth=b;
date=c;
}
voidDate:
:
Get_Date()
{
if(m==1)
cout<else
cout<<"刚才输入的天数不符合要求!
"<}
DateDate:
:
operator+(inta)
{
if(a>28)
{
m=0;
return*this;
}
else
{
if((year%400==0)||((year%4==0)&&(year%100!
=0)))
{
if((mounth==4)||(mounth==6)||(mounth==9)||(mounth==11))
{
if((date+a)<=30)
date=date+a;
else
{
date=date+a-30;
mounth++;
}
}
else
{
if(mounth==2)
{
if((date+a)<=29)
date=date+a;
else
{
date=date+a;
mounth++;
}
}//处理2月
else
{
if((date+a)<=31)
date=date+a;
else
{
date=date+a-31;
if(mounth==12)
{
year++;
mounth=1;
}
else
mounth++;
}
}
}
}
else
{
if((mounth==4)||(mounth==6)||(mounth==9)||(mounth==11))
{
if((date+a)<=30)
date=date+a;
else
{
date=date+a-30;
mounth++;
}
}
else
{
if(mounth==2)
{
if((date+a)<=28)
date=date+a;
else
{
date=date+a-28;
mounth++;
}
}
else
{
if((date+a)<=31)
date=date+a;
else
{
date=date+a-31;
if(mounth==12)
{
year++;
mounth=1;
}
else
mounth++;
}
}
}
}
return*this;
}
}
DateDate:
:
operator-(inta)
{
if(a>28)
{
m=0;
return*this;
}
else
{
if((year%400==0)||((year%4==0)&&(year%100!
=0)))
{
if((mounth==5)||(mounth==7)||(mounth==10)||(mounth==12))
{
if(date>=a)
date=date-a;
else
{
date=date-a+30;
mounth--;
}
}
else
{
if(mounth==3)
{
if(date>=a)
date=date-a;
else
{
date=date-a+29;
mounth--;
}
}
else
{
if(date>=a)
date=date-a;
else
{
date=date-a+31;
if(mounth==1)
{
year--;
mounth=12;
}
else
mounth--;
}
}
}
}
else
{
if((mounth==5)||(mounth==7)||(mounth==10)||(mounth==12))
{
if(date>=a)
date=date-a;
else
{
date=date-a+30;
mounth--;
}
}
else
{
if(mounth==3)
{
if(date>=a)
date=date-a;
else
{
date=date-a+28;
mounth--;
}
}
else
{
if(date>=a)
date=date-a;
else
{
date=date-a+31;
if(mounth==1)
{
year--;
mounth=12;
}
else
mounth--;
}
}
}
}
return*this;
}
}
ostream&operator<<(ostream&output,Date&a)
{
cout<<<<"年"<<<<"月"<<<<"日"<returnoutput;
}
istream&operator>>(istream&input,Date&a)
{
cout<<"年:
";
cin>>;
cout<<"月:
";
cin>>;
cout<<"日:
";
cin>>;
returninput;
}
intmain()
{
Datea,b,c;
intx,y,z;
cout<<"请输入第一个日期:
"<cout<<"年:
";
cin>>x;
cout<<"月:
";
cin>>y;
cout<<"日:
";
cin>>z;
(x,y,z);
c=a;
cout<<"请输入第二个日期:
"<cin>>b;
cout<<"请输入需要加上的天数:
"<cin>>z;
a=a+z;
cout<<"日期"<"<();
a=c;
cout<<"请输入需要减去的天数:
"<cin>>z;
cout<<"日期"<
"<b=b-z;
();
getch();
return0;
}
(2)结果截图
5.设计一个时间类Time,要求:
(1)包含时(hour)、分(minute)和秒(second)私有数据成员。
(2)包含构造函数,重载关于一时间加上另一时间的加法运算符+、重载关于一时间减去另一时间的减加运算符-、重载输出运算符<<与输入运算符>>等。
提示:
可仿照第4题编程实现,可将时间转换成秒数,将秒数转成时间进行辅助编程。
时间转换成秒数:
秒数=时*3600+分*60+秒
秒数转换成时间:
时=秒数/3600
分=(秒数-时*3600)/60
秒=秒数%60
为编程更方便,可增加一些成员函数,比如:
voidSetHour(inthh);//设置小时
voidSetMinute(intmm);//设置分钟
voidSetSecond(intss);//设置秒
intGetHour()const;//返回小时
intGetMinute()const;//返回分钟
intGetSecond()const;//返回秒
代码实现:
#include
#include<>
usingnamespacestd;
classTime
{
inthour,minute,second,sall;
public:
Time();
Timeoperator+(Timea);
Timeoperator-(Timea);
Timeoperator<<(Time&a);
Timeoperator>>(Time&a);
voidSetHour(inthh);//设置小时
voidSetMinute(intmm);//设置分钟
voidSetSecond(intss);//设置秒
intGetHour()const;//返回小时
intGetMinute()const;//返回分钟
intGetSecond()const;//返回秒
intGetSall()const;
};
Time:
:
Time()
{
second=0;
hour=0;
minute=0;
sall=0;
}
TimeTime:
:
operator+(Timea)
{
Timetemp;
=sall+;
=3600;
=*/60;
=%60;
returntemp;
}
TimeTime:
:
operator-(Timea)
{
Timetemp;
=;
=3600;
=*/60;
=%60;
returntemp;
}
TimeTime:
:
operator<<(Time&a)
{
minute=;
hour=;
second=;
sall=;
return*this;
}
TimeTime:
:
operator>>(Time&a)
{
=minute;
=second;
=hour;
=sall;
returna;
}
voidTime:
:
SetHour(inthh)//设置小时
{
hour=hh;
}
voidTime:
:
SetMinute(intmm)//设置分钟
{
minute=mm;
}
voidTime:
:
SetSecond(intss)//设置秒
{
second=ss;
sall=3600*hour+60*minute+second;
}
intTime:
:
GetHour()const//返回小时
{
returnhour;
}
intTime:
:
GetMinute()const//返回分钟
{
returnminute;
}
intTime:
:
GetSecond()const//返回秒
{
returnsecond;
}
intTime:
:
GetSall()const
{
returnsall;
}
intmain()
{
Timet1,t2,t3,t4,t5,t6,t7,t8