cout<
return0;
}
输出结果:
3.源代码:
#include
#include
classFenshu
{
private:
intzi;
intmu;
public:
Fenshu(int_zi=0,int_mu=1)
{
zi=_zi;
mu=_mu;
}
~Fenshu(){}
voidsetzi(int_zi=0)
{
zi=_zi;
}
voidsetmu(int_mu=1)
{
mu=_mu;
}
intgetzi()
{
returnzi;
}
intgetmu()
{
returnmu;
}
voidprint();
friendFenshuoperator+(constFenshu&f1,constFenshu&f2);
friendFenshuoperator-(constFenshu&f1,constFenshu&f2);
Fenshuoperator*(intn)
{
Fenshux;
x.zi=zi*n;
x.mu=mu;
returnx;
}
Fenshuoperator/(intn)
{
if(n==0)
{
cout<<"Nagetive!
";
return*this;
}
Fenshux;
x.zi=zi;
x.mu=mu*n;
returnx;
}
Fenshuoperator-()
{
Fenshux;
x.zi=-zi;
x.mu=mu;
returnx;
}
voidoperator=(constFenshu&f)
{
zi=f.zi;
mu=f.mu;
}
};
Fenshuoperator+(constFenshu&f1,constFenshu&f2)
{
Fenshuf;
f.zi=f1.zi*f2.mu+f2.zi*f1.mu;
f.mu=f1.mu*f2.mu;
returnf;
}
Fenshuoperator-(constFenshu&f1,constFenshu&f2)
{
Fenshuf;
f.zi=f1.zi*f2.mu-f2.zi*f1.mu;
f.mu=f1.mu*f2.mu;
returnf;
}
voidFenshu:
:
print()
{
if(zi==0)cout<<0<else
{
int_zi=abs(zi>mu?
zi:
mu),_mu=abs(mumu:
zi);
intx;
x=_zi%_mu;
while(x!
=0)
{
_mu=_zi;
_zi=x;
x=_mu%_zi;
}
zi/=(_zi>_mu?
_mu:
_zi);
mu/=(_zi>_mu?
_mu:
_zi);
cout<}
}
intmain()
{
Fenshux(2,4),y(4,4);
Fenshuz,a,b,c,d,e;
z=x+y;
a=y-x;
b=x-y;
c=-x;
d=y*(-1);
e=y/2;
z.print();
a.print();
b.print();
c.print();
d.print();
e.print();
return0;
}
输出结果:
4.源代码:
#include
usingnamespacestd;
constdoublePI=3.14;
classContainer
{
private:
doubler;
doubleh;
public:
~Container(){}
Container(double_r=0.0,double_h=0.0)
{
r=_r;
h=_h;
}
voidsetr(double_r=0.0)
{
r=_r;
}
doublegetr()
{
returnr;
}
voidseth(double_h=0.0)
{
h=_h;
}
doublegeth()
{
returnh;
}
virtualdoubles()=0;
virtualdoublev()=0;
};
classSphere:
publicContainer
{
public:
~Sphere(){}
Sphere(double_r=0.0):
Container(_r){}
doubles()
{
return4*PI*getr()*getr();
}
doublev()
{
return(double(4)/3)*PI*getr()*getr()*getr();
}
};
classCylinder:
publicContainer
{
public:
~Cylinder(){}
Cylinder(double_r=0.0,double_h=0.0):
Container(_r,_h){}
doubles()
{
return2*PI*getr()*(geth()+getr());
}
doublev()
{
return2*PI*getr()*geth();
}
};
intmain()
{
Container*p;
Spherea
(1);
Cylinderb(1,2);
p=&a;
cout<<"Spheres="<s()<<"v="<v()<p=&b;
cout<<"Cylinders="<s()<<"v="<v()<return0;
}
输出结果:
5.源代码:
#include
usingnamespacestd;
classTime
{
private:
inthour;
intminute;
intsecond;
public:
Time(int=0,int=0,int=0);
~Time(){}
voidsethour(int);
voidsetminute(int);
voidsetsecond(int);
intgethour();
intgetsecond();
intgetminute();
voidset(int=0,int=0,int=0);
voidshow();
Time&operator++(int);
Time&operator--(int);
};
Time:
:
Time(inth,intm,ints)
{
if(s>59)
{
s%=60;
m=m+s/60;
}
if(m>59)
{
m%=60;
h=h+m/60;
}
if(h>23)h%=24;
hour=h;
minute=m;
second=s;
}
voidTime:
:
sethour(inth)
{
if(h>23)h%=24;
hour=h;
}
voidTime:
:
setminute(intm)
{
if(minute>59)
{
minute%=60;
hour=hour+minute/60;
}
if(hour>23)hour%=24;
minute=m;
}
voidTime:
:
setsecond(ints)
{
if(s>59)
{
minute=minute+s/60;
s%=60;
}
if(minute>59)
{
hour=hour+minute/60;
minute%=60;
}
if(hour>23)hour%=24;
second=s;
}
intTime:
:
gethour()
{
returnhour;
}
intTime:
:
getsecond()
{
returnsecond;
}
intTime:
:
getminute()
{
returnminute;
}
voidTime:
:
set(inth,intm,ints)
{
if(s>59)
{
m=m+s/60;
s%=60;
}
if(m>59)
{
h=h+m/60;
m%=60;
}
if(h>23)h%=24;
hour=h;
minute=m;
second=s;
}
voidTime:
:
show()
{
cout<"<"<}
Time&Time:
:
operator++(int)
{
second++;
if(second>59)
{
minute=minute+second/60;
second%=60;
}
if(minute>59)
{
hour=hour+minute/60;
minute%=60;
}
if(hour>23)hour%=24;
return*this;
}
Time&Time:
:
operator--(int)
{
second--;
if(second<0)
{
minute--;
second=59;
}
if(minute<0)
{
minute=59;
hour--;
}
if(hour<0)hour=0;
return*this;
}
intmain()
{
Timet;
intx,y,z;
chartemp;
cout<<"现在初始化计数器(HourMinuteSecond):
";
cin>>x;cin>>y;cin>>z;
t.set(x,y,z);
cout<<"现在时间是:
";
t.show();
do
{
cout<<"输入*重新设置计数器;"<cout<<"输入+计数器递加,输入-计数器递减;"<cout<<"输入字母o计数器清零;"<cout<<"输入q退出计数器。
"<cout<<"请输入>";
cin>>temp;
switch(temp)
{
case'*':
cout<<"现在初始化计数器(HourMinuteSecond):
";
cin>>x;cin>>y;cin>>z;
t.set(x,y,z);
cout<<"现在时间是:
";
t.show();
break;
case'+':
t++;
cout<<"现在时间是:
";
t.show();
break;
case'-':
t--;
cout<<"现在时间是:
";
t.show();
break;
case'o':
t.set(0,0,0);
cout<<"现在时间是:
";
t.show();
break;
case'q':
break;
default:
cout<<"请输入正确的数据!
"<break;
}
}while(temp!
='q');
return0;
}
输出结果:
{
if(sub->getname()==sub_m->getname())
*sub=*sub_m;
sub_m++;
}
sub_m-=num_m;
sub++;
}
sub-=shumu;
}
voidCstudent:
:
insert_s()//录入分数
{
inttemp;
cout<<"Pleaseinputthescores.\n";
for(inti=0;i{
cout<getname()<<':
'<cin>>temp;
sub->setscore(temp);
sub++;
}
sub-=shumu;
}
voidCstudent:
:
dill()
{
intsum=0;doublec=0.0,s=0.0;
for(inti=0;i{
if(sub->getscore()>=95&&sub->getscore()<=100)sub->setpoint(4.5);
elseif(sub->getscore()>=90&&sub->getscore()<=95)sub->setpoint(4.0);
elseif(sub->getscore()>=85&&sub->getscore()<=90)sub->setpoint(3.5);
elseif(sub->getscore()>=80&&sub->getscore()<=85)sub->setpoint(3.0);
elseif(sub->getscore()>=75&&sub->getscore()<=80)sub->setpoint(2.5);
elseif(sub->getscore()>=70&&sub->getscore()<=75)sub->setpoint(2.0);
elseif(sub->getscore()>=65&&sub->getscore()<=70)sub->setpoint(1.5);
elseif(sub->getscore()>=60&&sub->getscore()<=65)sub->setpoint(1.0);
else
{
sub->setpoint(0.0);
sub->setflag(true);
}
sum=sum+sub->getscore();
c=c+sub->getxuefen()*sub->getpoint();
s=s+sub->getxuefen();
sub++;
}
sub-=shumu;
ave_g=double(sum)/shumu;
ave_p=c/s;
}
voidCstudent:
:
print_h()
{
cout<<"IDNamesexclassdateaverageofscoreaverageofpoint\n";
cout<<"--------------------------------------------------------------------------------\n";
}
voidCstudent:
:
print()
{
cout<cout<<"Thescoreofeachsubject:
\n";
cout<<"Namescorepoint"<cout<<"----------------------------\n";
for(inti=0;i{
cout<getname()<<""<getscore()<<""<getpoint()<sub++;
}
sub-=shumu;
cout<<"Theretestsubjectis:
\n";
intkey=0;
for(inti=0;i{
if(sub->getflag()==true)
{
key=1;
cout<getname()<}
sub++;
}
sub-=shumu;
if(key==0)cout<<"none\n";
}
intmain()
{
Cstudent