1、二级 C+分类模拟 116 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: NUM=0 Value=1 注意:只修改注释“/ ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class M
2、yClass int_i; friend void Increment(MyClass public: const int NUM; / ERROR *found* MyClass(int i=0) NUM = 0; _i = i; int GetValue() const return _i; ; / ERROR *found* void Increment() f._i +; int main() MyClass obj; / ERROR *found* MyClass:Increment (obj); cout “NUM = “ obj.NUM endl “Value = “ obj.G
3、etValue() endl; return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,此工程包含一个源程序文件 proj2.cpp。其中定义了 Score 类。 Score 是一个用于管理考试成绩的类。其中,数据成员_s 指向存储成绩的数组,_n 表示成绩的个数;成员函数 Sort 使用冒泡排序法将全部成绩按升序进行排列。 请在程序中的横线处填写适当的代码,然后删除横线,以实现 Score 类的成员函数 Sort。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除
4、或移动“/ *found*”。 / proj2.cpp #include iostream #include cstdlib #include ctime using namespace std; class Score public: Score(double * s, int n) : _s(s), _n(n) double GetScore(int i) constreturn_si; void Sort(); private: double *_s; int _n; ; void Score:Sort() / *found* for (int i = 0; i _n-1; _) / *
5、found* for (int j =_; j i; j-) if (_sj _sj -i) / 交换_sj和_sj-1 double t = _sj; / *found* _; / *found* _; int main() const int NUM = 10; double sNUM; srand(time(0); for (int i=0; iNUM; i+) si = double (rand()/RAND_MAX * 100; Score ss(s, NUM); ss.Sort(); for (int j = 0; jNUM; j +) cout ss.GetScore(j) en
6、dl; return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 prog3,其中声明了 ValArray 类,该类在内部维护一个动态分配的整型数组。ValArray 类的复制构造函数应实现对象的深层复制。请编写 ValArray 类的复制构造函数。在 main 函数中给出了一组测试数据,此种情况下程序的输出应该是: ValArray v1=1,2,3,4,5 ValArray v2=2,2,2,2,2 要求: 补充编制的内容写在“/ *333*”与“/ *666*”之间。不要修改程序的其他部分。
7、注意: 相关文件包括:main.cpp、ValArray.h。 程序最后调用 writeToFile 函数,使用另一组不同的测试数据,将不同的运行结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件。 /ValArray.h #include iostream using namespace std; class ValArray int * v; int size; public: ValArray (const int * p, int n) : size(n) v = new intsize; for (int i = 0; i size; i
8、+) vi = pi; ValArray (const ValArray ValArray() delete v; void setElement(int i, int val) vi = val; void print(ostream for (int i = 0; i size - 1; i +) out vi “,“; out vsize-1 “; ; void writeToFile(const char *); /main.cpp #include “ValArray.h“ ValArray: ValArray (const ValArray ValArray v1(a, 5); V
9、alArray v2(v1); for (int i=0; i5; i+) v2.setElement(i,2); cout “ValArray v1 = “; v1.print(cout); cout endl; cout “ValArray v2 = “; v2.print(cout); cout endl; writeToFile(“); return 0; (分数:40.00)_二级 C+分类模拟 116 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程
10、中含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: NUM=0 Value=1 注意:只修改注释“/ ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class MyClass int_i; friend void Increment(MyClass public: const int NUM; / ERROR *found* MyClass(int i=0) NUM =
11、0; _i = i; int GetValue() const return _i; ; / ERROR *found* void Increment() f._i +; int main() MyClass obj; / ERROR *found* MyClass:Increment (obj); cout “NUM = “ obj.NUM endl “Value = “ obj.GetValue() endl; return 0; (分数:30.00)_正确答案:()解析:(1)MyClass(inti=0):NUM(0) (2)void Increment(MyClass class S
12、core public: Score(double * s, int n) : _s(s), _n(n) double GetScore(int i) constreturn_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 -i) / 交换_sj和_sj-1 double t = _sj; / *found* _; / *found* _; int
13、 main() const int NUM = 10; double sNUM; srand(time(0); for (int i=0; iNUM; i+) si = double (rand()/RAND_MAX * 100; Score ss(s, NUM); ss.Sort(); for (int j = 0; jNUM; j +) cout ss.GetScore(j) endl; return 0; (分数:30.00)_正确答案:()解析:(1)i+ (2)_n-1 (3)_sj=_sj-1 (4)_sj-1=t 答案考生文件夹 考点 本题考查 Score 类,其中涉及构造函数、
14、成员函数和排序算法。 解析 (1)主要考查 for 循环语句,从题目要求可知循环变量 i 要从 0 到_n-2,因此 i 要递增操作,即i+。 (2)主要考查考生对冒泡排序的掌握,这里要求从后往前扫描,比较相邻两个元素,若后者小则交换,因此在这里下标 j 要从最后开始,即 int j=_n-1。 (3)考查交换算法,在 if 语句中_sj_sj-1满足条件,则实现交换。因为已经把_sj的值赋给了中间变量 t,所以这里要把_sj-1的值赋给_sj,即_sj=_sj-1;。 (4)考查交换算法,这里只需把中间变量 t 中的值赋给_sj-1即可。 本题考查的是考生对冒泡排序算法的掌握,要记住冒泡排序
15、的思想是两两比较待排序序列中的元素,并交换不满足顺序要求的各对元素,直到全部满足顺序要求为止。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 prog3,其中声明了 ValArray 类,该类在内部维护一个动态分配的整型数组。ValArray 类的复制构造函数应实现对象的深层复制。请编写 ValArray 类的复制构造函数。在 main 函数中给出了一组测试数据,此种情况下程序的输出应该是: ValArray v1=1,2,3,4,5 ValArray v2=2,2,2,2,2 要求: 补充编制的内容写在“/ *333*”与“/
16、*666*”之间。不要修改程序的其他部分。 注意: 相关文件包括:main.cpp、ValArray.h。 程序最后调用 writeToFile 函数,使用另一组不同的测试数据,将不同的运行结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件。 /ValArray.h #include iostream using namespace std; class ValArray int * v; int size; public: ValArray (const int * p, int n) : size(n) v = new intsize; for
17、(int i = 0; i size; i +) vi = pi; ValArray (const ValArray ValArray() delete v; void setElement(int i, int val) vi = val; void print(ostream for (int i = 0; i size - 1; i +) out vi “,“; out vsize-1 “; ; void writeToFile(const char *); /main.cpp #include “ValArray.h“ ValArray: ValArray (const ValArra
18、y ValArray v1(a, 5); ValArray v2(v1); for (int i=0; i5; i+) v2.setElement(i,2); cout “ValArray v1 = “; v1.print(cout); cout endl; cout “ValArray v2 = “; v2.print(cout); cout endl; writeToFile(“); return 0; (分数:40.00)_正确答案:()解析:size = other.size; /把对象数组的大小赋值给 Size v = new int other.size; /根据对象数组的大小动态
19、分配数组 V for (int i = 0; i size; +i) vi = other.vi; /遍历整个对象的数组把值 other.vi放到数组 v 中 答案考生文件夹 考点 本题主要考查的是 ValArray 类,其中涉及构造函数、成员函数和析构函数。题目要求编写 ValArray 类的复制构造函数,以实现对象的深层复制。即填写 ValArray:ValArray(const ValArray& other)函数的函数体。 解析 主要考查考生对复制构造函数的掌握。由函数名:ValArray:ValArray(const ValArray& other),知道要复制的对象是 other,对由 ValArray 类的成员:int*v;int size;知道要复制的内容是动态数组v 及整型变量 size。动态数组要使用 new 语句分配内存,最后利用 for 循环语句来完成复制过程。