1、二级 C+分类模拟 123 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,其中有枚举 DOGCOLOR、狗类 Dog 和主函数 main 的定义。程序中位于每个“/ ERROR *found*”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: There is a white dog named Hoho. There is a black dog named Haha. There is a motley dog named Hihi. 注意:只修改每
2、个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream using namespace std; /狗的颜色:黑、白、黄、褐、花、其他 enum DOGCOLOR BLACK, WHITE, YELLOW, BROWN, PIEBALD, OTHER; class Dog /狗类 DOGCOLOR color; char name20; static int count; public: Dog (char name, DOGCOLOR color) strcpy(this-name,name); / ERROR *found* strc
3、py (this - color, color); DOGCOLOR getColor() const return color; / ERROR *found* const char * getName() const return * name; const char * getColorString() const switch(color) case BLACK: return “black“; case WHITE: return “white“; case YELLOW: return “yellow“; case BROWN: return “brown“; case PIEBA
4、LD: return “piebald“; return “motley“; void show() const cout “There is a“ getColorString() “dog named“ name “.“ endl; ; int main() / ERROR *found* Dog dog1(“Hoho“, WHITE), dog2(“Haha“, BLACK); dog3(“Hihi“, OTHER); dog1.show(); dog2.show(); dog3.show(); return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.
5、请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,该工程中包含一个程序文件 main.cpp,其中有坐标点类 point、线段类 Line 和三角形类 Triangle 的定义,还有 main 函数的定义。程序中两点间距离的计算是按公式 实现的,三角形面积的计算是按公式 实现的,其中 (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中声明的 DataList 类,是一个用于表示数据表的类。sort 成员函数的功能是将当前数据表中的元素升序排列。请编写这个 sort 函数。
6、程序的正确输出应为: 排序前:7,1,3,11,6,9,12,10,8,4,5,2 排序后:1,2,3,4,5,6,7,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(i
7、nt len, double data = NULL); DataList() deleted; int length() const return len; /数据表长度(即数据元素的个数) double getElement (int i) const return di; void sort(); /数据表排序 void show() const; /显示数据表 ; void writeToFile (char *, const DataList /main.cpp #include “DataList.h“ DataList:DataList (int len, double data
8、):len(len) d = new doublelen; for(int i = 0; i len; i+) di = (data = NULL? 0.0:datai); void DataList:sort() /数据表排序 / *333* / *666* void DataList:show() const /显示数据表 for (int i = 0; i len - 1; i+) cout di “,“; cout dlen - 1 endl; int main() double s = 7,1,3,11,6,9,12,10,8,4,5,2; DataList list(12,s);
9、cout “排序前:“; list.show(); list.sort(); cout endl “排序后:“; list.show(); writeToFile(“ “, list); return 0; (分数:40.00)_二级 C+分类模拟 123 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,其中有枚举 DOGCOLOR、狗类 Dog 和主函数 main 的定义。程序中位于每个“/ ERROR *found*”下的语句行有错误,请加以改正。改正后程序的输出
10、结果应该是: There is a white dog named Hoho. There is a black dog named Haha. There is a motley dog named Hihi. 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream using namespace std; /狗的颜色:黑、白、黄、褐、花、其他 enum DOGCOLOR BLACK, WHITE, YELLOW, BROWN, PIEBALD, OTHER; class Dog /狗类 DOGCOLOR color; c
11、har name20; static int count; public: Dog (char name, DOGCOLOR color) strcpy(this-name,name); / ERROR *found* strcpy (this - color, color); DOGCOLOR getColor() const return color; / ERROR *found* const char * getName() const return * name; const char * getColorString() const switch(color) case BLACK
12、: return “black“; case WHITE: return “white“; case YELLOW: return “yellow“; case BROWN: return “brown“; case PIEBALD: return “piebald“; return “motley“; void show() const cout “There is a“ getColorString() “dog named“ name “.“ endl; ; int main() / ERROR *found* Dog dog1(“Hoho“, WHITE), dog2(“Haha“,
13、BLACK); dog3(“Hihi“, OTHER); dog1.show(); dog2.show(); dog3.show(); return 0; (分数:30.00)_正确答案:()解析:(1)this-color=color; (2)const char getName() const return*name; (3)Dog dog1(“Hoho“,WHITE),dog2(“Haha“,BLACK),dog3(“Hihi“,OTHER); 答案考生文件夹 考点 主要考查的是 Dog 类,其中涉及 enum、静态私有成员、const 函数和构造函数。strcpy 函数用来复制字符串,
14、而对 double、int 等类型直接用“=”赋值即可。定义同一类型的变量时,几个变量之间用“,”分开。 解析 (1)主要考查考生对 strcpy 函数的掌握,如果看到上一条语句 strcpy(this-name,name);,就以为本条语句也要用 strcpy 函数来赋值,这是错误的。Strcpy 函数只能复制字符串,根据类的私有成员声明可知,color 是 DOGCOLOR 型的,这里直接使用赋值语句“=”即可。 (2)主要考查考生对函数返回值的掌握,先解读语句 const char*getName() const return*name;,要返回的是一个 const 的字符指针,同时函数
15、内的值不能改变,name 在类的私有成员声明中是个字符数组,*name 代表字符数组而不是字符指针,问题就出来了,需要修改返回类型:const char getName() const return*name;。 (3)语法错误,定义变量时,变量之间应使用“,”分开。二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,该工程中包含一个程序文件 main.cpp,其中有坐标点类 point、线段类 Line 和三角形类 Triangle 的定义,还有 main 函数的定义。程序中两点间距离的计算是按公式 实现的,三角形面积的
16、计算是按公式 实现的,其中 (分数:30.00)_正确答案:()解析:(1)const Point class DataList /数据表类 int len; double * d; public: DataList(int len, double data = NULL); DataList() deleted; int length() const return len; /数据表长度(即数据元素的个数) double getElement (int i) const return di; void sort(); /数据表排序 void show() const; /显示数据表 ; vo
17、id writeToFile (char *, const DataList /main.cpp #include “DataList.h“ DataList:DataList (int len, double data):len(len) d = new doublelen; for(int i = 0; i len; i+) di = (data = NULL? 0.0:datai); void DataList:sort() /数据表排序 / *333* / *666* void DataList:show() const /显示数据表 for (int i = 0; i len - 1
18、; i+) cout di “,“; cout 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(); writeToFile(“ “, list); return 0; (分数:40.00)_正确答案:()解析:for(int i = 0; i len; +i) /从头遍历数组 d for(int j = i; j len; +j) /从
19、i+1 处遍历数组 d if(di dj) /di和j比较人,如果大于,就 dj和 dj值交换 int temp = di; /把临时整型变量 temp 赋值为 di di = dj; /把 dj赋值给 di dj = temp; /把 temp 赋值给 dj 答案考生文件夹 考点 主要考查的是 DataList 类,其中涉及动态数组、构造函数、析构函数、const函数和排序算法。Sort 函数是一个排序函数,对于排序可以使用的方法很多,考生只需要使用自己最擅长的方法即可,题目并没有指定考生使用哪种方法。 解析 本题使用最简单的冒泡排序算法,首先明确要排序的动态数组 d,其长度为 len,在此可以使用两个下标 i 和 j 相比较,当 didj时,数组内的值利用中间变量 temp 进行交换。