1、C+程序设计 练习题学院: 计算机学院 专业班级: 物联网1002 学号:0121010340705 姓名: 徐 远 志 第八章1.下面是一个类的测试程序,试设计出能是用如下测试程序的类.Int main() Test x;x.initx(30,20);x.printx();return 0;解:#include using namespace std;class Test /类的开始public:void initx(int i,int j);void printx();private:int x,y; void Test:initx(int i,int j)x=i;y=j;void Tes
2、t:printx()coutx*y=x*yendl; /类的结束int main() /测试函数Test x;x.initx(30,20);x.printx();return 0;得到的测试结果:4.定义并实现一个矩形类Crectangle。该类包含了下列成员函数。Crectangle(): 累的构造函数,根据需要可以定义多个构造函数SetTop(),SetLeft(): 设置矩形的左上角坐标SetLength(),SetWidth(): 设置矩形的长和宽Perimeter(): 求矩形的周长Area(): 求矩形的面积GetWidth(): 返回矩形的宽度Getlength(): 返回矩形的
3、长度IsSquare(): 判断矩形是否为正方形Move(): 将矩形从一个位置移动到另一个位置Size(): 改变矩形的大小Where(): 返回矩形的左上角的坐标PrintRectangle(): 输出矩形的四个顶点的坐标数据成员int top,left;int length,width;解:#include using namespace std;class Crectangle /类的开始int top,left;int length,width;public:Crectangle(int t=0,int l=0,int len=1,int w=1) top=t;left=l; if
4、(len0) length=len; else length=0;if (w0) width=w; else width=0;void SetTop(int t)top=t;void SetLeft(int l)left=l;void SetLength(int len)length=len;void SetWidth(int w)width=w;int Perimeter()return 2*(width+length);int Area()return width*length;int GetWidth()return width;int GetLength()return length;
5、int IsSquare() if(width=length) cout该矩形是正方形endl;return 1;elsecout该矩形不是正方形endl;return 0;void Move(int x,int y) left+=x; top+=y;void Size(int l,int w) length=l; width=w; void PrintRectangle()cout左上方的点为:(left,top)endl;cout右上方的点为:(left+length,top)endl;cout左下方的点为:(left,top+width)endl;cout右下方的点为:(left+len
6、gth,top+width)endl; /类的结束int main()Crectangle a(1,1,5,5);a.PrintRectangle();a.SetTop(2); a.SetLeft(3);a.SetLength(4);a.SetWidth(5); a.IsSquare();cout周长为:a.Perimeter()endl;cout面积为:a.Area()endl;a.PrintRectangle(); cout用getwidth等函数得到的值为:endl;cout 长为:a.GetLengthendl;cout 宽为:a.GetWidthendl;return 0;得到的测试
7、结果:6.某次歌手大赛中,有JudgeNum个评委给选手打分,参加比赛的选手有PlayerNum名,现为比赛积分编写一个CompetitionResult类,类的定义如下: (定义略)(1) 写出所有的成员函数和实现代码。(2) 编写main()函数对该类进行测试。在函数体中,定义了一个competitionResult类的对象数组rPlauerNum,他的每个元素记录了每个选手的所有信息,个评委的打分通过键盘的输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到底排列输出。 解:(1)CompetitionResult:CompetitionResult()num=0; str
8、cpy(name,);for(int i=0;iJudgeNum;i+) scorei=0.0;average=0;CompetitionResult:CompetitionResult(short n,char*ps)num=n;strcpy(name,ps);for(int i=0;iJudgeNum;i+) scorei=0.0;average=0;float CompetitionResult:MaxScore() int max=score0;for(int i=0;iJudgeNum;i+)if(maxscorei) max=scorei;return max;float Comp
9、etitionResult:MinScore() int min=score0; for(int i=0;iscorei) min=scorei;return min;void CompetitionResult:SetAvg() int i;float sum=0.0; for(i=0;iJudgeNum;i+) sum+=scorei; sum=sum-MaxScore()-MinScore(); average=sum/(JudgeNum-2);float CompetitionResult:GetAvg() return average;short CompetitionResult:
10、GetNo(int i) return num;void CompetitionResult:SetNo(int i) num=i;char CompetitionResult:* GetName() return name ;float CompetitionResult:GetScore(int j) return scorej;void CompetitionResult:Setscore(int k,float av) scorek=av; void Sort(CompetitionResult *pr,int n) int i,j ,k; CompetitionResult temp
11、; for(i=0;in-1;i+) k=i; for(j=i+1;j(prk.average) k=j; if(k!=j) temp=pri; pri=prk; prk=temp; (2) int main()CompetitionResult playerPlayerNum=CompetitionResult(1,one),CompetitionResult(2,two),CompetitionResult(3,there),CompetitionResult(4,four),CompetitionResult(5,five),CompetitionResult(6,six),Compet
12、itionResult(7,seven),CompetitionResult(8,eight),CompetitionResult(9,nine),CompetitionResult(10,ten);float a;for(int i=0;iPlayerNum;i+)cout请评委给第i+1个选手打分:endl;for(int j=0;ja;playeri.Setscore(j,a);playeri.SetAvg();coutendl;Sort(player,PlayerNum);cout 最后的得分情况为: endl;for (int j=0;jPlayerNum;j+)coutj+1 ;c
13、outsetw(6)playerj.GetAvg();cout playerj.GetName()endl;return 0;程序运行的结果为:第九章1. 在下面的程序中,派生类Derived 的成员函数sets()能否访问基类base中得变量a和b?为什么?如果在基类base中将数据成员a和b定义为私有成员,下面的程序能否通过编译?为什么?#include using namespace std;class Base public:void set(int i,int j) a=i;b=j;void show() couta=a,b=bendl;protected:int a,b;class
14、 Derived:public Base public:void sets() s=a*b;void shows() couts=sendl;private:int s;int main() Derived obj;obj.set(2,3);obj.show();obj.sets();obj.shows();return 0;解:可以访问a和b;因为a,b为保护成员,经公有继承,成为派生类中得保护成员,所以派生类Derived 的成员函数sets()能访问基类base中得变量a和b。如果定义为私有,则不能通过编译。因为派生类中的成员函数不能访问基类中得私有成员2声明一个图形基类Shape,在它
15、的基础上派生出矩形类Rectangle和圆形类Circle,它们都有计算面积的和周长、输出图形信息的成员函数,再在Rectangle类的基础上派生方形类Square。编写程序和各类的定义和实现,以及类的使用。解: #include using namespace std;class Shape public:double getArea()double getPerimeter() ;class Rectangle:public Shape protected:double height;double width;public:Rectangle() Rectangle(double a,do
16、uble b)height=a;width=b;double getArea() return height*width;double getPerimeter() return 2*(height+width);class Circle:public Shapepublic:Circle(double x):r(x)double getPerimeter()return 2*3.1416*r;double getArea()return r*r*3.1416;private:double r;class Square:public Rectanglepublic:Square(double
17、x)height=x;width=x;int main()Rectangle a1(2.2,3.2);Circle a2(4.2);Square a3(5.3);coutArea=a1.getArea()endl;coutPerimeter=a1.getPerimeter()endl;coutArea=a2.getArea()endl;coutPerimeter=a2.getPerimeter()endl;coutArea=a3.getArea()endl;coutPerimeter=a3.getPerimeter()endl;return 0;5.某销售公司有销售经理和销售员工,月工资的计算
18、办法为:销售激励的底薪为4000元,并将销售额的2/1000做提成,销售员工无底薪,只提取5/1000为工资。编写程序:(1) 定义一个积累Employee,它包含三个数据成员number,name和salary,以及编号和姓名的构造函数。(2) 有Employee类派生Salesman类。Salesman类包含两个新数据成员commrate和sales,还包括了用于输入销售额并计算销售员工工资的成员函数pay()和用于输出打印的print()。(3) 由Salesman派生Salemanager,Salesmmanage类包含新数据成员monthlypay,以及用于输入销售额并计算销售经理工
19、资的成员函数pay()和用于输出的print()。(4) 编写main()函数中测试所设计的类结构。解:(1)class Employeepublic:Employee(long num,char *n)number=num;strcpy(name,n);protected:long number;char name20;double salary;(2)class Salesman:public Employeepublic:Salesman(long num,char *n,float c=0.005):Employee(num,n) commrate=c;void pay()coutna
20、mesales;salary=commrate*sales;void print()coutnamesalaryendl;protected:float commrate;double sales;(3) class Salesmanager:public Salesmanpublic:Salesmanager(long num,char *n,float c=0.002,double m=4000):Salesman(num,n,c) monthlypay=m;void pay()coutnamesales;salary=monthlypay+commrate*sales;void prin
21、t()coutnamesalaryendl;private:double monthlypay;(4) int main()Salesman s1(1234,);Salesmanager s2(1357,);s1.pay();s1.print();s2.pay();s2.print();return 0;第十章1.定义一个辅助类Complex,重载运算符+、-、*、/使之能用于复数的加减乘除运算。运算符重载函数为Complex类的成员函数。编写程序,分别求出两个复数之和、差、积、商。解:#include using namespace std;class Complexpublic:Compl
22、ex(double r=0.0,double i=0.0) real=r;imag=i;Complex operator + (Complex);friend Complex operator - (Complex,Complex);Complex operator * (Complex);friend Complex operator / (Complex,Complex);void display();private:double real,imag;Complex Complex:operator + (Complex c)return Complex(real+c.real,imag+
23、c.imag);Complex operator - (Complex c1,Complex c2)return Complex(c1.real-c2.real,c1.imag-c2.imag);Complex Complex:operator * (Complex c)return Complex(real*c.real-imag+c.imag,imag*c.imag+real*c.real);Complex operator / (Complex c1,Complex c2)double r,i;r=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2
24、.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);void Complex:display()cout(real,imag)endl;int main()Complex c1(7.1,8.4),c2(1.4,5),c3;coutc1=; c1.display();coutc2=; c2.display();c3=c1+c2;coutc1+c2=;c3.display();c3=c1-c2;coutc1-c2=;c3.display();
25、c3=c1*c2;coutc1*c2=;c3.display();c3=c1/c2;coutc1/c2=;c3.display();return 0;2.定义一个日期类Date,包括年,月,日等私有数据成员。要求为所定义的Date类设计如下重载运算符函数:Date operator+(int days); 返回一日起加天数days后得到的日期Date operator-(int days); 返回一日起减天数days后得到的日期Int operator-(Date &b); 返回两日期相差的天数解:#include using namespace std;int day_tab12=31,28
26、,31,30,31,30,31,31,30,31,30,31;class Datepublic:Date() Date(int y,int m,int d) year=y;mon=m;day=d;void setday(int d) day=d;void setmon(int m) mon=m;void setyear(int y) year=y;int getday() return day;int getmon() return mon;int getyear() return year;Date operator + (int days)static Date date;int numb
27、er=dton(*this);number-=days;date=ntod(number);return date;Date operator - (int days) static Date date;int number=dton(*this);number-=days;date=ntod(number);return date; int operator - (Date &b)int days=dton(*this)-dton(b)-1;return days;void disp()coutyear.mon.dayendl;private:int year,mon,day;int lea
28、p(int);int dton(Date &);Date ntod(int);int Date:leap(int year)if(year%4=0&year&100!=0|year%400=0)return 1;else return 0;int Date:dton(Date &d)int y,m,days=0;for(y=1;y=d.year;y+)if(leap(y)days+=366;elsedays+=365;day_tab1=leap(d.year)?29:28;for(m=0;md.mon-1;m+)days+=day_tabm;days+=d.day;return days;Da
29、te Date:ntod(int n)int y=1,m=1,d,rest=n,days_year;while(1)days_year=leap(y)?366:365;if(restday_tabm-1)rest-=day_tabm-1;elsebreak;m+;d=rest;return Date(y,m,d);int main()Date now(2011,12,22),Nationday(1949,10,1);coutnow:;now.disp();coutNationday:;Nationday.disp();cout:(now-Nationday)endl;Date d1=now+3
30、65,d2=now-365;coutnow+365:;d1.disp();coutnow-365:;d2.disp();return 0;3.编写一个程序尖酸正方体圆柱体的表面积和体积。要求抽象出一个公共的基类Body,吧他当做抽象类,在该类中定义表面积和体积的纯虚函数。抽象类中定义一个数据成员data,他可以作为求的半径,正方形的边长,或者圆柱体的地面半径。在三个类中都有计算白面积和体积的函数的具体实现。解:#include using namespace std;class Bodyprotected:double data;public:Body(double data)Body:dat
31、a=data;virtual double s()=0;virtual double v()=0;class Cube:public Bodypublic:Cube(double data):Body(data);double s()return data*data*6;double v()return data*data*data;class Sphere:public Bodypublic:Sphere(double r):Body(r);double s()return data*data*3.1416*4;double v() return 4*data*data*data/3;cla
32、ss Cylinder:public Bodydouble height;public:Cylinder(double r,double height): Body(r)Cylinder:height=height;double s()return (2*3.1416*data*data+2*data*3.1416*height);double v()return data*data*3.1416*height;int main()Body *p;Cube obj1(5);Sphere obj2(5);Cylinder obj3(5,5);p=&obj1;cout正方体的表面积:s()endl
33、;cout正方体的体积:v()endl;p=&obj2;cout球的表面积:s()endl;cout球的体积:v()endl;p=&obj3;cout球的表面积:s()endl;cout球的体积:v()endl;return 0;第十一章1. 已知Void Sort(int a,int size);Void Sort(double a,int size);是一个函数模板的两个例子,起功能是将数组a中得前size个元素按从小到大的顺序依次排列。是设计函数模板。解:#include using namespace std;templatevoid Sort(T set,int n)int i,j;
34、T temp;for (i=1;ii;j-)if(setj-1setj)temp=setj-1;setj-1=setj;setj=temp;int main()int a=4,5,8,9,3;double b=3,5,6.7,2,5.2,9.2,10.3;Sort(a,6);Sort(b,6);for(int i=0;i6;i+)coutai ;coutendl;for(i=0;i6;i+)coutbi ;coutendl;return 0;2. 设有如下声明class Testpublic: Void SetData1(int val) data1=val;Void SetData2(dou
35、le val) data2=val;int GetData1() return data1;double GetData2() return data2;private: int data1;double data2;试将此类声明为类模板的声明,使得数据成员data1和data2可以使任何类型#include #include using namespace std;template class Testpublic: void SetData1(T1 val) data1=val; void SetData2(T2 val) data2=val; T1 GetData1() return d
36、ata1; T2 GetData2() return data2;private: int data1; double data2;int main()Test t1;t1.SetData1(10);t1.SetData2(5.4); coutt1.GetData1() t1.GetData2()endl;Test t2;t2.SetData1(a);t2.SetData2(China);coutt2.GetData1() t2.GetData2()endl;return 0;3栈是一种重要的数据结构,它是一种只允许在彪的一端进行插入或删除操作的线性表。表中允许进行插入、删除操作的一端称为栈顶。表的令一端称为栈底。下面是一个整形栈类的定义: Const int MaxSize=100; Class IstackPublic: Istack(); void Push(int &n); void pop();int GetTop();bool Empty();int size();boid ClearStack();Istack();Private:Int elemMaxSize;Int top;编写一个栈的类模板,以便为任何类型的对象提供栈结构的数据操作。解:#include #include using namespace std;const int