【计算机类职业资格】C++语言笔试-9及答案解析.doc

上传人:figureissue185 文档编号:1318227 上传时间:2019-10-17 格式:DOC 页数:9 大小:42KB
下载 相关 举报
【计算机类职业资格】C++语言笔试-9及答案解析.doc_第1页
第1页 / 共9页
【计算机类职业资格】C++语言笔试-9及答案解析.doc_第2页
第2页 / 共9页
【计算机类职业资格】C++语言笔试-9及答案解析.doc_第3页
第3页 / 共9页
【计算机类职业资格】C++语言笔试-9及答案解析.doc_第4页
第4页 / 共9页
【计算机类职业资格】C++语言笔试-9及答案解析.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、C+语言笔试-9 及答案解析(总分:100.00,做题时间:90 分钟)一、操作题(总题数:3,分数:100.00)1.请使用 VC6 或使用答题菜单打开考生文件夹 proj1 下的工程 proj1。其中有线段类 Line 的定义。程序中位于每个“/ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:End point 1=(1,8),End point 2=(5,2),length=7.2111。注意:只修改每个“/ERROR *found*”下的那一行,不要改动程序中的其他内容。#include iostream#include cmathusing

2、namespace std;class Line;double length(Line);class Line /线段类double x1,y1; /线段端点 1double x2,y2; /线段端点 2public:/ERROR *found*Linedouble x1,double y1,doublex2,double y2)constthis-x1=x1;this-y1=y1;this-x2=x2;this-y2=y2;double getX1()constreturn x1;double getY1()constreturn y1;double getX2()constreturn x

3、2;double getY2()constreturn y2;void show () const cout“End point 1=(“x1“,“y1;cout“),End point 2=(“x2“,“y2;/ERROR *found*cout“),length=“length(this)“。“endl;double length (Line 1)/ERROR *found*return sqrt(1.x1-1.x2)*(1.x1-1.x2)+(1.y1-1.y2)*(1.y1-1.y2);int main () Line r1(1.0,8.0,5.0,2.0);r1.show();ret

4、urn 0;(分数:30.00)_2.请使用 VC6 或使用答题菜单打开考生文件夹 proj2 下的工程 proj2。其中有向量基类 VectorBase、向量类 Vector 和零向量类 ZeroVector 的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为:(1,2,3,4,5)(0,0,0,0,0,0)注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。#includeiostreamusing namespace std;class VectorBase /向量基类,一个抽象类int len;public

5、:VectorBase(int len):len(len)int length()constreturn len;/向量长度,即向量中元素的个数virtual double getElement(int i)const=0;/取第 i 个元素的值virtual double sum()const=0;/求所有元素的和void show()const/显示向量中所有元素cout“(“;for(int i=0;ilength()-1;i+)coutgetElement(i)“,“;/*found*cout_“)“endl;/显示最后一个元素;class Vector:public VectorBa

6、se/向量类double*val;public:Vector(int len,double v=NULL):VectorBase(len)val=new doublelen;for(int i=0;ilen;i+)vali=(v=NuLL?0.0:vi);/*found*Vector()_;double getElement(int index)const return val index;double sum()constdouble S=0.0;/*found*for(int i=0;ilength();i+)_;return s;class ZeroVector:public Vecto

7、rBase/零向量类public:ZeroVector(int len):VectorBasedouble getElement(int index)const_;double sum()constreturn 0.0;int main()VectorBase * V;double d=1,2,3,4,5;v=new Vector(5,d);v-show();delete V;V=new ZeroVector(6);V-show();delete V;return 0;(分数:30.00)_3.请使用 VC6 或使用答题菜单打开考生文件夹 proj3 下的工程 proj3,其中声明了 Sort

8、edList 类,是一个用于表示有序数据表的类。其成员函数 insert 的功能是将一个数据插入到一个有序表中,使得该数据表仍然保持有序。请编写这个 insert 函数。程序的正确输出应为:插入前:1,2,4,5,7,8,10插入 6 和 3 后:1,2,3,4,5,6,7,8,10要求:补充编制的内容写在“/*333*”与“/*666*”之间。不得修改程序的其他部分。注意:程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。/SortedList.h#includeiostreamusing namespace std

9、;class SortedList /有序数据表类int len;double*d;public:SortedList(int len,double data=NULL);SortedList()deleted;int length()constreturn len;/有序数据表长度(即元素的个数)double getElement(int i)constreturn di;void insert(double data);void show()const;/显示有序数据表;void writeToFile(char*,const Sort-edlist /main.cpp#include“S

10、ortedList.h“SortedList:SortedList (int len,double data):len(len)d=new doublelen;for(int k=0;klen;k+)dk=(data=NULL?0.0:datak);for(int i=0;ilen-1;i+) int m=i;for(int j=i;jlen;j+)if(djdm)m=j;double t=dm;dm=di;di=t;void SortedList:insert(double data)/*333*/*666*void SortedList:show()const/显示有序数据表for(int

11、 i=0;ilen-1;i+)coutdi“,“;coutdlen-1endl;int main () double s=5,8,1,2,10,4,7;SortedList list(7,s);cout“插入前:“endl;list.show();list.insert(6.0 );list.insert(3.0);list.show();writeToFile(“ “,list);return 0;(分数:40.00)_C+语言笔试-9 答案解析(总分:100.00,做题时间:90 分钟)一、操作题(总题数:3,分数:100.00)1.请使用 VC6 或使用答题菜单打开考生文件夹 proj1

12、 下的工程 proj1。其中有线段类 Line 的定义。程序中位于每个“/ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:End point 1=(1,8),End point 2=(5,2),length=7.2111。注意:只修改每个“/ERROR *found*”下的那一行,不要改动程序中的其他内容。#include iostream#include cmathusing namespace std;class Line;double length(Line);class Line /线段类double x1,y1; /线段端点 1double

13、x2,y2; /线段端点 2public:/ERROR *found*Linedouble x1,double y1,doublex2,double y2)constthis-x1=x1;this-y1=y1;this-x2=x2;this-y2=y2;double getX1()constreturn x1;double getY1()constreturn y1;double getX2()constreturn x2;double getY2()constreturn y2;void show () const cout“End point 1=(“x1“,“y1;cout“),End

