1、二级 C+机试-172 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:33.00)1.使用 VC6 打开考生文件夹下的工程 RevProj12。此工程包含一个源程序文件 RevMain12.cpp,但在该程序中有错误。请改正程序中的错误,使它能得到正确结果。注意:不得删行或增行,也不得更改程序的结构。源程序文件 RevMain12.cpp 中的程序清单如下:/RevMain12.cpp#includeiostream/* * * * FOUND * * * * */using namespace std;class testprivate:const i
2、nt value;char dep10;public:/* * * * *FOUND* * * * */test()value=0;strcpy(dep,“m“);/* * * * *FOUND* * * * */test(int newvalue)value=newvalue;strcpy (dep, “m“);/* * * * *FOUND * * * * */void show()cout“value= “valueend1;int main ()test t1;const test t2;t1.show ();t2.show();return 0;(分数:33.00)_二、2简单应用题
3、(总题数:1,分数:33.00)2.请编写一个函数 void fun(int a ,int n),其中 a 为数组,n 为数组 a 的长度。函数 fun()的功能是冒泡排序法将数组 a 元素按从小到大的顺序排列,实现数组 a 的升序排列。注意:部分源程序已存在文件 PROC12.cpp 中。请勿修改主函数和其他函数中的任何内容,仅在函数 fun()的花括号中填写若干语句。文件 PROC12.cpp 的内容如下:/PROC12. cpp#include iostreamusing namespace std;#define MAX 100void fun(int a,int n);int mai
4、n ()int aMAX,n,i;cout“Please enter the array size n:/n“;do cinn;if (n100)cout“array size flowover! ReEnter a number(0-100)/n“;while (n100);cout“Enter the array data:/n“;for (i=0; in; i+)cina ii;fun(a,n);for (i=0; in; i+)couta i “ “;coutend1;return 0;void fun(int a ,int n)/ * * * * * * * *(分数:33.00)_
5、三、3综合应用题(总题数:1,分数:34.00)3.使用 VC6 打开考生文件夹下的工程 MyProj12。此工程包含一个源程序文件 MyMain12.cpp。程序中定义了两个类 Base 和 Derived,但类的定义并不完整。请按要求完成下列操作,将类的定义补充完成:类 Derived 是基类 Base 公有派生来的。请在注释“/* *1* *”之后添加适当的语句。完成构造函数 Derived(int x)定义,采用初始化列表的方式使基类 Base 私有成员 a 初始化为 x,类Derived 的私有成员 b 初始化为 x+1。请在注释“/* *2* *”之后添加适当的语句。完成类 Der
6、ived 的成员函数 show()的类体外的定义。函数 show()中要输出基类数据成员 a 的值,然后要输出私有成员 b 的值。请在注释“/* *3* *之后添加适当的语句。注意;除在指定位置添加语句之外,请不要改动程序中的其他内容。源程序文件 MyMain12.cpp 清单如下:/MyMain12.cpp#includeiostreamusing namespace std;class Basepublic:int a;Base(int i)a=i;/* * * 1 * * *private:int b;public:/* * * 2 * * *void show();void Deriv
7、ed:show()/* * * 3 * * *int main()Derived d(1);d.show();return 0;(分数:34.00)_二级 C+机试-172 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:33.00)1.使用 VC6 打开考生文件夹下的工程 RevProj12。此工程包含一个源程序文件 RevMain12.cpp,但在该程序中有错误。请改正程序中的错误,使它能得到正确结果。注意:不得删行或增行,也不得更改程序的结构。源程序文件 RevMain12.cpp 中的程序清单如下:/RevMain12.cpp#includeiost
8、ream/* * * * FOUND * * * * */using namespace std;class testprivate:const int value;char dep10;public:/* * * * *FOUND* * * * */test()value=0;strcpy(dep,“m“);/* * * * *FOUND* * * * */test(int newvalue)value=newvalue;strcpy (dep, “m“);/* * * * *FOUND * * * * */void show()cout“value= “valueend1;int main
9、 ()test t1;const test t2;t1.show ();t2.show();return 0;(分数:33.00)_正确答案:(正确的类定义为:#includeiostream#includestringusing namespace std;class testprivate:const int value;char dep10;public:test():value(0)strcpy(dep,“m“);test(Int newvalue):value(newvalue)strcpy(dep,“m“);void show() constcout“value=“valueend
10、1;)解析:解析 该程序类定义中有以下错误:类中的常量成员 value 的初始化工作应放在构造函数的初始化列表中,所以两个构造函数的定义应该改为;test():value(0) strcpy(dep,“m“);test(int newvalue):value(newvalue) strcpy(dep,“m“);程序中用到了库函数 strcpy(),因此需要在程序的开头处加上:#includestring。程序的主函数中定义了常对象 t2,因此借助对象 t2 不能调用非常成员函数 show(),为保证程序能够正常运行,可将成员函数 show()改为常成员函数。二、2简单应用题(总题数:1,分数:
11、33.00)2.请编写一个函数 void fun(int a ,int n),其中 a 为数组,n 为数组 a 的长度。函数 fun()的功能是冒泡排序法将数组 a 元素按从小到大的顺序排列,实现数组 a 的升序排列。注意:部分源程序已存在文件 PROC12.cpp 中。请勿修改主函数和其他函数中的任何内容,仅在函数 fun()的花括号中填写若干语句。文件 PROC12.cpp 的内容如下:/PROC12. cpp#include iostreamusing namespace std;#define MAX 100void fun(int a,int n);int main ()int aM
12、AX,n,i;cout“Please enter the array size n:/n“;do cinn;if (n100)cout“array size flowover! ReEnter a number(0-100)/n“;while (n100);cout“Enter the array data:/n“;for (i=0; in; i+)cina ii;fun(a,n);for (i=0; in; i+)couta i “ “;coutend1;return 0;void fun(int a ,int n)/ * * * * * * * *(分数:33.00)_正确答案:(函数 f
13、un()的定义如下:void fun(int a ,int n)int F=n;while(F0)int k=F-1; F=0;for(int j=1;j=k;j+)if(ajaj+1)int temp=ai;aj=ai+1;aj+1=temp;F=j;return;)解析:解析 将待排序的数存放于数组 a 中,首先比较 a1和 a2,如果 91a2,则交换这两个元素的值,接着比较 a2和 a3,此时的 a2可能是刚交换来的值,若 a12)a3,则交换这两个元素的值,依此类推,直到处理完 an-1)和 an这两个元素的比较。经过了 n-1 次的比较处理,最大的数被传到数组最后一个元素中,而较小
14、的数就会像气泡一样上浮。在上述算法的实现函数中,k 表示每遍扫描时需要进行比较的项目数。当 F0 时,表示在上遍扫描过程中曾经发生过交换,还需要进行扫描。F 的数值表示上一遍扫描过程中最后一次发生交换的位置。三、3综合应用题(总题数:1,分数:34.00)3.使用 VC6 打开考生文件夹下的工程 MyProj12。此工程包含一个源程序文件 MyMain12.cpp。程序中定义了两个类 Base 和 Derived,但类的定义并不完整。请按要求完成下列操作,将类的定义补充完成:类 Derived 是基类 Base 公有派生来的。请在注释“/* *1* *”之后添加适当的语句。完成构造函数 Der
15、ived(int x)定义,采用初始化列表的方式使基类 Base 私有成员 a 初始化为 x,类Derived 的私有成员 b 初始化为 x+1。请在注释“/* *2* *”之后添加适当的语句。完成类 Derived 的成员函数 show()的类体外的定义。函数 show()中要输出基类数据成员 a 的值,然后要输出私有成员 b 的值。请在注释“/* *3* *之后添加适当的语句。注意;除在指定位置添加语句之外,请不要改动程序中的其他内容。源程序文件 MyMain12.cpp 清单如下:/MyMain12.cpp#includeiostreamusing namespace std;class
16、 Basepublic:int a;Base(int i)a=i;/* * * 1 * * *private:int b;public:/* * * 2 * * *void show();void Derived:show()/* * * 3 * * *int main()Derived d(1);d.show();return 0;(分数:34.00)_正确答案:(class Derived:public BaseDerived(int x):Base(x),b(x+1)coutBase: :a;coutbend1;)解析:解析 程序中定义了两个类 Base 和 Derived,类 Derived 是类 Base 的公有派生类。