编程题复习资料Word文档下载推荐.docx

上传人:b****2 文档编号:5780369 上传时间:2023-05-05 格式:DOCX 页数:16 大小:21.95KB
下载 相关 举报
编程题复习资料Word文档下载推荐.docx_第1页
第1页 / 共16页
编程题复习资料Word文档下载推荐.docx_第2页
第2页 / 共16页
编程题复习资料Word文档下载推荐.docx_第3页
第3页 / 共16页
编程题复习资料Word文档下载推荐.docx_第4页
第4页 / 共16页
编程题复习资料Word文档下载推荐.docx_第5页
第5页 / 共16页
编程题复习资料Word文档下载推荐.docx_第6页
第6页 / 共16页
编程题复习资料Word文档下载推荐.docx_第7页
第7页 / 共16页
编程题复习资料Word文档下载推荐.docx_第8页
第8页 / 共16页
编程题复习资料Word文档下载推荐.docx_第9页
第9页 / 共16页
编程题复习资料Word文档下载推荐.docx_第10页
第10页 / 共16页
编程题复习资料Word文档下载推荐.docx_第11页
第11页 / 共16页
编程题复习资料Word文档下载推荐.docx_第12页
第12页 / 共16页
编程题复习资料Word文档下载推荐.docx_第13页
第13页 / 共16页
编程题复习资料Word文档下载推荐.docx_第14页
第14页 / 共16页
编程题复习资料Word文档下载推荐.docx_第15页
第15页 / 共16页
编程题复习资料Word文档下载推荐.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

编程题复习资料Word文档下载推荐.docx

《编程题复习资料Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《编程题复习资料Word文档下载推荐.docx(16页珍藏版)》请在冰点文库上搜索。

编程题复习资料Word文档下载推荐.docx

a[j+1])

{temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

voidsort2(inta[])

for(i=0;

for(j=i+1;

j<

if(a[i]<

a[j])

{temp=a[i];

a[i]=a[j];

a[j]=temp;

voidsort3(inta[])

{

inti,j,k,temp;

{k=i;

for(j=i+1;

if(a[k]>

a[j])k=j;

if(k>

i)

{temp=a[i];

a[i]=a[k];

a[k]=temp;

voidmain(void)

inti;

intb[10];

"

请输入10个数:

10;

cin>

b[i];

sort1(b);

输出排好序的10个数:

print(b);

sort2(b);

sort3(b);

程序运行结果:

269115612532221419

2569101114222532

3225221411109652

(2)

①递归公式为:

x;

n=1

px(n)=

px(n-1)+(-1)n-1xn;

n>

1

②递归结束条件:

n=1

③递归约束条件:

#include<

{floatx;

intn;

floatpx(float,int);

cout<

pleaseinputx,n:

;

cin>

x>

n;

px="

px(x,n)<

floatpx(floatx,intn)

{floatp;

if(n==1)

p=x;

else

p=px(x,n-1)-pow(-1,n)*pow(x,n);

returnp;

24

px=-10

二、类与对象的定义与使用

(1)定义一个复数类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)、姓名(Name[8])、数学(Math)、物理(Phi)、数据结构(Data)、平均分(ave)。

定义能输入学生成绩的公有成员函数Write(),能计算学生平均分的公有成员函数Average(),能显示学生成绩的公有成员函数Display()。

在主函数中用Score类定义学生成绩对象数组s[3]。

用Write()输入学生成绩,用Average()计算每个学生的平均分,最后用Display()显示每个学生的成绩。

实验数据:

NoNameMathPhiDataAve

1001Zhou807060

1002Chen908085

1003Wang707589

(3)定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。

用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。

在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。

然后调用Show()显示矩形左上角与右下角坐标及对角线长度。

最后用delete运算符回收为矩形动态分配的存储空间。

(1)

classComplex

{private:

floatReal,Image;

public:

Complex(floatr,floati)rite(no,name,math,phi,data);

s[i].Average();

}

学号姓名数学物理数据结构平均分\n"

for(i=0;

3;

s[i].Display();

}

程序运行结果:

Input3studentdata

1001Zhou807060

1002Chen908085

1003Wang707589

学号姓名数学物理数据结构平均分

1001Zhou80706070

1002Chen90808585

1003Wang70758978

(3)

classRectangle

{protected:

floatLeft,Top;

floatRight,Bottom;

Rectangle(floatl,floatt,floatr,floatb)

{Left=l;

Top=t;

Right=r;

Bottom=b;

}

Rectangle(Rectangle&

R)

{Left=0;

Top=0;

Right=;

Bottom=;

doubleDiagonal()

{returnsqrt((Left-Right)*(Left-Right)+(Top-Bottom)*(Top-Bottom));

voidShow()

{cout<

(Left,Top)=("

Left<

"

Top<

)"

'

\n'

(Right,Bottom)=("

Right<

Bottom<

Diagonal="

Diagonal()<

};

{Rectangle*r1=newRectangle(10,10,20,20);

r1->

Show();

deleter1;

(Left,Top)=(10,10)

(Right,Bottom)=(20,20)

Diagonal=

三、类的继承

(1)定义描述职工档案的类Archives,私有数据成员为职工号(No)、姓名(Name[8])、性别(Sex)、年龄(Age)。

成员函数有:

构造函数、显示职工信息的函数Show()。

再由职工档案类派生出职工工资类Laborage,在职工工资类Laborage中新增数据成员:

应发工资(SSalary)、社保金(Security)、实发工资(Fsalary),其成员函数有:

构造函数,计算实发工资的函数Count(),计算公式为:

实发工资=应发工资-社保金。

显示职工档案及工资的函数Display()。

在主函数中用Laborage类定义职工对象lab,并赋初始值(1001,”Cheng”,’M’,21,2000,100),然后显示职工档案与工资。

(2)定义描述矩形的类Rectangle,其数据成员为矩形的中心坐标(X,Y)、长(Length)与宽(Width)。

成员函数为计算矩形面积的函数Area()与构造函数。

再定义描述圆的类Circle,其数据成员为圆的中心坐标(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,增加描述学生的信息:

班级、学号、专业、英语成绩和数学成绩。

再由基类Person定义一个职工的派生类Employee,增加描述职工的信息:

部门、职务、工资。

编写程序实现学生与职工信息的输入与输出。

classArchives

intNo;

charName[8];

charSex;

intAge;

Archives(intn,charname[],chars,inta)

{No=n;

strcpy(Name,name);

Sex=s;

Age=a;

voidShow(void)

No="

No<

\t'

Name="

Name<

<

Sex="

Sex<

Age="

Age<

};

classLaborage:

publicArchives

floatSSalary,Security,Fsalary;

Laborage(intn,charname[],chars,inta,floatss,floatse):

Archives(n,name,s,a)

{SSalary=ss;

Security=se;

voidCount()

{

Fsalary=SSalary-Security;

voidDisplay(void)

{Show();

cout<

SSalary="

SSalary<

Security="

Security

<

Fsalary="

Fsalary<

{Laboragelab(1001,"

Zhou"

'

M'

52,2000,200);

();

No=1001Name=ZhouSex=MAge=52

SSalary=2000Security=200Fsalary=1800

(2)

#definePI

classRectangle//定义一个长方体类

floatLength,Width;

floatCenterx,Centery;

Rectangle(floatl,floatw,floatx,floaty)

{Length=l;

Width=w;

Centerx=x;

Centery=y;

floatArea(void)

{returnLength*Width;

classCircle//定义一个圆形类

floatradius;

Circle(floatr,floatx,floaty)

{radius=r;

doubleArea(void)

{returnradius*radius*PI;

classCuboid:

publicRectangle,publicCircle//由基类Rectangle、Circle派生出类Cuboid

floatHigh;

doubleRVolume,CVolume;

Cuboid(floatl,floatw,floatx1,floaty1,floatr,floatx2,floaty2,floath):

Rectangle(l,w,x1,y1),Circle(r,x2,y2)

{High=h;

voidVol(void)//分别计算长方体和圆柱体的体积

{RVolume=Rectangle:

:

Area()*High;

CVolume=Circle:

voidShow(void)//分别显示长方体和圆柱体的信息

Length="

Length<

Width="

Width<

High="

High<

RectangleCentercoordinate="

Rectangle:

Centerx<

Centery<

Vol();

CuboidVolume="

RVolume<

Radius="

radius<

CircleCentercoordinate="

Circle:

CylinderVolume="

CVolume<

voidmain(void)

{Cuboidcub(10,10,10,20,30,30,10,10);

Length=10Width=10High=10

RectangleCentercoordinate=10,20

RectangleVolume=1000

Radius=30High=10

CircleCentercoordinate=30,10

CircleVolume=

classPerson

charBirth[10];

Person()

{}

Person(charname[],charsex,charbirth[])

{strcpy(Name,name);

Sex=sex;

strcpy(Birth,birth);

voidShow()

Birth<

classStudent:

publicPerson

charSclass[10];

charMajor[10];

floatEng,Math;

Student():

Person()

Student(charname[],charsex,charbirth[],charsclass[],intno,charmajor[],floateng,floatmath):

Person(name,sex,birth)

{strcpy(Sclass,sclass);

No=no;

strcpy(Major,major);

Eng=eng;

Math=math;

voidPrint()

{Person:

Sclass<

Major<

Eng<

Math<

classEmployee:

charDepartment[10];

charTitle[10];

floatSalary;

Employee():

Employee(charname[],charsex,charbirth[],chardepartment[10],chartitle[10],

floatsalary):

Person(name,sex,birth)

{strcpy(Department,department);

strcpy(Title,title);

Salary=salary;

Department<

Title<

Salary<

{intno;

charname[8],sex,birth[10],major[10],class1[10],depa[10],title[10];

floateng,math,salary;

Student*s;

Employee*e;

Inputastudentdata"

name>

sex>

birth>

class1>

no>

major>

eng>

math;

s=newStudent(name,sex,birth,class1,no,major,eng,math);

Thestudentinformation:

\n"

s->

Print();

Inputanemployeedata"

depa>

title>

salary;

e=newEmployee(name,sex,birth,depa,title,salary);

Theemployeeinformation:

e->

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

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2