C++程序设计习题.docx

上传人:b****3 文档编号:4638416 上传时间:2022-12-07 格式:DOCX 页数:36 大小:162.61KB
下载 相关 举报
C++程序设计习题.docx_第1页
第1页 / 共36页
C++程序设计习题.docx_第2页
第2页 / 共36页
C++程序设计习题.docx_第3页
第3页 / 共36页
C++程序设计习题.docx_第4页
第4页 / 共36页
C++程序设计习题.docx_第5页
第5页 / 共36页
点击查看更多>>
下载资源
资源描述

C++程序设计习题.docx

《C++程序设计习题.docx》由会员分享,可在线阅读,更多相关《C++程序设计习题.docx(36页珍藏版)》请在冰豆网上搜索。

C++程序设计习题.docx

C++程序设计习题

C++程序设计

练习题

 

学院:

计算机学院

专业班级:

物联网1002

学号:

0121010340705

姓名:

第八章

1.下面是一个类的测试程序,试设计出能是用如下测试程序的类.

Intmain()

{

Testx;

x.initx(30,20);

x.printx();

return0;

}

解:

#include

usingnamespacestd;

classTest//类的开始

{

public:

voidinitx(inti,intj);

voidprintx();

private:

intx,y;

};

voidTest:

:

initx(inti,intj)

{

x=i;

y=j;

}

voidTest:

:

printx()

{

cout<

}//类的结束

intmain()//测试函数

{

Testx;

x.initx(30,20);

x.printx();

return0;

}

得到的测试结果:

4.定义并实现一个矩形类Crectangle。

该类包含了下列成员函数。

Crectangle():

累的构造函数,根据需要可以定义多个构造函数

SetTop(),SetLeft():

设置矩形的左上角坐标

SetLength(),SetWidth():

设置矩形的长和宽

Perimeter():

求矩形的周长

Area():

求矩形的面积

GetWidth():

返回矩形的宽度

Getlength():

返回矩形的长度

IsSquare():

判断矩形是否为正方形

Move():

将矩形从一个位置移动到另一个位置

Size():

改变矩形的大小

Where():

返回矩形的左上角的坐标

PrintRectangle():

输出矩形的四个顶点的坐标

数据成员

inttop,left;

intlength,width;

解:

#include

usingnamespacestd;

classCrectangle//类的开始

