1、国家二级 C+机试(操作题)模拟试卷 328及答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.请使用 VC6或使用【答题】菜单打开考生文件夹 projl下的工程 projl,该工程中包含程序文件maincpp,其中有类 Clock(“时钟”)的定义和主函数 main的定义。程序中位于每个“ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Initial times are 0 d:0 h:0 m:59 s After one second times are 0 d:0 h:1 m:0 s 注意:只修改每个“
2、ERROR*found*”下的那一行,不要改动程序中的其他内容。#includeiostreamusing namespace std;class Clockpublic: Clock(unsigned long i=0); void set(unsigned long i=0); void print()const; void tick();时间前进一秒 Clock operator+();private: unsigned long total_sec,seconds,minutes ,hours, days;Clock:Clock(unsigned long i) :total sec(
3、i),seconds(i60), minutes(i60)60), hours(i3 600)24), days(i8 64 00)void Clock:set(unsigned long i) total sec=i; seconds=i60; minutes=(i60)60; hours=(i3600)60; days=i864 00;ERROR*found*void Clock:print() toutdays”d:”hours”h:” minutes”m:”seconds”s”endl;void Clock:tick()ERROR*found* set(total sec+);Cloc
4、k Clock:operator+() tick();ERROR*found* return ths;int main() Clock ck(59); tout ”Initial timeS aEe” endl; ckprint(); +ck; tout ”After one second timesaEe”endl; ckprint(); return 0;(分数:2.00)_二、简单应用题(总题数:1,分数:2.00)2.使用 VC6打开考生文件夹 proj2下的工程 Proj2,其中有元素类 Element和队列类 Queue的定义。请在程序中的横线处填写适当的代码,然后删除横线,以实现
5、上述类定义。此程序的输出结果应为: 3 8 5 0 5 0 7 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。#includeiOStream#includecmath#includecstdlibusing namespace std;#define MaxLength i 00 class Element “元素”类 public: int n; Element(int i=0):n(i) ; class Queue “队列”类 Element * element; 指向存储元素的数组的指针 int tail;队尾元素的下标 public: Queue():element(n
6、ew Element100),tail(一 1) 一 Queue()deleteelement; void push(Element ele); 在队列尾端添加一个元素 Element pop();在队列首端删除一个元素,返回被删元素 Element front()constreturn element0;返回队首元素,但不从队列中删除该元素 *found* int size()const return(_);返回元素个数 void show()const,显示集合中所有元素;void Queue:push(Element ele) if(tail=MaxLength一 1) return;空
7、间满,不做任何处理 *found* _;Element Queue:pop() if(Size()=0)exit(1); 队列空,不做任何处理 Element tmp=element0; for(int i=0;itail;i+) elementi=elementi+1; *found* _; return tmp;VOid Queue:show ()const *found* for(_) coutelementin; coutendl;int main() Queue q; qpush(3); qpush(8); qpush(5); qpush(0); qshow(); qpop(); q
8、pop(); qpush(7); qshow(); return 0;(分数:2.00)_三、综合应用题(总题数:1,分数:2.00)3.请使用 VC6或使用【答题】菜单打开考生文件夹 proj3下的工程 proj3,其中使用友元函数访问类的私有数据成员,求出两个数据成员的大于 1的最小公因子。请编写友员函数 FriFun,使其输出结果为:Commnn denominator is 2 要求:补充编制的内容写在“*333*”与“*666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件outdat 中。输出函数 writeToFile已经编译为 0bj文件,并且在本程序中调用。
9、proj3h clasS FriFunClass int a,b;public: EriFunClass(int i,int j)a=i;b=j;) friend int FriFun(FriFunClass x); /友元函数 ; void writeToFile(const char*);proj3cpp#includeiostreamusing namespace std;#include”prj3h”int FriFun(FriFunClass X)/*333* 由于函数 FriFun()是类 FriFunclass的友元函数,所以它可以直接访问 a和 b*666*int main()
10、 FriFunClass n(10,20);if(FriFun(n) tout;”Common denominat:or iS”FriFun(n) ”n”; else cout:”No commom denominatorn”; writeToFile(”); return 0; (分数:2.00)_国家二级 C+机试(操作题)模拟试卷 328答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.请使用 VC6或使用【答题】菜单打开考生文件夹 projl下的工程 projl,该工程中包含程序文件maincpp,其中有类 Clock(“时钟”)的定义和主
11、函数 main的定义。程序中位于每个“ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Initial times are 0 d:0 h:0 m:59 s After one second times are 0 d:0 h:1 m:0 s 注意:只修改每个“ERROR*found*”下的那一行,不要改动程序中的其他内容。#includeiostreamusing namespace std;class Clockpublic: Clock(unsigned long i=0); void set(unsigned long i=0); void pri
12、nt()const; void tick();时间前进一秒 Clock operator+();private: unsigned long total_sec,seconds,minutes ,hours, days;Clock:Clock(unsigned long i) :total sec(i),seconds(i60), minutes(i60)60), hours(i3 600)24), days(i8 64 00)void Clock:set(unsigned long i) total sec=i; seconds=i60; minutes=(i60)60; hours=(i3
13、600)60; days=i864 00;ERROR*found*void Clock:print() toutdays”d:”hours”h:” minutes”m:”seconds”s”endl;void Clock:tick()ERROR*found* set(total sec+);Clock Clock:operator+() tick();ERROR*found* return ths;int main() Clock ck(59); tout ”Initial timeS aEe” endl; ckprint(); +ck; tout ”After one second time
14、saEe”endl; ckprint(); return 0;(分数:2.00)_正确答案:(正确答案:(1)void Clock:print()const (2)set(+total_sec); (3)return*this:)解析:解析:(1)主要考查考生对成员函数的掌握,由 Clock类中对函数 print的声明 void print()const;可知,在定义 print函数时少了 const。 (2)主要考查考生对+操作的掌握,根据函数要求,时间要先前进一秒,再调用函数 set,因此 total_sec+应改为+total_sec。 (3)主要考查考生对 this指针的掌握,函数要求
15、返回值 Clock,即返回一个类,而不是指针,因此使用*this。二、简单应用题(总题数:1,分数:2.00)2.使用 VC6打开考生文件夹 proj2下的工程 Proj2,其中有元素类 Element和队列类 Queue的定义。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的输出结果应为: 3 8 5 0 5 0 7 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。#includeiOStream#includecmath#includecstdlibusing namespace std;#define MaxLength i 00 class Ele
16、ment “元素”类 public: int n; Element(int i=0):n(i) ; class Queue “队列”类 Element * element; 指向存储元素的数组的指针 int tail;队尾元素的下标 public: Queue():element(new Element100),tail(一 1) 一 Queue()deleteelement; void push(Element ele); 在队列尾端添加一个元素 Element pop();在队列首端删除一个元素,返回被删元素 Element front()constreturn element0;返回队首
17、元素,但不从队列中删除该元素 *found* int size()const return(_);返回元素个数 void show()const,显示集合中所有元素;void Queue:push(Element ele) if(tail=MaxLength一 1) return;空间满,不做任何处理 *found* _;Element Queue:pop() if(Size()=0)exit(1); 队列空,不做任何处理 Element tmp=element0; for(int i=0;itail;i+) elementi=elementi+1; *found* _; return tmp
18、;VOid Queue:show ()const *found* for(_) coutelementin; coutendl;int main() Queue q; qpush(3); qpush(8); qpush(5); qpush(0); qshow(); qpop(); qpop(); qpush(7); qshow(); return 0;(分数:2.00)_正确答案:(正确答案:(1)tail +1 (2)element+tail =ele (3)tail- (4)int i=0;i =tail ; i+)解析:解析:题意定义了 Element类,表示队列中的元素,它包含一个成员
19、 n,表示元素的值;定义了Queue类,表示队列,它包含两个成员:element 是动态分配的一个数组,每个元素都是 Element类型;tail表示队列尾部的下标值,指向 element数组的最后一个元素,当数组为空时,tail 的值为一 1,所以初始化为一 1;另外 Queue类还定义了若干成员函数:构造函数为 element 数组动态分配 100个元素空间,初始化 tail为一 1;析构函数释放 element;push()函数向队列中添加一个新元素 ele,即将形参ele存放到数组尾部,并将 tail自增 1;pop()函数将 element数组的第一个元素(下标为 0)从数组中删除(
20、采用移动后续所有元素的方式),并作为函数返回值返回;front()函数返回队列首元素;size()函数返回当前 element数组的元素个数;show()函数将 element数组中的元素的 n值输出,由上面的分析,补充代码如下: (1)size()函数返回 element数组的元素个数,tail 的值是 element数组的最后一个元素的下标,所以返回 tail+l即可。 (2)push()函数将参数 ele插入到队列的尾部,即将 ele保存到element数组下标为(tail+1)的位置:element+tail=ele; (3)poP()函数将 element数组的首元素保存到 tmp变
21、量中,然后遍历 element数组,将下标从 1开始的元素逐个赋给前一个元素,再把 tail的值自减 1,tail-; (4)show()函数需要遍历 element数组,然后将元素的 n值输出,遍历下标的变量需要定义为 i,i 初值从 0开始,直到 i等于最后一个元素的下标,即 tail。int i=0;i=tail;i+三、综合应用题(总题数:1,分数:2.00)3.请使用 VC6或使用【答题】菜单打开考生文件夹 proj3下的工程 proj3,其中使用友元函数访问类的私有数据成员,求出两个数据成员的大于 1的最小公因子。请编写友员函数 FriFun,使其输出结果为:Commnn deno
22、minator is 2 要求:补充编制的内容写在“*333*”与“*666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件outdat 中。输出函数 writeToFile已经编译为 0bj文件,并且在本程序中调用。proj3h clasS FriFunClass int a,b;public: EriFunClass(int i,int j)a=i;b=j;) friend int FriFun(FriFunClass x); /友元函数 ; void writeToFile(const char*);proj3cpp#includeiostreamusing names
23、pace std;#include”prj3h”int FriFun(FriFunClass X)/*333* 由于函数 FriFun()是类 FriFunclass的友元函数,所以它可以直接访问 a和 b*666*int main() FriFunClass n(10,20);if(FriFun(n) tout;”Common denominat:or iS”FriFun(n) ”n”; else cout:”No commom denominatorn”; writeToFile(”); return 0; (分数:2.00)_正确答案:(正确答案:int min=xaxb?xa:xb; 此处为取出 xa 与 xb 中的最小值for(int i=2;i=min ; i+) 从 i到 min遍历数组 if(xai =0&xbi =0) 如 i能同时整除 xa 来的 xb,则仅回 i return i; return 一 1;)解析:解析:主要考查考生对友元函数的掌握情况,友元函数可以访问类的私有数据成员,题目要求函数求出两个数据成员的大于 1的最小公因子,从 2开始往上算,因此要同时可以被两个私有成员整除,这里用取余符号完成,取余为 0即为整除。