ImageVerifierCode 换一换
格式:DOCX , 页数:28 ,大小:22.18KB ,
资源ID:6273961      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6273961.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C程序设计习题.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C程序设计习题.docx

1、C程序设计习题 文档编制序号:KKIDT-LLE0828-LLETD298-POI08C程序设计习题C+程序设计 练习题学院: 计算机学院 专业班级: 物联网1002 学号: 姓名: 徐 远 志 第八章1.下面是一个类的测试程序,试设计出能是用如下测试程序的类.Int main() Test x;(30,20);();return 0;解:#include using namespace std;class Test 义并实现一个矩形类Crectangle。该类包含了下列成员函数。Crectangle(): 累的构造函数,根据需要可以定义多个构造函数SetTop(),SetLeft(): 设置

2、矩形的左上角坐标SetLength(),SetWidth(): 设置矩形的长和宽Perimeter(): 求矩形的周长Area(): 求矩形的面积GetWidth(): 返回矩形的宽度Getlength(): 返回矩形的长度IsSquare(): 判断矩形是否为正方形Move(): 将矩形从一个位置移动到另一个位置Size(): 改变矩形的大小Where(): 返回矩形的左上角的坐标PrintRectangle(): 输出矩形的四个顶点的坐标数据成员int top,left;int length,width;解:#include using namespace std;class Crecta

3、ngle 次歌手大赛中,有JudgeNum个评委给选手打分,参加比赛的选手有PlayerNum名,现为比赛积分编写一个CompetitionResult类,类的定义如下: (定义略)(1)写出所有的成员函数和实现代码。(2)编写main()函数对该类进行测试。在函数体中,定义了一个competitionResult类的对象数组rPlauerNum,他的每个元素记录了每个选手的所有信息,个评委的打分通过键盘的输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到底排列输出。解:(1)CompetitionResult:CompetitionResult() num=0; strcpy

4、(name,); for(int i=0;iJudgeNum;i+) scorei=; average=0;CompetitionResult:CompetitionResult(short n,char*ps) num=n;strcpy(name,ps); for(int i=0;iJudgeNum;i+) scorei=; average=0;float CompetitionResult:MaxScore() int max=score0; for(int i=0;iJudgeNum;i+) if(maxscorei) max=scorei; return max;float Compe

5、titionResult:MinScore() int min=score0; for(int i=0;iscorei) min=scorei; return min;void CompetitionResult:SetAvg() int i;float sum=; for(i=0;iJudgeNum;i+) sum+=scorei; sum=sum-MaxScore()-MinScore(); average=sum/(JudgeNum-2);float CompetitionResult:GetAvg() return average;short CompetitionResult:Get

