【计算机类职业资格】二级C++-56及答案解析.doc

上传人:sofeeling205 文档编号:1324110 上传时间:2019-10-17 格式:DOC 页数:4 大小:41.50KB
下载 相关 举报
【计算机类职业资格】二级C++-56及答案解析.doc_第1页
第1页 / 共4页
【计算机类职业资格】二级C++-56及答案解析.doc_第2页
第2页 / 共4页
【计算机类职业资格】二级C++-56及答案解析.doc_第3页
第3页 / 共4页
【计算机类职业资格】二级C++-56及答案解析.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

1、二级 C+-56及答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ERROR*found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The extension is:CPP 注意:只修改注释“/ERROR*found*”的下一行语句,不要改动程序中的其他内容。 / proj1.epp #include iostream using namespace std; class

2、MyClass char * p; public: MyClass(char c) p = new char; * p=c; / ERROR * found* MyClass (const MyClass copy) p =new char; * p = * (copy.p); / ERROR * found* MyClass() free p; MyClass *p = * (rhs.p); / ERROR * found* return this; char GetChar() const return * p; ; int main() MyClass obj1(C), obj2 (P)

3、 MyClass obj3(obj1); obj3 = obj2; cout “The extension is:“ obj1.GetChar () obj2.GetChar() obj3.GetChar () endl; return 0; (分数:30.00)_二、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 prod2,此工程中含有一个源程序文件proj2.cpp,其中定义了 Array类。 在 C+程序中访问数组元素时,如果索引值(下标)小于 0或者大于元素个数减 1,就会产生越界访问错误。Array 是一个带有检

4、查越界访问功能的数组类,其成员列表如下: 公有成员函数 功能 GetValue 获取指定元素的值 SetValue 将指定元素设置为指定值 GetLength 获取元素个数 私有成员函数 功能 IsOutOfRange 检查索引是否越界 私有数据成员 功能 _p 指向动态分配的整型数组的指针 _size 存放元素个数 Array 类的构造函数会动态分配一个 int类型数组,以存储给定数量的元素。在公有成员函数 GetValue和 SetValue中,首先调用私有成员函数 IsOutOfRange检查用于访问数组元素的索引是否越界,只有当索引值在有效范围内时,才能进行元素访问操作。 请在横线处填

5、写适当的代码,然后删除横线,以实现 Array类的功能。此程序的正确输出结果应为: 1,2,3,4,5,6,7,8,9,10 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。 / proj2.cpp #include iostream using namespace std; class Array public: Array(int size) /构造函数 /* found* 下列语句动态分配一个 int类型数组 _p=_; _size = size; Array() delete _p; /析构函数 void SetValue (int in

