实验12 运算符重载Word格式文档下载.docx
《实验12 运算符重载Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验12 运算符重载Word格式文档下载.docx(31页珍藏版)》请在冰点文库上搜索。
//判断年份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;
//返回秒
3、实验程序及结果
1、
#include<
iostream>
usingnamespacestd;
classchessBoard
{
private:
inta[2][2];
public:
chessBoard(inty,intb,intc,intd)
{
a[0][0]=y;
a[0][1]=b;
a[1][0]=c;
a[1][1]=d;
}
int&
operator()(intb,intc);
};
int&
chessBoard:
:
operator()(intb,intc)
returna[b][c];
}
intmain()
chessBoardv(1,2,3,4);
v(0,1)=v(1,0);
cout<
v(0,1)<
endl;
return0;
2、
iostream.h>
classxiabiao
xiabiao()//构造函数赋予数组初值
for(inti=0;
i<
10;
i++)
if(i>
5)
jihe[i]=(-1)*2*i;
else
jihe[i]=i+3;
cout<
"
所有元素:
for(i=9;
i>
=0;
i--)
cout<
jihe[i]<
\t"
;
operator[](inta)
intt;
i++)//冒泡排序大的放前面
for(intj=0;
j<
9-i;
j++)
{
if(jihe[j]<
jihe[j+1])
{
t=jihe[j];
jihe[j]=jihe[j+1];
jihe[j+1]=t;
}
}
returnjihe[a-1];
intjihe[10];
xiabiaoxi;
最大的为:
xi[1]<
第二的为:
xi[2]<
第三的为:
xi[3]<
3、
#include<
iomanip.h>
classPolynomial
Polynomial();
Polynomialoperator+(constPolynomial&
)const;
Polynomialoperator-(constPolynomial&
Polynomialoperator*(constPolynomial&
);
Polynomial&
operator+=(constPolynomial&
operator-=(constPolynomial&
operator*=(constPolynomial&
voidEnterTerms();
//输入函数
voidPrintPolynomial()const;
//打印函数
intexponents[100];
intcoefficients[100];
voidpolynomialCombine(Polynomial&
//合并同类项
Polynomial:
Polynomial()
for(inti=0;
i<
100;
i++)//置零
coefficients[i]=0;
exponents[i]=0;
voidPolynomial:
PrintPolynomial()const//输出函数
intstart;
//输出累加系数
boolzero=false;
if(coefficients[0])//常数存在
cout<
coefficients[0];
start=1;
zero=true;
else
if(coefficients[1])
{
coefficients[1]<
'
x'
//常量不存在,输出指数为1的项
if((exponents[1]!
=0)&
&
(exponents[1]!
=1))
^'
exponents[1];
zero=true;
}
start=2;
for(intx=start;
x<
x++)//输出其他各项
if(coefficients[x]!
=0)
setiosflags(ios:
showpos)<
coefficients[x]
<
resetiosflags(ios:
if((exponents[x]!
(exponents[x]!
exponents[x];
if(!
zero)//多项式为空
0'
PolynomialPolynomial:
operator+(constPolynomial&
r)const
Polynomialtemp;
boolexponentExists;
temp.coefficients[0]=coefficients[0]+r.coefficients[0];
//计算常量之和
for(ints=1;
(s<
100)&
(r.exponents[s]!
=0);
s++)
temp.coefficients[s]=r.coefficients[s];
temp.exponents[s]=r.exponents[s];
for(intx=1;
x++)//计算其他各项之和
exponentExists=false;
for(intt=1;
(t<
(!
exponentExists);
t++)
if(exponents[x]==temp.exponents[t])
temp.coefficients[t]+=coefficients[x];
exponentExists=true;
if(!
exponentExists)
temp.exponents[s]=exponents[x];
temp.coefficients[s]+=coefficients[x];
s++;
returntemp;
Polynomial&
operator+=(constPolynomial&
r)
*this=*this+r;
return*this;
operator-(constPolynomial&
r)const
temp.coefficients[0]=coefficients[0]-r.coefficients[0];
(exponents[s]!
{
temp.coefficients[s]=coefficients[s];
temp.exponents[s]=exponents[s];
x++)
if(r.exponents[x]==temp.exponents[t])
temp.coefficients[t]-=r.coefficients[x];
if(!
temp.exponents[s]=r.exponents[x];
temp.coefficients[s]-=r.coefficients[x];
s++;
operator-=(constPolynomial&
r)
*this=*this-r;
operator*(constPolynomial&
ints=1;
for(intx=0;
(x<
(x==0||coefficients[x]!
for(inty=0;
(y<
(y==0||r.coefficients[y]!
y++)
if(coefficients[x]*r.coefficients[y])
if((exponents[x]==0)&
(r.exponents[y]==0))
temp.coefficients[0]+=coefficients[x]*r.coefficients[y];
else
temp.coefficients[s]=coefficients[x]*r.coefficients[y];
temp.exponents[s]=exponents[x]+r.exponents[y];
s++;
polynomialCombine(temp);
//合并同类项
polynomialCombine(Polynomial&
w)
Polynomialtemp=w;
intexp;
w.coefficients[x]=0;
w.exponents[x]=0;
for(x=1;
exp=temp.exponents[x];
for(inty=x+1;
y<
if(exp==temp.exponents[y])
temp.coefficients[x]+=temp.coefficients[y];
temp.exponents[y]=0;
temp.coefficients[y]=0;
w=temp;
operator*=(constPolynomial&
*this=*this*r;
EnterTerms()//初始函数
boolfound=false;
intnumberOfTerms,c,e;
Enternumberofpolynomialterms:
cin>
numberOfTerms;
for(intn=1;
n<
=numberOfTerms;
n++)
Entercoefficient:
//系数
cin>
c;
Enterexponent:
//阶数
e;
if(c!
if(e==0)//初始置零
coefficients[0]+=c;
//常数
continue;
for(intterm=1;
(term<
(coefficients[term]!
term++)//找是否阶数相同
if(e==exponents[term])
coefficients[term]+=c;
exponents[term]=e;
found=true;
found)//阶数不同另外储存
coefficients[term]+=c;
exponents[term]=e;
voidmain()
Polynomiala,b,c,t,d;
a.EnterTerms();
b.EnterTerms();
endl<
Firstpolynomialis:
a.PrintPolynomial();
Secondpolynomialis:
b.PrintPolynomial();
Addingthepolynomialsyields:
c=a+b;
c.PrintPolynomial();
+=thepolynomialsyields:
t=a;
a+=b;
Subtractingthepolynomialsyields:
a=t;
c=a-b;
-=thepolynomialsyields:
a-=b;
Multiplyingthepolynomialsyields:
c=a*b;
*=thepolynomialsyields:
a*=b;
4、
#include"
iostream.h"
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<
日"
刚才输入的天数不符合要求!
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;
date=date+a-30;
mounth++;
if(mounth==2)
if((date+a)<
=29)
date=date+a;
else
{
mounth++;
}
=31)
date=date+a-31;
if(mounth==12)
{
year++;
mounth=1;
}
else
mounth++;
else
=28)
date=date+a-28;