河南理工大学C++程序设计期末考试程序设计题及复习资料.docx
《河南理工大学C++程序设计期末考试程序设计题及复习资料.docx》由会员分享,可在线阅读,更多相关《河南理工大学C++程序设计期末考试程序设计题及复习资料.docx(20页珍藏版)》请在冰豆网上搜索。
![河南理工大学C++程序设计期末考试程序设计题及复习资料.docx](https://file1.bdocx.com/fileroot1/2023-2/3/223375f9-8c6d-4492-a22b-8d9f0898da6e/223375f9-8c6d-4492-a22b-8d9f0898da6e1.gif)
河南理工大学C++程序设计期末考试程序设计题及复习资料
河南理工大学C++程序设计期末考试复习题
(计算机1505张诚华制作)
1、面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了。
2、面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为。
3、泛型程序设计(Genericprogramming):
可以被很多不同的类型的对象所重用。
比那些直接使用Object变量,然后强制类型的转换的代码具有更好的安全性和可读性。
4、泛型类,就是指具有一个或者多个类型变量,也就是说这个类适应这几种类型。
使用类型变量T,用<>括起来,放在类名后面。
这个泛型可以有多个类型变量,如,可以使用类定义的类型变量指定类中属性和方法的类型
声明语法:
publicclassClassName{...};
5、泛型方法既可以在普通类中,也可以在泛型类中,定义方式是在方法名前加T,说明该方法是泛型方法
classArrayAlg{
publicstaticTgetA(Ta){
//方法体;
returna;
}
};
以下习题好好做一遍
1.打印如下形式的乘法口诀表
*123456789
1123456789
224681012141618
3369121518212427
44812162024283236
551015202530354045
661218243036424854
771421283542495663
881624324048566472
991827364554637281
//乘法口诀表.cpp:
Definestheentrypointfortheconsoleapplication.
//Code:
#include"stdafx.h"
#include
usingnamespacestd;
voidmymultable();
intmain()
{
mymultable();
return0;
}
voidmymultable()
{
inti,j,um;
cout<<"*";
for(i=1;i<10;i++)
{
cout<<"\t"<
}
for(i=1;i<10;i++)
{
cout<<"\n"<
for(j=1;j<10;j++)
{
cout<<"\t"<
}
}
cout<<"\n";
}
2.斐波那契数列第一项和第二项都是1,此后各项满足F(n)=F(n-1)+F(n-2),编写程序,输入整数n,输出斐波那契数列的第n项
//斐波那契数列.cpp:
Definestheentrypointfortheconsoleapplication.
//
//Code:
#include"stdafx.h"
#include
#include
#include
usingnamespacestd;
voidmyfn();
intmain()
{
myfn();
return0;
}
voidmyfn()
{
inti,n;
cin>>n;//输入数组大小
int*p=(int*)calloc(n,sizeof(int));//内存空间分配长度为n的动态数组,用p指向首地址
p[0]=p[1]=1;
if(n==1||n==2)
{
cout<<"斐波那契额数列的第"<"<
}
else{
for(i=2;i{
p[i]=p[i-2]+p[i-1];
}
cout<<"斐波那契额数列的第"<"<
}
cout<<"\n";
}
3.实现一个学生信息处理程序,输入:
姓名,年龄,学号(整数),第一学年的平均成绩,第二学年的平均成绩,第三学年的平均成绩,第四学年的平均成绩。
输出:
姓名,年龄,学号,4年的平均成绩。
例如:
输入:
Bob223102070609060
输出:
Bob,22,31020,70
要求实现一个能代表学生的类,并且所有成员都是私有的。
//学生信息处理程序.cpp:
Definestheentrypointfortheconsoleapplication.
//
//Code:
#include"stdafx.h"
#include
usingnamespacestd;
classstudent{
private:
intstu_age,grade1,grade2,grade3,grade4;
charstu_name[20];
charstu_num[10];
chark;//这里用于空格的定义
public:
voidinit();
voidprint();
voidaverage_grade();
};
voidstudent:
:
init(){
cout<<"请输入学生相关信息:
";
cin>>stu_name;
cin>>stu_age;
cin>>stu_num;
cin>>grade1>>grade2>>grade3>>grade4;
}
voidstudent:
:
print(){
cout<}
voidstudent:
:
average_grade(){
cout<<(grade1+grade2+grade3+grade4)/4<}
intmain(){
studentc;
c.init();
c.print();
c.average_grade();
system("pause");
return0;
}
4.打印以下杨辉三角形,要求输出到第15行
1
11
121
1331
14641
//Code:
#include
usingnamespacestd;
intmain(){
constintn=15;
inta[20][20],i,j;
for(i=0;ia[i][0]=1;
a[i][i]=1;
for(j=1;j
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
for(i=0;i{
for(j=0;j<=i;++j)
{
cout<}
cout<}
return0;
}
5.定义一个CRectangle类(矩形类),该类封装信息如下
要求:
(1)私有的两个成员变量width,height分别表示对象的宽和高
(2)类中包含三个共有的函数CRectangle(intw,inth),area(),perimeter(),函数功能如下
CRectangle(intw,inth):
设置宽和高
area():
求得矩形面积
perimeter():
求得矩形周长
//矩形周长面积.cpp:
Definestheentrypointfortheconsoleapplication.
//
//Code:
#include"stdafx.h"
#include
usingnamespacestd;
classCRectangle
{
public:
CRectangle(intw,inth);
intperimeter();
intarea();
private:
intwidth,height;
};
CRectangle:
:
CRectangle(intw,inth){
width=w;
height=h;
}
intCRectangle:
:
area(){
returnwidth*height;
}
intCRectangle:
:
perimeter(){
return2*(width+height);
}
intmain(void){
CRectangleCRectangle(3,5);
cout<cout<return0;
}