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

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

1、国家二级 C+机试(操作题)模拟试卷 499及答案与解析 一、基本操作题 1 请打开考生文件夹下的解决方案文件 proj1,其中在编辑窗口内显示的主程序文件中定义有类 AAA和主函数 main。程序文本中位于每行 “ERROR*found*”下面的一行有错误,请加以改正。改正后程序的输出结果应该是: sum=60 注意:只修改每个 “ ERROR*found*”下面的一行,不要改动程序中的其他任何内容。 #include iostream using namespace std; class AAA int a10; int n; ERROR*found* private: AAA(int a

2、a , int nn): n(nn) ERROR*found* for(int i=0; i rl; i+)aai=ai; int Geta(int i)return ai; ; int main( ) int a6=2, 5, 8, 10, 15, 20; AAA x(a, 6); int sum=0; ERROR*found* for(int i=0; i 6; i+) sum+=x ai; cout “sum=“ sum endl; return0; 二、简单应用题 2 请打开考生文件夹下的解决方案文件 proj2,此工程中包含一个头文件shape h,其中包含了类 Shape、 Poi

3、nt和 Triangle的声明;包含程序文件shape cpp,其中包含了类 Triangle的成员函数和其他函数的定义;还包含程序文件 proj2 cpp,其中包含测试类 Shape、 Point和 Triangle的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为: 此图形是一个抽象图形,周长 =0,面积 =0 此图形是一个三角形,周长 =6 82843,面积 =2 注意:只能在横线处填写适当的代码,不要改动 程序中的其他内容,也不要删除或移动 “ *found*”。 shape h class Shape public: virtual do

4、uble perimeter( ) constreturn0; 返回形状的周长 virtual doublearea( )constreturn0; 返回形状的面积 virtual const char*name( )constreturn“抽象图形 “; 返回形状的名称 ; class Point表示平面坐标系中的点的类 double x; double y; public: *found* Point(double x0, doub2e y0): _用 x0、 y0初始化数据成员 x、 y double getX( )constreturn x; double getY( )constre

5、turn y; ; class Triangle: public Shape *found* _; 定义 3个私有数据成员 public: Triangle(Point p1, Point p2, Point p3): point1(p1), point2(p2), point: 3(p3) double perimeter( )const; double area( )const; const char*narne( )constreturn“三角形 “; ; shape cpp #include“shape h“ include cmath double length(Point p1,