14、point 2=(“x2“,“y2;/ERROR *found*cout“),length=“length(this)“。“endl;double length (Line 1)/ERROR *found*return sqrt(1.x1-1.x2)*(1.x1-1.x2)+(1.y1-1.y2)*(1.y1-1.y2);int main () Line r1(1.0,8.0,5.0,2.0);r1.show();return 0;(分数:30.00)_正确答案:(1)Line(double x1,double y1,double x2,double y2)(2)cout“),length=“

15、length(*this)“。“endl;(3)retum sqrt(1.getX1()-1.getX2()*(1.getX1()-1.getX2()+(1.getY1()-1.getY2()*(1.getY1()-1.getY2();)解析:解析 本题考查 Line 类,其中涉及构造函数、this 指针和 const 函数。判断一个函数是否为const 函数,就要观察该函数体内是否有变量值发生改变,若有变量值发生改变,则该函数不是 const 函数。一般构造函数不能用 const,因为要给私有成员赋初始值。类的私有成员不能被类外函数调用,只能通过成员函数调用。(1)主要考查考生对构造函数的掌

16、握,构造函数要给私有成员赋初始值,因此不能使用 const 来限制。(2)主要考查考生对 this 指针的掌握,由函数 length 的声明 double length(Line);可知,length 函数的形参是 Line 类,在 void show()const 函数里,this 指针指向的是当前 Line 类,因此可以用*this 表示当前 Line 类。(3)主要考查考生对成员函数的掌握,length 函数是类外函数,不能直接调用类的私有成员,因此要通过成员函数取得对应的值。this 是指向自身对象的指针,它是一个指针类型,因此要使用标识符“*”来取出它所指向的值。2.请使用 VC6

17、或使用答题菜单打开考生文件夹 proj2 下的工程 proj2。其中有向量基类 VectorBase、向量类 Vector 和零向量类 ZeroVector 的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为:(1,2,3,4,5)(0,0,0,0,0,0)注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。#includeiostreamusing namespace std;class VectorBase /向量基类,一个抽象类int len;public:VectorBase(int len):len(le

18、n)int length()constreturn len;/向量长度,即向量中元素的个数virtual double getElement(int i)const=0;/取第 i 个元素的值virtual double sum()const=0;/求所有元素的和void show()const/显示向量中所有元素cout“(“;for(int i=0;ilength()-1;i+)coutgetElement(i)“,“;/*found*cout_“)“endl;/显示最后一个元素;class Vector:public VectorBase/向量类double*val;public:Vec

19、tor(int len,double v=NULL):VectorBase(len)val=new doublelen;for(int i=0;ilen;i+)vali=(v=NuLL?0.0:vi);/*found*Vector()_;double getElement(int index)const return val index;double sum()constdouble S=0.0;/*found*for(int i=0;ilength();i+)_;return s;class ZeroVector:public VectorBase/零向量类public:ZeroVector

20、(int len):VectorBasedouble getElement(int index)const_;double sum()constreturn 0.0;int main()VectorBase * V;double d=1,2,3,4,5;v=new Vector(5,d);v-show();delete V;V=new ZeroVector(6);V-show();delete V;return 0;(分数:30.00)_正确答案:(1)getElement(length()-1)(2)deleteval(3)s+=vali(4)return 0.0;)解析:解析 主要考查的是

21、抽象基类 VectorBase 类及其派生类 Vector 和 ZeroVector,其中涉及构造函数、纯虚函数、coiist 函数、动态数组和析构函数。(1)主要考查考生对成员函数的掌握,题目要求显示最后一个元素。前面有纯虚函数 virtual double getElement(int i)const=0,因此可以直接调用 getElement 函数来取得最后一个元素,注意最后一个元素位置是 Length() -1 而不是 Length()。(2)主要考查考生对析构函数的掌握,前面定义了类的私有成员*val,因此析构函数要释放 val,使用delete 语句完成。(3)主要考查考生对 fo

22、r 循环的掌握,由函数名 doublesum()const 可知,该函数要求元素之和,for 循环语句的作用是遍历整个数组,在此使用语句 s+=vali完成程序。(4)主要考查考生对成员函数的掌握,由该类的注释:零向量类,可以了解到该类的元素都为零,因此无论要取第几个元素都返回 0,由于数据类型为 double,所以为 return0.0。3.请使用 VC6 或使用答题菜单打开考生文件夹 proj3 下的工程 proj3,其中声明了 SortedList 类,是一个用于表示有序数据表的类。其成员函数 insert 的功能是将一个数据插入到一个有序表中,使得该数据表仍然保持有序。请编写这个 in

23、sert 函数。程序的正确输出应为:插入前:1,2,4,5,7,8,10插入 6 和 3 后:1,2,3,4,5,6,7,8,10要求:补充编制的内容写在“/*333*”与“/*666*”之间。不得修改程序的其他部分。注意:程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。/SortedList.h#includeiostreamusing namespace std;class SortedList /有序数据表类int len;double*d;public:SortedList(int len,double da

24、ta=NULL);SortedList()deleted;int length()constreturn len;/有序数据表长度(即元素的个数)double getElement(int i)constreturn di;void insert(double data);void show()const;/显示有序数据表;void writeToFile(char*,const Sort-edlist /main.cpp#include“SortedList.h“SortedList:SortedList (int len,double data):len(len)d=new doublel

25、en;for(int k=0;klen;k+)dk=(data=NULL?0.0:datak);for(int i=0;ilen-1;i+) int m=i;for(int j=i;jlen;j+)if(djdm)m=j;double t=dm;dm=di;di=t;void SortedList:insert(double data)/*333*/*666*void SortedList:show()const/显示有序数据表for(int i=0;ilen-1;i+)coutdi“,“;coutdlen-1endl;int main () double s=5,8,1,2,10,4,7;S

26、ortedList list(7,s);cout“插入前:“endl;list.show();list.insert(6.0 );list.insert(3.0);list.show();writeToFile(“ “,list);return 0;(分数:40.00)_正确答案:(for(int i=0;ilen;+i)/遍历数组 dif(datadi)/如果 data 小于 dilen+; /数组 d 的长度自加 1douole*dd=new doublelen;/分配长度为 len 空间for(int k=len;ki;k-)/在数组 d 中从 k 等于 len 到 i 做遍历ddk=d

27、k-1;/把 dk-1赋值给 ddkddi=data;/把 data 赋值给 ddifor(int j=0;ji;j+)/把数组 d 从 0 到 i 做遍历ddj=dj;/把 dj赋值给 ddjdeleted;/删 d 分配的空间d=new doublelen;/给 d 分配长度为 len 的空间for(int index=0;indexlen;+index)/遍历数组 dd 从 0 到 lendindex=ddindex;/地 ddindex赋值给 dindexdeletedd;/删 dd 分配的空间break; /跳出循环)解析:解析 主要考查 SortedList 类,其中涉及动态数组、构造函数、析构函数、const 函数和排序算法。插入算法有两个步骤,一是比较,即要插入的元素在哪里;二是插入元素,后面的元素要逐个后移一位,为新加入的元素空出位置。主要考查考生对插入算法的掌握,题目要求 insert 函数的功能是将一个数据插入到一个有序表中,使得该数据表仍保持有序。可以知道数据表 d 是一组有序的数组,那么就采取先比较再插入的步骤完成即可。要注意动态数组 d 的长度是确定的,要添加元素,就要重新分配空间。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1