[计算机类试卷]国家二级(C++)机试模拟试卷36及答案与解析.doc

上传人:孙刚 文档编号:502484 上传时间:2018-11-29 格式:DOC 页数:7 大小:37.50KB
下载 相关 举报
[计算机类试卷]国家二级(C++)机试模拟试卷36及答案与解析.doc_第1页
第1页 / 共7页
[计算机类试卷]国家二级(C++)机试模拟试卷36及答案与解析.doc_第2页
第2页 / 共7页
[计算机类试卷]国家二级(C++)机试模拟试卷36及答案与解析.doc_第3页
第3页 / 共7页
[计算机类试卷]国家二级(C++)机试模拟试卷36及答案与解析.doc_第4页
第4页 / 共7页
[计算机类试卷]国家二级(C++)机试模拟试卷36及答案与解析.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

1、国家二级( C+)机试模拟试卷 36及答案与解析 一、程序改错题( 30分) 1 使用 VC6打开考生文件夹下的工程 test22_1,此工程包含 个源程序文件test22_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果为: 6/15 3/4 9/19 源程序文件 test22_1.cpp清单如下: #include iostream.h class Franction int nume; int deno; public: Franction FranAdd(const Franction deno=l; void InitFranction(int n, int d)

2、 nume=n; deno=d; void FranOutput() cout nume / deno endl; ; void main() Franction *a=new Franction; Franction *b=new Franction; a- InitFranction(6,15); b- InitFranction(3,4); a- FranOutput(); b- FranOutput(); Franction c; /*found*/ c- InitFranction(); /*found*/ c=a- FranAdd(b); c. FranOutput(); 二、简单

3、应用题( 40分) 2 请编写一个函数 void bubble(double data,int length),其中 data是一维数组,存放比较的数据, length是数组中存放元素的个数,用冒泡法将数据 (个数可变 )捧序后由小到大输出。冒泡法是常用的排序算法,这种算法执行效率不高,但比较简单,就是将相邻的两个数据作比较,把较小的数据交换到前面。纵向看来,交换过程 中较小的数据就好像水中的气泡不断浮起。要求使用 for循环实现算法。 注意:部分源程序已存在文件 test23_2.cpp中。 请勿修改主函数 main和其他函数中的任何内容,仅在函数 bubble的花括号中填写若干语句。 文件

4、 test23_.cpp的内容如下: #include iostream.h void bubble(double data, int length) void main () int n; cout “请输入数据的个数 “; cin n; double* ddata = new doublen; for(int i = 0; i n; i+) cout “No.“ i+1 “: “; cin ddatai; bubble (ddata, n); cout “排序后输出数据 :“ endl; for(i = O; i n; i+) cout “No.“ i+1 “:“; cout ddatai

5、 endl; 三、综合应用题( 30分) 3 使用 VC6打开考生文件夹下的工程 test21_3,此工程包含一个源程序文件test21_3.cpp,其中定义了用于表示长方形的类 CRectangle,但类 CRectangle的定义并不完整。请按要求完成下列操作,将类 CRectangle的定义补充完整。 (1)定义 CRectangle的构造函数,函数含参数 dx, dy, da和 db,它们都是 double型的数据,请将类数据成员 x, y, a和 b初始化,并输出 “CRectangle Constructed.”(另起一行输出该文字 )。请在注释 “/*1*之后添加适当的语句。 (

6、2)完成类 CRectangle的成员函数 getperimeter()的定义,将以 a和 b为边的矩形周长的值返回,请 在注释 “/*2*”之后添加适当的语句。 (3)完成类 CRectangle的成员函数 getarea()的定义,将以 a和 b为边的矩形面积的值返回,请在注释 “/*3*”之后添加适当的语句。 (4)完成类 CRectangle的友元函数 friend double dist(CRectangle double y; double a; double b; public: CRectangle() cout “nCRectangle Constructed.“ endl;

7、 CRectangle(double dx, double dy, double da, double db) /*1* a=da; b=db; cout “nCRectangle Constructed.“ endl; CRectangle ( ) cout “CRectangle Destructed.“ endl; void putxy(double dx, double dy) x=dx; y=dy; void putab(double da, double db)( a=da; b=db; double getx() return x; double gety() return y;

8、 double geta() return a; double getb() return b; double getperimeter() /*2* double getarea() /*3* friend double dist(CRectangle ; double dist(CRectangle return sqrt(tx*tx+ty*ty); void main() CRectangle rect; rect.putxy(100.0, 50.0); rect.putab(1200.0, 700.0); cout “Down_Left corner point is: (“ rect

9、.getx() “, “ rect.gety()“)“ endl; cout “a= “ rect.geta() “, b=“ rect.getb() endl; cout “Perimeter of this rectangle is: “ rect.getperimeter() endl; cout “Area of this rectangle is:“ rect.getarea() endl; cout “The Distance is:“ dist(rect) endl; CRectangle recta(200,150,2000,800); cout “Down_Left corn

10、er point is:(“ recta.getx() “,“ recta.gety() “)“ endl; cout “a=“ recta.geta() “, b=“ recta.getb() endl; cout “Perimeter of this rectangle is: “ recta.getperimeter() endl; cout “Area of this rectangle is: “ recta.getarea() endl; cout “The Distance is :“ dist(recta) endl; 国家二级( C+)机试模拟试卷 36答案与解析 一、程序改

11、错题( 30分) 1 【正确答案】 (1)错误: return this; 正确: return*this; (2)错误: c- InitFranction(); 正确: c.InitFrancfion(); (3)错误: c=a- FranAdd(b); 正确: c=a- FranAdd(*b); 【试题解析】 (1)主要考查考生对于 this指针和函数返回值的掌握,它是指向正在活动的对象自己的指针,该函数应该返回 Franction类 型的对象,即应该使用“*“; (2)主要考查考生对于对象和对象指针的使用,对象指针调用成员函数使用符号 “- ”,而对象调用成员函数使用 “.”; (3)主

12、要考查考生对于函数参数使用的掌握, FranAdd()使用对象的引用作为形参,所以实参应该是对象本身。 二、简单应用题( 40分) 2 【正确答案】 void bubble(double data, int length) int segment; int loop; double temp; for(segment=0; segment =length-2; segment+) for(1oop=length-2; loop =segment; loop-) if(dataloop+1 dataloop) temp=dataloop; dataloop =data loop+1; dataloop+1=temp; 【试题解析】 本题考查的是考生使用 for循环和常用的冒泡排序法的综合水平。冒泡排序法就是将相邻的两个数据作比较,把较小的数据交换到前面,以此类推。这是经典的算法应该掌握。 三、综合应用题( 30分) 3 【正确答案】 (1) x=dx; y=dy; (2) return2*(a+b); (3) return a*b; (4) double tx,ty; tx=rt.x+rt.a/2.0; 【试题解析】 本题主要考查考生对于类的定义和友元函数的定义的理解。注意 (4)中使用了求开平方的数学函数 sqrt。

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

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1