高等程序设计实验报告总结归纳Word文档下载推荐.docx
《高等程序设计实验报告总结归纳Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《高等程序设计实验报告总结归纳Word文档下载推荐.docx(21页珍藏版)》请在冰豆网上搜索。
4)重载乘法算符*,将两个多项式相乘;
5)重载加法赋值运算符+=、减法赋值运算符-=以及乘法赋值运算符*=。
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&
//返回从公元1年1月1日起的天数
staticDateNumToDate(intn);
//由从公元1年1月1日起的天数返回日期
润年条件:
年份能被4整除,并且年份不能被100整除,或者年份能被400整除
润年天数:
366
平年天数:
365
润年2月份天数:
29
平年2月份天数:
28
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<
iostream>
usingnamespacestd;
classshuzu{
intchessBoard[8][8];
public:
shuzu(){
for(inti=0;
i<
8;
i++)
for(intj=0;
j<
j++)
chessBoard[i][j]=i+j;
}
intoperator()(inta,intb){returnchessBoard[a][b];
}
};
intmain()
{
shuzua;
cout<
"
输出(6,7)="
a(6,7)<
endl;
矩阵:
for(inti=0;
{
for(intj=0;
cout<
a(i,j)<
\t"
;
cout<
getch();
return0;
结果截图:
classJ
private:
inta[5];
J(ints[])
for(inti=0;
5;
a[i]=s[i];
voidpaixu()
4;
{
4-i;
{
if(a[j]>
a[j+1])
{
inttemp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
intoperator[](intb)
returna[b];
ints[5];
请输入5个数:
cin>
s[i];
Jj(s);
();
最大的元素、次最大的元素以及第三大的元素依次为:
j[4]<
"
j[3]<
j[2]<
(1)程序实现
#include"
classDate
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&
intyear,mounth,date,m;
Date:
:
Date(inta,intb,intc)
year=a;
mounth=b;
date=c;
m=1;
voidDate:
Set_Date(inta,intb,intc)
Get_Date()
if(m==1)
year<
年"
mounth<
月"
date<
日"
else
刚才输入的天数不符合要求!
DateDate:
operator+(inta)
if(a>
28)
m=0;
return*this;
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
{
mounth++;
}
}//处理2月
=31)
date=date+a-31;
if(mounth==12)
{
year++;
mounth=1;
}
else
mounth++;
else
=28)
date=date+a-28;
operator-(inta)
if((mounth==5)||(mounth==7)||(mounth==10)||(mounth==12))
if(date>
=a)
date=date-a;
date=date-a+30;
mounth--;
if(mounth==3)
if(date>
date=date-a;
date=date-a+29;
mounth--;
date=date-a+31;
if(mounth==1)
year--;
mounth=12;
mounth--;
date=date-a+28;
ostream&
output,Date&
a)
returnoutput;
istream&
input,Date&
年:
"
cin>
月:
日:
returninput;
{
Datea,b,c;
intx,y,z;
请输入第一个日期:
x;
月:
y;
z;
(x,y,z);
c=a;
请输入第二个日期:
b;
请输入需要加上的天数:
a=a+z;
日期"
c<
加上"
z<
天"
得到的日期为:
a=c;
请输入需要减去的天数:
b<
减去"
b=b-z;
(2)结果截图
classTime
inthour,minute,second,sall;
Time();
Timeoperator+(Timea);
Timeoperator-(Timea);
Timeoperator<
(Time&
a);
Timeoperator>
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;
operator-(Timea)
=;
operator<
minute=;
hour=;
second=;
sall=;
return*this;
operator>
=minute;
=second;
=hour;
=sall;
returna;
voidTime:
SetHour(inthh)//设置小时
hour=hh;
SetMinute(intmm)//设置分钟
minute=mm;
}
SetSecond(intss)//设置秒
second=ss;
sall=3600*hour+60*minute+second;
intTime:
GetHour()const//返回小时
returnhour;
GetMinute()const//返回分钟
returnminute;
GetSecond()const//返回秒
returnsecond;
GetSall()const
returnsall;
Timet1,t2,t3,t4,t5,t6,t7,t8