6、No(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; f

7、or(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),Compe

8、titionResult(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;

9、j+) coutj+1 ; coutsetw(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;protecte

10、d: int a,b;class Derived:public Base public: void sets() s=a*b; void shows() couts=sendl;private: int s;int main() Derived obj; (2,3); (); (); (); return 0;解:可以访问a和b;因为a,b为保护成员,经公有继承,成为派生类中得保护成员,所以派生类Derived 的成员函数sets()能访问基类base中得变量a和b。如果定义为私有,则不能通过编译。因为派生类中的成员函数不能访问基类中得私有成员2声明一个图形基类Shape,在它的基础上派生出矩

11、形类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,doubl

12、e 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*r; double getArea() return r*r*; private: double r;class Square:public Rectanglepublic: Square(double x)

13、 height=x; width=x; ;int main() Rectangle a1,; Circle a2; Square a3; coutArea=()endl; coutPerimeter=()endl; coutArea=()endl; coutPerimeter=()endl; coutArea=()endl; coutPerimeter=()endl; return 0;5.某销售公司有销售经理和销售员工,月工资的计算办法为:销售激励的底薪为4000元,并将销售额的2/1000做提成,销售员工无底薪,只提取5/1000为工资。编写程序:(1)定义一个积累Employee,它包含

14、三个数据成员number,name和salary,以及编号和姓名的构造函数。(2)有Employee类派生Salesman类。Salesman类包含两个新数据成员commrate和sales,还包括了用于输入销售额并计算销售员工工资的成员函数pay()和用于输出打印的print()。(3)由Salesman派生Salemanager,Salesmmanage类包含新数据成员monthlypay,以及用于输入销售额并计算销售经理工资的成员函数pay()和用于输出的print()。(4)编写main()函数中测试所设计的类结构。解:(1)class Employeepublic: Employee

15、(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=:Employee(num,n) commrate=c; void pay() coutnamesales; salary=commrate*sales; void print() coutname1aosalaryendl; protected: fl

16、oat commrate; double sales;(3) class Salesmanager:public Salesmanpublic: Salesmanager(long num,char *n,float c=,double m=4000):Salesman(num,n,c) monthlypay=m; void pay() coutnamesales; salary=monthlypay+commrate*sales; void print() coutname1aosalaryendl; private: double monthlypay;(4) int main() Sal

17、esman s1(1234,e); Salesmanager s2(1357,); (); (); (); (); return 0;第十章1.定义一个辅助类Complex,重载运算符+、-、*、/使之能用于复数的加减乘除运算。运算符重载函数为Complex类的成员函数。编写程序,分别求出两个复数之和、差、积、商。解:#include using namespace std;class Complexpublic: Complex(double r=,double i= real=r;imag=i; Complex operator + (Complex); friend Complex op

18、erator - (Complex,Complex); Complex operator * (Complex); friend Complex operator / (Complex,Complex); void display();private: double real,imag;Complex Complex:operator + (Complex c) return Complex(real+,imag+;Complex operator - (Complex c1,Complex c2) return Complex Complex:operator * (Complex c) r

19、eturn Complex(real*+,imag*+real*;Complex operator / (Complex c1,Complex c2) double r,i; r=*+*/*+*; i=* return(r,i);void Complex:display() cout(real,imag)endl;int main() Complex c1,c2,5),c3; coutc1=; (); coutc2=; (); c3=c1+c2; coutc1+c2=;(); c3=c1-c2; coutc1-c2=;(); c3=c1*c2; coutc1*c2=;(); c3=c1/c2;

20、 coutc1/c2=;(); 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,31,30,31,30,31,31,30,31,30,31;class Datepublic: D

21、ate() 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 number=dton(*this); number-=days; date=ntod(

22、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 leap(int); int dton(Date &);

23、 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=;y+) if(leap(y) days+=366; else days+=365; day_tab1=leap29:28; for(m=0;m;m+) days+=day_tabm; days+=; return days;Date Date:ntod(int n) int y=1,m=1,d,re

24、st=n,days_year; while(1) days_year=leap(y)366:365; if(restday_tabm-1) rest-=day_tabm-1; else break; m+; d=rest; return Date(y,m,d);int main() Date now(2011,12,22),Nationday(1949,10,1); coutnow:;(); coutNationday:;(); cout2y:(now-Nationday)endl; Date d1=now+365,d2=now-365; coutnow+365:;(); coutnow-36

25、5:;(); return 0;3.编写一个程序尖酸正方体圆柱体的表面积和体积。要求抽象出一个公共的基类Body,吧他当做抽象类,在该类中定义表面积和体积的纯虚函数。抽象类中定义一个数据成员data,他可以作为求的半径,正方形的边长,或者圆柱体的地面半径。在三个类中都有计算白面积和体积的函数的具体实现。解:#include using namespace std;class Bodyprotected: double data;public: Body(double data) Body:data=data; virtual double s()=0; virtual double v()=0

26、;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*4; double v() return 4*data*data*data/3; ;class Cylinder:public Body double height;public: Cylinder(double r,double height): Body(r)Cylinder:height=height; double s() return (2*data*data+2*data*height); double v() return data*data*height; ;int main() Body *p; Cube obj1(

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

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