{

inttop,left;

intlength,width;

public:

Crectangle(intt=0,intl=0,intlen=1,intw=1)

{

top=t;left=l;

if(len>0)length=len;

elselength=0;

if(w>0)width=w;

elsewidth=0;

}

voidSetTop(intt)

{

top=t;

}

voidSetLeft(intl)

{

left=l;

}

voidSetLength(intlen)

{

length=len;

}

voidSetWidth(intw)

{

width=w;

}

intPerimeter()

{

return2*(width+length);

}

intArea()

{

returnwidth*length;

}

intGetWidth()

{

returnwidth;

}

intGetLength()

{

returnlength;

}

intIsSquare()

{

if(width==length)

{

cout<<"该矩形是正方形"<

return1;

}

else

{

cout<<"该矩形不是正方形"<

return0;

}

}

voidMove(intx,inty)

{

left+=x;

top+=y;

}

voidSize(intl,intw)

{

length=l;

width=w;

}

voidPrintRectangle()

{

cout<<"左上方的点为:

("<

cout<<"右上方的点为:

("<

cout<<"左下方的点为:

("<

cout<<"右下方的点为:

("<

}

};//类的结束

intmain()

{

Crectanglea(1,1,5,5);

a.PrintRectangle();

a.SetTop

(2);

a.SetLeft(3);

a.SetLength(4);

a.SetWidth(5);

a.IsSquare();

cout<<"周长为:

"<

cout<<"面积为:

"<

a.PrintRectangle();

cout<<"用getwidth等函数得到的值为:

"<

cout<<"长为:

"<

cout<<"宽为:

"<

return0;

}

得到的测试结果:

6.某次歌手大赛中,有JudgeNum个评委给选手打分,参加比赛的选手有PlayerNum

名,现为比赛积分编写一个CompetitionResult类,类的定义如下:

(定义略)

(1)写出所有的成员函数和实现代码。

(2)编写main()函数对该类进行测试。

在函数体中,定义了一个competitionResult类的对象数组r[PlauerNum],他的每个元素记录了每个选手的所有信息,个评委的打分通过键盘的输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到底排列输出。

解:

(1)CompetitionResult:

:

CompetitionResult()

{

num=0;strcpy(name,"");

for(inti=0;i

score[i]=0.0;

average=0;

}

CompetitionResult:

:

CompetitionResult(shortn,char*ps)

{

num=n;strcpy(name,ps);

for(inti=0;i

score[i]=0.0;

average=0;

}

floatCompetitionResult:

:

MaxScore()

{intmax=score[0];

for(inti=0;i

if(max

returnmax;

}

floatCompetitionResult:

:

MinScore()

{intmin=score[0];

for(inti=0;i

if(min>score[i])min=score[i];

returnmin;

}

voidCompetitionResult:

:

SetAvg()

{inti;floatsum=0.0;

for(i=0;i

sum+=score[i];

sum=sum-MaxScore()-MinScore();

average=sum/(JudgeNum-2);

}

floatCompetitionResult:

:

GetAvg()

{returnaverage;

}

shortCompetitionResult:

:

GetNo(inti){returnnum;}

voidCompetitionResult:

:

SetNo(inti){num=i;}

charCompetitionResult:

:

*GetName(){returnname;}

floatCompetitionResult:

:

GetScore(intj){returnscore[j];}

voidCompetitionResult:

:

Setscore(intk,floatav){score[k]=av;}

voidSort(CompetitionResult*pr,intn)

{

inti,j,k;

CompetitionResulttemp;

for(i=0;i

{

k=i;

for(j=i+1;j

if((pr[j].average)>(pr[k].average))

k=j;

if(k!

=j)

{

temp=pr[i];

pr[i]=pr[k];

pr[k]=temp;

}

}

}

(2)

intmain()

{

CompetitionResultplayer[PlayerNum]={CompetitionResult(1,"one"),

CompetitionResult(2,"two"),CompetitionResult(3,"there"),

CompetitionResult(4,"four"),CompetitionResult(5,"five"),

CompetitionResult(6,"six"),CompetitionResult(7,"seven"),

CompetitionResult(8,"eight"),CompetitionResult(9,"nine"),

CompetitionResult(10,"ten")};

floata;

for(inti=0;i

{

cout<<"请评委给第"<

"<

for(intj=0;j

{cin>>a;

player[i].Setscore(j,a);

}

player[i].SetAvg();

cout<

}

Sort(player,PlayerNum);

cout<<"最后的得分情况为:

"<

for(intj=0;j

{cout<

cout<

cout<<""<

}

return0;

}

程序运行的结果为:

第九章

1.在下面的程序中,派生类Derived的成员函数sets()能否访问基类base中得变量a和b?

为什么?

如果在基类base中将数据成员a和b定义为私有成员,下面的程序能否通过编译?

为什么?

#include

usingnamespacestd;

classBase{

public:

voidset(inti,intj){a=i;b=j;}

voidshow(){cout<<"a="<

protected:

inta,b;

};

classDerived:

publicBase{

public:

voidsets(){s=a*b;}

voidshows(){cout<<"s="<

private:

ints;

};

intmain(){

Derivedobj;

obj.set(2,3);

obj.show();

obj.sets();

obj.shows();

return0;

}

解:

可以访问a和b;因为a,b为保护成员,经公有继承,成为派生类中得保护成员,所以派生类Derived的成员函数sets()能访问基类base中得变量a和b。

如果定义为私有,则不能通过编译。

因为派生类中的成员函数不能访问基类中得私有成员

2.声明一个图形基类Shape,在它的基础上派生出矩形类Rectangle和圆形类Circle,它们都有计算面积的和周长、输出图形信息的成员函数,再在Rectangle类的基础上派生方形类Square。

编写程序和各类的定义和实现,以及类的使用。

解:

#include

usingnamespacestd;

classShape{

public:

doublegetArea(){}

doublegetPerimeter(){}

};

classRectangle:

publicShape{

protected:

doubleheight;

doublewidth;

public:

Rectangle(){}

Rectangle(doublea,doubleb)

{

height=a;

width=b;

}

doublegetArea()

{returnheight*width;

}

doublegetPerimeter()

{return2*(height+width);

}

};

classCircle:

publicShape{

public:

Circle(doublex):

r(x){}

doublegetPerimeter()

{

return2*3.1416*r;

}

doublegetArea()

{

returnr*r*3.1416;

}

private:

doubler;

};

classSquare:

publicRectangle

{

public:

Square(doublex)

{

height=x;

width=x;

}

};

intmain()

{

Rectanglea1(2.2,3.2);

Circlea2(4.2);

Squarea3(5.3);

cout<<"Area="<

cout<<"Perimeter="<

cout<<"Area="<

cout<<"Perimeter="<

cout<<"Area="<

cout<<"Perimeter="<

return0;

}

5.某销售公司有销售经理和销售员工,月工资的计算办法为:

销售激励的底薪为4000元,并将销售额的2/1000做提成,销售员工无底薪,只提取5/1000为工资。

编写程序:

(1)定义一个积累Employee,它包含三个数据成员number,name和salary,以及编号和姓名的构造函数。

(2)有Employee类派生Salesman类。

Salesman类包含两个新数据成员commrate和sales,还包括了用于输入销售额并计算销售员工工资的成员函数pay()和用于输出打印的print()。

(3)由Salesman派生Salemanager,Salesmmanage类包含新数据成员monthlypay,以及用于输入销售额并计算销售经理工资的成员函数pay()和用于输出的print()。

(4)编写main()函数中测试所设计的类结构。

解:

(1)classEmployee

{

public:

Employee(longnum,char*n)

{

number=num;

strcpy(name,n);

}

protected:

longnumber;

charname[20];

doublesalary;

};

(2)classSalesman:

publicEmployee

{

public:

Salesman(longnum,char*n,floatc=0.005):

Employee(num,n)

{commrate=c;}

voidpay()

{

cout<<"ÇëÊäÈë"<

";

cin>>sales;

salary=commrate*sales;

}

voidprint(){

cout<

}

protected:

floatcommrate;

doublesales;

};

(3)

classSalesmanager:

publicSalesman

{

public:

Salesmanager(longnum,char*n,floatc=0.002,doublem=4000):

Salesman(num,n,c){

monthlypay=m;}

voidpay(){

cout<<"ÇëÊäÈë"<

cin>>sales;

salary=monthlypay+commrate*sales;

}

voidprint(){

cout<

}

private:

doublemonthlypay;

};

(4)intmain()

{

Salesmans1(1234,"ÀîÈð");

Salesmanagers2(1357,"ÎÄÇ¿");

s1.pay();

s1.print();

s2.pay();

s2.print();

return0;

}

 

第十章

1.定义一个辅助类Complex,重载运算符+、-、*、/使之能用于复数的加减乘除运算。

运算符重载函数为Complex类的成员函数。

编写程序,分别求出两个复数之和、差、积、商。

解:

#include

usingnamespacestd;

classComplex

{

public:

Complex(doubler=0.0,doublei=0.0){real=r;imag=i;}

Complexoperator+(Complex);

friendComplexoperator-(Complex,Complex);

Complexoperator*(Complex);

friendComplexoperator/(Complex,Complex);

voiddisplay();

private:

doublereal,imag;

};

ComplexComplex:

:

operator+(Complexc)

{

returnComplex(real+c.real,imag+c.imag);

}

Complexoperator-(Complexc1,Complexc2)

{

returnComplex(c1.real-c2.real,c1.imag-c2.imag);

}

ComplexComplex:

:

operator*(Complexc)

{

returnComplex(real*c.real-imag+c.imag,imag*c.imag+real*c.real);

}

Complexoperator/(Complexc1,Complexc2)

{

doubler,i;

r=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

i=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

return(r,i);

}

voidComplex:

:

display()

{

cout<<"("<

}

intmain()

{

Complexc1(7.1,8.4),c2(1.4,5),c3;

cout<<"c1=";c1.display();

cout<<"c2=";c2.display();

c3=c1+c2;

cout<<"c1+c2=";c3.display();

c3=c1-c2;

cout<<"c1-c2=";c3.display();

c3=c1*c2;

cout<<"c1*c2=";c3.display();

c3=c1/c2;

cout<<"c1/c2=";c3.display();

return0;

}

2.定义一个日期类Date,包括年,月,日等私有数据成员。

要求为所定义的Date类设计如下重载运算符函数:

Dateoperator+(intdays);返回一日起加天数days后得到的日期

Dateoperator-(intdays);返回一日起减天数days后得到的日期

Intoperator-(Date&b);返回两日期相差的天数

解:

#include

usingnamespacestd;

intday_tab[12]={31,28,31,30,31,30,31,31,30,31,30,31};

classDate

{

public:

Date(){}

Date(inty,intm,intd){year=y;mon=m;day=d;}

voidsetday(intd){day=d;}

voidsetmon(intm){mon=m;}

voidsetyear(inty){year=y;}

intgetday(){returnday;}

intgetmon(){returnmon;}

intgetyear(){returnyear;}

Dateoperator+(intdays)

{

staticDatedate;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1