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