6、Pointp2) return sqrt(p1 getX( )-p2 getX( )*(p1 getX( )-p2 getX( )+(p1 getY( )-p2 getY( )*(p1 getY( )-p2 getY( ); double Triangle: perimeter( )const 一个 return语句,它利用 length函数计算并返回三角形的周长 *found* _; double Triangle: area( )const double s=perimeter( ) 2 0; return sqrt(s*(s-length (point1, point2)* (s-len

7、gth(point2, point3)* (s-length(point3, pointl); proj2 cpp #include“shape h“ #include iostream using namespace std; *found* _ show函数的函数头 (函数体以前的部分 ) cout “此图形是一个 “ shape name( ) “,周长 =“ shape perimeter( ) “,面积: “ shape area( ) endl; int msin( ) Shape s; Triangle tri(Point(0, 2), Point(2, 0), Point(0,

8、 0); show(s); Show(tri); return0; 三、综合应用题 3 请打开考生文件夹下的解决方案文件 proj3,其中包含主程序文件 main cpp和用户定义的头文件 Array h,整个程序包含有类 Array的定义和主函数 main的定义。请把主程序文件中的 Array类的成员函数 Contrary( )的定义补充完整,经补充后运行程序,得到的输出结果应该是: 5 8 5, 4, 3, 2, 1 0, 0, 8 4, 5 6, 4 5, 3 4, 2 3, 1 2 注意:只允许在 “ *333*”和 “ *666*”之间填写内容,不允许修改其他任何地方的内容。 Arr

9、ay h #include iostream using namespace std; template class Type, int m class Array数组类 public: Array(Typeb , int mm)构造函数 for(int i=0; i m; i+) if(i mm)ai=bi; elseai=0; void Contrary( ); 交换数组 a中前后位置对称的元素的值 int Length( )constreturn m; 返回数组长度 Type operator (int i)const 下标运算符重载为成员函数 if(i 0|i =m) cout “下标

10、越界 !“ endl; exit(1); return ai; private: Type am; ; void writeToFile(const char*); 不用考虑此语句 的作用 main cpp #include“Array h“ 交换数组 a中前后位置对称的元素的值 template class Type, int m void Array Type, m: Contrary( )补充函数体 *333* *666* int main( ) int s15=1, 2, 3, 4, 5; double s26=1 2, 2 3, 3 4, 4 5, 5 6, 8 4; Array i

11、nt, 5 d1(s1, 5); Array double, 8 d2(s2, 6); int i; d1 Contrary( ); d2 Contrary( ); cout d1 Length( ) d2 Length( ) endl; for(i=0; i 4; i+) cout d1i “, “; cout d14 endl; for(i=0; i 7; i+) cout d2i “, “; cout d27 endl; writeToFile(“ “); 不用考虑此语句的作用 return0; 国家二级 C+机试(操作题)模拟试卷 499答案与解析 一、基本操作题 1 【正确答案】 (

12、1)public: (2)for(int i=0; i n; i+)ai=aai; (3)for(int i=0; i 6; i+)sum+=x Geta(i); 【试题解析】 (1)构造函数肯定是公有成员,所以应使用 public。 (2)主要考查考生对赋值语句的掌握,因为数组 a是私有成员,因此 a应该在左边,而数组 aa是形参,要赋值给数组 a。 (3)主要考查考生对成员函数调用的 掌握,数组 a是类的私有成员,因此不能被main函数直接调用,要通过成员函数 Geta来调用数组 a。 二、简单应用题 2 【正确答案】 (1)x(x0), y(y0) (2)Point point1, po

13、int2, point3 (3)return length(point1, point2)+length(point1, point3)+length(point2, point3) (4)void show(Shape&shape) 【试题解析】 (1)主要考查考生对构造函数的掌握,题目要求用 x0、 y0初始 化数据成员 x、 y,因此在这里使用成员列表初始化,即 Point(double x0, double y0):X(x0), y(y0)。 (2)主要考查考生对构造函数的掌握,题目要求定义 3个私有数据成员。由构造函数可知 3个私有数据成员的类型都是 Point,名称分别为 poin

14、t1、 point2和point3,因此空格处填写: Point point1, point2, point3。 (3)主要考查考生对成员函数的掌握,题目要求使用 return语句,利用 length函数计算并返回三角形的周长。 length函数返回的是 两点间的距离,因此 return语句只要返回三角形三条边的距离和,即为三角形的周长。 (4)主要考查考生对成员函数的掌握,这里要定义 show函数的函数头 (函数体以前的部分 )。由主函数 main中 show函数的使用情况 show(S)和 show(tri)可知, S是Shape类, tri是 Triangle类,因为 Triangle是

15、 Shape类的派生类,所以可知 show函数的参数是 Shape类型,无返回值,得出语句 void show(Shape&shape)。 三、综合应用题 3 【正确答案】 for(int i=0, j=m-1; i j; i+, j-) i从 0开始自加, j从数组最一位开始自减,条件是 i j,开始遍历数组 a Type temp=ai; 把 ai赋值给变量 temp ai=aj;把 aj赋值给 j ai=temp; 把 temp赋值给 aj,最终使 ai与 aj值的互换 【试题解析】 主要考查考生对交换算法的掌握情况,根据题目对要完成的函数Contrary的要求:交换数组 a中前后位置对称的元素的值。这里取下标 j=0,即为数组中的第一个元素, j=m-1, 即为数组中的最后一个元素,利用中间值 temp交换元素的值,然后 i逐次递增的同时 j逐次递减,再交换,循环到 i j时停止交换即可。

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

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

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