6、dex, int value) /设置指定元素的值 if (IsOutOfRange(index) ) cerr “Index out of range!“ endl; return; /* found* _; int GetValue(int index) const /获取指定元素的值 if (IsOutOfRange(index) cerr “Index out of range!“ endl; return -1; /* found* _; int GetLength () const return_size; /获妈元素个数 private: int *_p; int_size; b

7、ool IsOutOfRange(int index) const /检查索引是否越界 /* found* if (index 0 |_) return true; else return false; ; int main() Array a(10); for (int i = 0; i a.GetLength(); i+) a. SetValue(i, i+1); for (int j = 0; j a.GetLength() -1 j+) cout a. GetValue(j) “,“; cout a. GetValue (a. GetLength()-1) endl; return 0

8、 (分数:30.00)_三、B综合应用题/B(总题数:1,分数:40.00)3.请使用 VC6或使用答题菜单打开考生目录 proj3下的工程文件 proj3,此工程中包含一个源程序文件proj3.cpp,补充编制 C+程序 proj3.cpp,其功能是读取文本文件 in.dat中的全部内容,将文本存放到doc类的对象 myDoc中。然后分别统计 26个英文字母在文本中出现的次数,统计时不区分字母大小写。最后将统计结果输出到文件 out.dat中。文件 in.dat长度不大于 1000字节。 要求: 补充编制的内容写在“/*333*”与“/*666*”之间。实现分别统计 26个英文字母在文本中

9、出现的次数,并将统计结果在屏幕上输出。统计时不区分字母大小写,输出不限格式。不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat中,输出函数 writeToFile已经给出并且调用。 / proj3.cpp #include iostream #include fstream #include cstring using namespace std; class doc private: char * str; /文本字符串首地址 int counter26; /用于存入 26个字母的出现次数 int length; /文本字符个数 public: /构造函数,读取文件内容

10、用于初始化新对象。filename是文件名字符串首地址。 doc(char * filename); void count(); /统计 26个英文字母在文本中出现的次数,统计时不区分大小写。 doc(); void writeToFile(char * filename); ; doc:doc(char * filename) ifstreammyFile(filename); int len=1001, tmp; str=new charmen; length=0; while(tmp=myFile.get()!=EOF) strlength+ =tmp; strlength =/0;

11、myFile.close(); for(int i=0; i26; i+) counteri =0; /* 333* /* 666* doc:doc() delete str; void doc:writeToFile ( char * filename) ofstream outFile(filename); for(int i=0; i26; i +) outFile counteri endl; outFile.close(); void main () doc mymoc (“); myDoc.count(); myDoc.writeToFile(“); (分数:40.00)_二级 C

12、56答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ERROR*found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The extension is:CPP 注意:只修改注释“/ERROR*found*”的下一行语句,不要改动程序中的其他内容。 / proj1.epp #include iostream using namespace std; class MyCla

13、ss char * p; public: MyClass(char c) p = new char; * p=c; / ERROR * found* MyClass (const MyClass copy) p =new char; * p = * (copy.p); / ERROR * found* MyClass() free p; MyClass *p = * (rhs.p); / ERROR * found* return this; char GetChar() const return * p; ; int main() MyClass obj1(C), obj2 (P); MyC

14、lass obj3(obj1); obj3 = obj2; cout “The extension is:“ obj1.GetChar () obj2.GetChar() obj3.GetChar () endl; return 0; (分数:30.00)_正确答案:(1)MyClass(const MyClass class Array public: Array(int size) /构造函数 /* found* 下列语句动态分配一个 int类型数组 _p=_; _size = size; Array() delete _p; /析构函数 void SetValue (int index,

15、 int value) /设置指定元素的值 if (IsOutOfRange(index) ) cerr “Index out of range!“ endl; return; /* found* _; int GetValue(int index) const /获取指定元素的值 if (IsOutOfRange(index) cerr “Index out of range!“ endl; return -1; /* found* _; int GetLength () const return_size; /获妈元素个数 private: int *_p; int_size; bool

16、IsOutOfRange(int index) const /检查索引是否越界 /* found* if (index 0 |_) return true; else return false; ; int main() Array a(10); for (int i = 0; i a.GetLength(); i+) a. SetValue(i, i+1); for (int j = 0; j a.GetLength() -1 j+) cout a. GetValue(j) “,“; cout a. GetValue (a. GetLength()-1) endl; return 0; (分

17、数:30.00)_正确答案:(1)new intsize (2)_pindex=value (3)return_pindex (4)index_size)解析:考点 本题考查 Array类,其中涉及构造函数、析构函数、成员函数和 const函数。 解析 (1)主要考查考生对动态分配的掌握,题目要求分配一个 int类型数组,数组长度为 size,应使用 new语句分配空间,因此为 new intsize。 (2)主要考查考生对成员函数的掌握,先看函数功能:设置指定元素的值。index 为指定的下标,value 为指定的值,因此使用语句:_pindex=value;。 (3)主要考查考生对成员函数

18、的掌握,函数功能为获取指定元素的值,index 为要求返回的元素的下标,直接使用 return语句返回数组元素即可。 (4)主要考查考生对 if语句的掌握,函数功能是检查索引是否越界,当index0 或者 index_size 时,index 越界,返回 true。三、B综合应用题/B(总题数:1,分数:40.00)3.请使用 VC6或使用答题菜单打开考生目录 proj3下的工程文件 proj3,此工程中包含一个源程序文件proj3.cpp,补充编制 C+程序 proj3.cpp,其功能是读取文本文件 in.dat中的全部内容,将文本存放到doc类的对象 myDoc中。然后分别统计 26个英文

19、字母在文本中出现的次数,统计时不区分字母大小写。最后将统计结果输出到文件 out.dat中。文件 in.dat长度不大于 1000字节。 要求: 补充编制的内容写在“/*333*”与“/*666*”之间。实现分别统计 26个英文字母在文本中出现的次数,并将统计结果在屏幕上输出。统计时不区分字母大小写,输出不限格式。不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat中,输出函数 writeToFile已经给出并且调用。 / proj3.cpp #include iostream #include fstream #include cstring using namespac

20、e std; class doc private: char * str; /文本字符串首地址 int counter26; /用于存入 26个字母的出现次数 int length; /文本字符个数 public: /构造函数,读取文件内容,用于初始化新对象。filename是文件名字符串首地址。 doc(char * filename); void count(); /统计 26个英文字母在文本中出现的次数,统计时不区分大小写。 doc(); void writeToFile(char * filename); ; doc:doc(char * filename) ifstreammyFil

21、e(filename); int len=1001, tmp; str=new charmen; length=0; while(tmp=myFile.get()!=EOF) strlength+ =tmp; strlength =/0; myFile.close(); for(int i=0; i26; i+) counteri =0; /* 333* /* 666* doc:doc() delete str; void doc:writeToFile ( char * filename) ofstream outFile(filename); for(int i=0; i26; i +)

22、outFile counteri endl; outFile.close(); void main () doc mymoc (“); myDoc.count(); myDoc.writeToFile(“); (分数:40.00)_正确答案:(void doc:count() for(int i=0; ilength; i+) /从 0开始遍整个文本字符 if(stri=a /在数组 counter对加,做计数 if(stri=A /在数组counter对应的做计数 for(int index=0; index26; +index) /遍历数组数 counter,将下标转化为字母,然后输出其出

23、现次数 cout (char)(index+65)“or“ (char)(index+97) “出现的次数是:“counter indexendl; )解析:考点 本题考查 doc类,其中涉及动态数组、构造函数、成员函数和析构函数。 解析 主要考查考生对统计字母的掌握,首先要判断该字符是否为字母,即当字符的 ASCII码大于等于 a,小于等于 z时,为小写字母;当大于等于 A,小于等于 Z时,为大写字母,则用于计量该字母出现次数的元素值加1。如何确定计量字母次数的元素下标是本题的难点。当字母为小写时,用该字母的 ASCII码减去 a的ASCII码;当字母为大写时,用该字母的 ASCII码减去 A的 ASCII码,即可得到计量该字母的元素下标。

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

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

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