编程题复习资料_第1页
编程题复习资料_第2页
编程题复习资料_第3页
编程题复习资料_第4页
编程题复习资料_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.一、函数的定义与调用(1)分别用冒泡法(升序)、选择法(降序)、擂台法(升序)编写三个对一维数组进行排序的函数,函数名为sort1()、sort2()、sort3()。再定义一个输出数组元素值的函数print()。在主函数中定义一维整型数组aN(N=10),用键盘输入10个整数给aN数组。依次调用sort1()、print()、sort2()、print()、sort3()、print(),进行升序、降序、升序的操作,并输出每次排序后的结果。输入十个实验数据:10,25,90,80,70,35,65,40,55,5(2)编写一个函数px(float x,int n)用递归的方法求下列级数前n

2、项的和s。 在主函数中定义变量x与n,用键盘输入x与n的值,调用px()函数计算并返回级数前n项和s。最后输出s的值。输入实验数据:x=1.2 n=10解答参考 (1)#include #include #define N 10void print(int a) int i;for(i=0;iN;i+)coutsetw(5)ai;coutendl;void sort1( int a ) int i,j,temp;for(i=0;iN-1;i+) for(j=0;jaj+1) temp=aj; aj=aj+1; aj+1=temp;void sort2( int a ) int i,j,temp

3、; for(i=0;iN-1;i+) for(j=i+1; jN;j+) if (aiaj) temp=ai;ai=aj;aj=temp;void sort3( int a )int i,j,k,temp; for(i=0;iN-1;i+) k=i; for(j=i+1; jaj) k=j; if (ki) temp=ai; ai=ak; ak=temp;void main(void)int i;int b10;cout请输入10个数:endl;for(i=0;ibi;sort1(b);cout输出排好序的10个数:endl;print(b);sort2(b);cout输出排好序的10个数:e

4、ndl;print(b);sort3(b);cout输出排好序的10个数:1递归结束条件: n=1递归约束条件: n1# include # include void main(void) float x; int n; float px(float,int); coutxn; coutpx=px(x,n)endl;float px(float x,int n) float p;if (n=1) p=x; else p=px(x,n-1)-pow(-1,n)*pow(x,n); return p;程序运行结果:please input x,n:2 4px=-10二、类与对象的定义与使用(1)定

5、义一个复数类Complex,复数的实部Real与虚部Image定义为私有数据成员。用复数类定义复数对象c1、c2、c3,用默认构造函数将c1初始化为c1=20+40i ,将c2初始化为c2=0+0i,用拷贝构造函数将c3初始化为c3=20+40i。用公有成员函数Dispaly()显示复数c1、c2与c3 的内容。(2)定义一个学生成绩类Score,描述学生成绩的私有数据成员为学号(No)、姓名(Name8)、数学(Math)、物理(Phi)、数据结构(Data)、平均分(ave)。定义能输入学生成绩的公有成员函数Write(),能计算学生平均分的公有成员函数Average(),能显示学生成绩的

6、公有成员函数Display()。在主函数中用Score类定义学生成绩对象数组s3。用Write()输入学生成绩,用Average()计算每个学生的平均分,最后用Display()显示每个学生的成绩。实验数据:No Name Math Phi Data Ave1001Zhou 80 70 601002Chen90 80 851003Wang70 75 89(3)定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对

7、角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最后用delete运算符回收为矩形动态分配的存储空间。解答参考(1)# include class Complex private: float Real,Image; public: Complex(float r,float i) /定义有参构造函数 Real=r; Image=i; Complex(Complex &c) /定义拷贝构造函数 Real=c.Real; Image=c.Image; Complex() /定义无参构造函数 Re

8、al=0; Image=0; void Display() coutReal+Imagein; ;void main(void) Complex c1(10,20),c2,c3(c1); c1.Display(); c2.Display(); c3.Display(); 程序运行结果:10+20i0+0i10+20i(2)# include # include class Score private: int No; char Name8; float Math,Phi,Data,Ave; public: void Write(int no,char name,float math,float

9、 phi,float data) No=no; strcpy(Name,name); Math=math; Phi=phi; Data=data; void Average(void) Ave=(Math+Phi+Data)/3; void Display() coutNotNametMatht; coutPhitDatatAven; ;void main(void) int i,no; char name8; float math,phi,data; Score s3;coutInput 3 student datan; for (i=0;inonamemathphidata; si.Wri

10、te(no,name,math,phi,data); si.Average(); cout学号 姓名 数学 物理 数据结构 平均分n;for (i=0;i3;i+) si.Display(); 程序运行结果:Input 3 student data1001 Zhou 80 70 601002 Chen 90 80 851003 Wang 70 75 89学号 姓名 数学 物理 数据结构 平均分1001 Zhou 80 70 60 701002 Chen 90 80 85 851003 Wang 70 75 89 78(3)# include # include class Rectangle

11、protected: float Left,Top; float Right,Bottom; public: Rectangle(float l,float t, float r,float b) Left=l;Top=t; Right=r;Bottom=b; Rectangle(Rectangle & R) Left=0;Top=0; Right=R.Right;Bottom=R.Bottom; double Diagonal() return sqrt(Left-Right)* (Left-Right)+(Top-Bottom)*(Top-Bottom); void Show() cout

12、(Left,Top)=(Left,Top)n; cout(Right,Bottom)=(Right,Bottom)n; cout Diagonal= Diagonal()Show(); delete r1; 程序运行结果:(Left,Top)=(10,10)(Right,Bottom)=(20,20)Diagonal=14.1421三、类的继承(1)定义描述职工档案的类Archives,私有数据成员为职工号(No)、姓名(Name8)、性别(Sex)、年龄(Age)。成员函数有:构造函数、显示职工信息的函数Show()。再由职工档案类派生出职工工资类Laborage,在职工工资类Laborag

13、e中新增数据成员:应发工资(SSalary)、社保金(Security)、实发工资(Fsalary),其成员函数有:构造函数,计算实发工资的函数Count(),计算公式为:实发工资=应发工资社保金。显示职工档案及工资的函数Display()。在主函数中用Laborage类定义职工对象lab,并赋初始值(1001,”Cheng”,M,21,2000,100),然后显示职工档案与工资。(2)定义描述矩形的类Rectangle,其数据成员为矩形的中心坐标(X,Y)、长(Length)与宽(Width)。成员函数为计算矩形面积的函数Area()与构造函数。再定义描述圆的类Circle,其数据成员为圆的

14、中心坐标(X,Y)与半径R,其成员函数为构造函数。再由矩形类与圆类多重派生出长方体类Cuboid,其数据成员为长方体的高(High)与体积(Volume)。成员函数为:构造函数,计算体积的函数Vol(),显示矩形坐标(X,Y)、长方体的长、宽、高与体积的函数Show()。主函数中用长方体类定义长方体对象cub,并赋初始值(10,10,10,20,30,30,10,10),最后显示长方体的矩形坐标(X,Y)与长方体的长、宽、高与体积。(3)定义个人信息类Person,其数据成员有姓名、性别、出生年月。并以Person为基类定义一个学生的派生类Student,增加描述学生的信息:班级、学号、专业、

15、英语成绩和数学成绩。再由基类Person定义一个职工的派生类Employee,增加描述职工的信息:部门、职务、工资。编写程序实现学生与职工信息的输入与输出。解答参考(1)# include # include class Archives private: int No; char Name8; char Sex; int Age; public: Archives(int n,char name,char s,int a) No=n; strcpy(Name,name); Sex=s; Age=a; void Show(void) coutNo=NotName=Namet Sex=SextA

16、ge=Agen; ;class Laborage:public Archives private: float SSalary,Security,Fsalary; public: Laborage(int n,char name,char s,int a,float ss,float se):Archives(n,name,s,a) SSalary=ss; Security=se;void Count() Fsalary=SSalary-Security;void Display(void) Show(); coutSSalary=SSalarytSecurity=Security tFsal

17、ary=Fsalaryn;void main(void) Laborage lab(1001,Zhou,M,52,2000,200); lab.Count(); lab.Display();程序运行结果:No=1001 Name=Zhou Sex=M Age=52SSalary=2000 Security=200 Fsalary=1800(2)# include # define PI 3.14159class Rectangle /定义一个长方体类 protected: float Length,Width;float Centerx,Centery; public: Rectangle(f

18、loat l,float w,float x,float y) Length=l; Width=w; Centerx=x; Centery=y;float Area(void) return Length*Width;class Circle /定义一个圆形类 protected: float radius;float Centerx,Centery; public: Circle(float r,float x,float y) radius=r; Centerx=x; Centery=y;double Area(void) return radius*radius*PI;class Cub

