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

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

1、国家二级 C+机试(操作题)模拟试卷 492及答案与解析 一、基本操作题 1 请打开考生文件夹下的解决方案文件 proj1,该工程中包含程序文件main cpp,其中有类 Clock(“时钟 ”)的定义和主函数 main的定义。程序中位于每个 “ ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Initial times are 0 d: 0 h: 0 m: 59s After one second times are 0 d: 0 h: 1 m: 0S 注意:只修改每个 “ ERROR*found*”下的那一行,不要改动程序中的其他内容。 #inclu

2、de iostream using namespace std; class Clock public: Clock(unsigned long i=0); void set(unsigned long i=0); void print( )const; void tick( );时间前进一秒 Clock operator+( ); private: uns igned long total_sec, seconds, fminutes, hours, days; ; Clock: Clock(unsigned long i) : total sec(i), seconds(i 60), mi

3、nutes(i 60) 60), hours(i 3600) 24), days(i 86400) void Clock: set(unsigned long i) total sec=i; seconds=i 60; minutes=(i 60) 60; hours=(i 3600) 60; days=i 86400; ERROR*found* voidClock: print( ) cout days “d: “ hours “h: “ minutes “m: “ seconds “s“ endl; void Clock: tick( ) ERROR*found* set(total_se

4、c+); Clock Clock: operator+( ) tick( ); ERROR*found* return this; int main( ) Clock ck(59); tout “Initial times are“ endl; ck print( ); +ck; tout “After One second times are“ endl; ck print( ); return0; 二、简单应用题 2 请打开考生文件夹下的解决方案文件 proj2,此工程包含一个源程序文件proj2 cpp。其中定义了 Score类。 Score是一个用于管理考试成绩的类。其中 ,数据成员

5、_s指向存储成绩的数组, _n表示成绩的个数;成员函数 Sort使用冒泡排序法将全部成绩按升序进行排列。 请在程序中的横线处填写适当的代码,然后删除横线,以实现 Score类的成员函数Sort。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动 “ *found*”。 proj2 cpp #include iostream #include cstdlib #include ctime using namespace std; class score public: Score(double*s, intn): _s(s), _n(n) double GetScore

6、(int i)COBst return_si; void sort( ); private: double*_s; int_n; ; void Score: Sort( ) *found* for(int i=0; i _n-1; _) *found* for(int j=_; j i; j-) if(_sj _sj-1) 交换 _sj和 _sj-1 double t=_sj; *found* _; *found* _; int main( ) conSt int NUN=10; double sNUN; stand(time(0); for(int i=0; i NUN; i+) si=do

7、uble(rand( ) RAND MAX*100; Score ss(s, NUM); ss Sort( ); for(int j=0; j NUM; j+) cout ss GetScore(j) endl; return0; 三、综合应用题 3 请打开考生文件夹下的解决方案文件 proj3,其中声明的 DataList类,是一个用于表示数据表的类。 sort成员函数的功能是将当前数据表中的元素升序排列。请编写这个 sort函数。程序的正确输出应为: 排序前: 7, 1, 3, 11, 6, 9, 12, 10, 8, 4, 5, 2 排序后: 1, 2, 3, 4, 5, 6, 7, 8

8、, 9, 10, 11, 12 要求: 补充编制的内容写在 “ *333*”与 “ *666*”两行之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out dat中。输出函数 writeToFile已经编译为obj文件,并且在本程序调用。 DataList h #include iostream using namespace std; class DataList数据表类 int len; double*d; public: DataList(int len, double data =NULL); DataList( )delete d; int length( )cons

9、treturnlen; 数据表长度 (即数据元素的个数 ) double getElement(int i)constreturn di; void sort( );数据表排序 void show( )const;显示数据表 ; void writeToFile(char*, constDataList&); main cpp #include“DataList h“ DataList: DataList(intlen, double data ): len(len) d=new doublelen; for(int i=0; i len; i+) di=(data=NULL?0 0: dat

10、ai); void DataList: sort( )数据表排序 *333* *666* void DataList: show( )const显示数据表 for(int i=0; i len-1; i+)tout di “, “; tout dlen-1 endl; int main( ) double s =7, 1, 3, 11, 6, 9, 12, 10, 8, 4, 5, 2; DataList list(12, s); cout “排序前: “; list show( ); liSt sort( ); cout endl “排序后: “; list show( ); writeTo

11、File(“ “, liSt); return0; 国家二级 C+机试(操作题)模拟试卷 492答案与解析 一、基本操作题 1 【正确答案】 (1)void Clock: print( )const (2)set(+total_see); (3)return*this; 【试题解析】 (1)主要考查考生对成员函数的掌握,由 Clock类中对函数 print的声明 void print( )const;可知,在定义 print函数时少了 const。 (2)主要考查考生对 +操作的掌握,根据函数要求,时间要先前进一秒,再调用函数 set,因此 total_sec+应改为 +total_sec.

12、(3)主要考查考生对 this指针的掌握,函数要求返回值 Clock,即返回一个类,而不是指针,因此使用 *this。 二、简单应用题 2 【正确答案】 (1)i+ (2)_n-1 (3)_sj=_sj-1 (4)_sj-1=t 【试题解析】 (1)主要考查 for循环语句,从题目要求可知循环变量 i要从 0到 _n-2,因此 i要递增操作,即 i+。 (2)主要考查考生对冒泡排序的掌握,这里要求从后往前扫描,比较相邻两个元素,若后者小则交换,因此在这里下标 j要从最后开始,即 int_i=_n-1。 (3)考查交换算法,在 if语句中 _sj _sj-1满足条件,则实现交换。因为已经把_sj

13、的值赋给了中间变量 t,所以这里要把 _sj-1的值赋给 _sj,即 _sj=_sj-1;。 (4)考查交换算法,这里只需把中间变量 t中的值赋给 _sj-1即可。 三、综合应用题 3 【正确答案】 for(int i=0; 1 len; +i)从头遍历数组 d for(int j=i; j len; +j)从 i+1处遍历数组 d if(di dj) di和 dj比较人,如果大于,就 di和 dj值交换 int temp=di;把临时整型变量 temp赋值为 di di=dj;把 dj赋值给 di dj=temp;把 temp值赋给 dj 【试题解析】 本题使用最简单的冒泡排序算法,首先明确要排序的动态数组 d,其长度为 len,在此可以使用两个下标 i和 j相比较,当 di dj时,数组内的值利用中间变量 temp进行交换。

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

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

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