19、oid:public Rectangle,public Circle /由基类Rectangle、Circle派生出类Cuboid private: float High; double RVolume,CVolume; public: Cuboid(float l,float w,float x1,float y1,float r,float x2,float y2,float h):Rectangle(l,w,x1,y1),Circle(r,x2,y2) High=h; void Vol(void) /分别计算长方体和圆柱体的体积 RVolume=Rectangle:Area()*High

20、; CVolume=Circle:Area()*High; void Show(void) /分别显示长方体和圆柱体的信息 coutLength=LengthtWidth=Widtht High=Highn; coutRectangle Center coordinate = Rectangle:Centerx, Rectangle:Centeryn; Vol(); coutCuboid Volume=RVolumen; coutRadius=radiustHigh=Highn; coutCircle Center coordinate = Circle:Centerx, Circle:Cen

21、teryn; coutCylinder Volume=CVolumen; ;void main (void) Cuboid cub(10,10,10,20,30,30,10,10); cub.Show();程序运行结果:Length=10 Width=10 High=10Rectangle Center coordinate = 10,20Rectangle Volume=1000Radius=30 High=10Circle Center coordinate = 30,10Circle Volume=28274.3(3)# include # include class Person pr

22、ivate: char Name8; char Sex; char Birth10; public: Person() Person(char name,char sex,char birth) strcpy(Name,name); Sex=sex; strcpy(Birth,birth); void Show() coutNametSext; coutBirtht; ;class Student:public Person private: char Sclass10; int No; char Major10; float Eng,Math; public:Student():Person() Student(char name,char sex,char birth,char sclass,int no,char major,float eng,float math):Person(name,sex,birth) strcpy(Sclass,sclass); No=no; strcpy(Major,major); Eng=eng; Math=math; void Print() Person:Show(); coutSclasstNot

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论