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

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

1、二级 C+真题 2 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.使用 VC6 打开 proj1 下的工程 proj1,其中有“沙发”类 Sofa 和主函数 main 的定义。程序中位于每个/ERROR*found*下的语句行有错误,请加以更正。更正后程序的输出应该是: 座位数:3 颜色:红色 注意:只能修改每个/ERROR*found*下的那一行,不要改动程序中的其他内容。 #includeiostream using namespace std; class Sofa /“沙发”类 int seats; /座位数 char colo

2、r10; /颜色 public: /ERROR*found* Sofa(int s,const char*co) /ERROR*found* if(co=NULL) color0=“/0“; else strcpy(color,co); /ERROR*found* const char*getSeats()constreturn seats; const char*getColor()constreturn color; ; int main() Sofa safa(3); cout“座位数:“sara.getSeats()endl; cout“颜色:“sara.getColor()endl;

3、 return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.使用 VC6 打开 proj2 下的工程 proj2,其中有元素类 Element 和队列类 Queue 的定义。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的输出结果应为: 3 8 5 0 5 0 7 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。 #includeiostream #includecmath #includecstdlib using namespace std; #define MaxLength 100 class Element /“

4、元素“类 public: int n; Element(int i=0):n(i) ; class Queue /“队列“类 Element*element; /指向存储元素的数组的指针 int tail; /队尾元素的下标 public: Queue():element(new Element100),tail(-1) Queue()deleteelement; void push(Element ele); /在队列尾端添加一个元素 Element pop(); /在队列首端删除一个元素,返回被删元素 Element front() const return element0;) /返回队

5、首元素,但不从队列中删除该元素 /*found* int size()constreturn(_);)/返回元素个数 void show()const; /显示集合中所有元素 ; void Queue:push (Element ele) if(tail=MaxLength-1) return; /空间满,不做任何处理 /*found* _; Element Queue:pop() if(size()=0)exit(1); /队列空,不做任何处理 Element tmp=element0; for(int i=0;itail;i+) elementi=elementi+1; /*found*

6、_; return tmp; void Queue:show()const /*found* for(_) coutelementi.n“; coutendl; int main() Queue q; q.push(3); q.push(8); q.push(5); q.push(0); q.show(); q.pop(); q.pop(); q.push(7); q.show(); return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.使用 VC6 打开 proj3 下的工程 proj3,其中声明了 MyString 类,它是一个用于表示字符串的类。成员

7、函数 reverse 将字符串反转,例如“abcde”反转后就成了“edcba”。请补充完整函数 reverse。在 main函数中给出了一个测试数据,此情况下程序的输出应该是: This is a string gnirts a si sihT 注意:只需在函数 reverse 的/*333*和/*666*之间填入若干语句,不要改动程序中的其他内容。 /MyString.h #includeiostream using namespace std; char*dup(const char*); class MyString char*str; public: MyString(const c

8、har*s=“ “):str(dup(s) MyString(const MyString) void reverse(); void show(ostream ; inline ostream return os; void writeToFile(const char*path); /MyString.cpp #include“MyString.h“ char*dup(const char*s) char*p=new charstrlen(s)+1; strcpy(p,s); return p; void MyString:reverse() /*333* /*666* void writ

9、eToFile(const char*path); /writetoFile.cpp #includefstream #include“MyString.h“ void writeToFile(const char*path) char full50; strcpy(full,path); strcat(full,“out.dat“); ofstream outfile(full); if(outfile.fail()cerr“打开输出文件失败!“;return;) MyString m1=“This is another string“; MyString m2(“字符串反转演示“); ou

10、tfilem1m2; m1.reverse(); m2.reverse(); outfilem1m2; outfile.close(); (分数:40.00)_二级 C+真题 2 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.使用 VC6 打开 proj1 下的工程 proj1,其中有“沙发”类 Sofa 和主函数 main 的定义。程序中位于每个/ERROR*found*下的语句行有错误,请加以更正。更正后程序的输出应该是: 座位数:3 颜色:红色 注意:只能修改每个/ERROR*found*下的那一行,不要改动程序中的其他内容。 #i

11、ncludeiostream using namespace std; class Sofa /“沙发”类 int seats; /座位数 char color10; /颜色 public: /ERROR*found* Sofa(int s,const char*co) /ERROR*found* if(co=NULL) color0=“/0“; else strcpy(color,co); /ERROR*found* const char*getSeats()constreturn seats; const char*getColor()constreturn color; ; int ma

12、in() Sofa safa(3); cout“座位数:“sara.getSeats()endl; cout“颜色:“sara.getColor()endl; return 0; (分数:30.00)_正确答案:()解析:(1)Sofa(int s,const char*co=“红色“):seats(s) (2)if(co=NULL) (3)int getSeats() const return seats; 考点 本题主要考查的知识点为:关系运算符和关系表达式、默认参数和函数返回类型。 解析 程序定义 Sofa 类,它包含两个数据成员:字符数组 color 和整型变量 seats;还包含一个

13、构造函数,两个公有成员函数,getSeats()函数返回成员 seats 的值,getColor()函数返回字符数组 color 的地址 Sofa 类的定义体中,有三个错误: (1)Sofa 类的构造函数错误:由 main()函数中定义 Sofa 类对象时可知:构造函数虽然有两个参数,但是可以只传入一个整型变量,所以另一个参数 co 需要提供一个默认值,由题意可知程序运行结果输出颜色是红色,所以 co 的默认字符串值为“红色”;另外程序需要输出座位数:3,所以成员 seats 需要使用形参 s 完成初始化,构造函数体中并未对 seats 进行赋值。 (2)sofa 类的构造函数中,需要根据形参

14、 co 的值是否为空,完成 color 的赋值,此处的 if 判断需要使用相等关系运算符,而不是赋值运算符。 (3)成员函数 getSeats()返回的是 seats 的值,所以返回值应该是 int 类型,而不是 const char*类型。二、简单应用题(总题数:1,分数:30.00)2.使用 VC6 打开 proj2 下的工程 proj2,其中有元素类 Element 和队列类 Queue 的定义。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的输出结果应为: 3 8 5 0 5 0 7 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。 #includ

15、eiostream #includecmath #includecstdlib using namespace std; #define MaxLength 100 class Element /“元素“类 public: int n; Element(int i=0):n(i) ; class Queue /“队列“类 Element*element; /指向存储元素的数组的指针 int tail; /队尾元素的下标 public: Queue():element(new Element100),tail(-1) Queue()deleteelement; void push(Element

16、 ele); /在队列尾端添加一个元素 Element pop(); /在队列首端删除一个元素,返回被删元素 Element front() const return element0;) /返回队首元素,但不从队列中删除该元素 /*found* int size()constreturn(_);)/返回元素个数 void show()const; /显示集合中所有元素 ; void Queue:push (Element ele) if(tail=MaxLength-1) return; /空间满,不做任何处理 /*found* _; Element Queue:pop() if(size(

17、)=0)exit(1); /队列空,不做任何处理 Element tmp=element0; for(int i=0;itail;i+) elementi=elementi+1; /*found* _; return tmp; void Queue:show()const /*found* for(_) coutelementi.n“; coutendl; int main() Queue q; q.push(3); q.push(8); q.push(5); q.push(0); q.show(); q.pop(); q.pop(); q.push(7); q.show(); return

18、0; (分数:30.00)_正确答案:()解析:(1)tail+1 (2)element+tail=ele (3)tail- (4)int i=0;i=tail;i+ 考点 本题考查的知识点为:类的数据成员和类的成员函数。 解析 题意定义了 Element 类,表示队列中的元素,它包含一个成员 n,表示元素的值;定义了 Queue 类,表示队列,它包含两个成员:element 是动态分配的一个数组,每个元素都是 Element 类型;tail 表示队列尾部的下标值,指向 element 数组的最后一个元素,当数组为空时,tail 的值为-1,所以初始化为-1;另外 Queue 类还定义了若干成

19、员函数:构造函数为 element 数组动态分配 100 个元素空间,初始化 tail 为-1;析构函数释放 element;push()函数向队列中添加一个新元素 ele,即将形参 ele 存放到数组尾部,并将 tail 自增 1;pop()函数将 element 数组的第一个元素(下标为 0)从数组中删除(采用移动后续所有元素的方式),并作为函数返回值返回;front()函数返回队列首元素;size()函数返回当前 element 数组的元素个数;show()函数将 element 数组中的元素的 n 值输出,由上面的分析,补充代码如下: (1)size()函数返回 element 数组的

20、元素个数,tail 的值是 element 数组的最后一个元素的下标,所以返回 tail+1 即可。 (2)push()函数将参数 ele 插入到队列的尾部,即将 ele 保存到 element,数组下标为(tail+1)的位置:element+tail=ele; (3)pop()函数将 element 数组的首元素保存到 tmp 变量中,然后遍历 element 数组,将下标从 1 开始的元素逐个赋给前一个元素,再把 tail 的值自减 1,tail-; (4)show()函数需要遍历 element 数组,然后将元素的 n 值输出,遍历下标的变量需要定义为 i,i 初值从0 开始,直到 i

21、 等于最后一个元素的下标,即 tail。int i=0;i=tail;i+三、综合应用题(总题数:1,分数:40.00)3.使用 VC6 打开 proj3 下的工程 proj3,其中声明了 MyString 类,它是一个用于表示字符串的类。成员函数 reverse 将字符串反转,例如“abcde”反转后就成了“edcba”。请补充完整函数 reverse。在 main函数中给出了一个测试数据,此情况下程序的输出应该是: This is a string gnirts a si sihT 注意:只需在函数 reverse 的/*333*和/*666*之间填入若干语句,不要改动程序中的其他内容。

22、/MyString.h #includeiostream using namespace std; char*dup(const char*); class MyString char*str; public: MyString(const char*s=“ “):str(dup(s) MyString(const MyString) void reverse(); void show(ostream ; inline ostream return os; void writeToFile(const char*path); /MyString.cpp #include“MyString.h“

23、 char*dup(const char*s) char*p=new charstrlen(s)+1; strcpy(p,s); return p; void MyString:reverse() /*333* /*666* void writeToFile(const char*path); /writetoFile.cpp #includefstream #include“MyString.h“ void writeToFile(const char*path) char full50; strcpy(full,path); strcat(full,“out.dat“); ofstream

24、 outfile(full); if(outfile.fail()cerr“打开输出文件失败!“;return;) MyString m1=“This is another string“; MyString m2(“字符串反转演示“); outfilem1m2; m1.reverse(); m2.reverse(); outfilem1m2; outfile.close(); (分数:40.00)_正确答案:()解析:void MyString:reverse() int i,j; char ch; int len=strlen(str); for(i=0,j=len-1;ij;i+,j-)

25、 ch=stri; stri=strj; strj=ch; 考点 本题考查类的数据成员、类的成员函数、重载流运算符和插入运算符和字符数组。 解析 程序定义了 MyString 类,它包含一个成员:str 字符指针;默认构造函数和复制构造函数通过 dup()函数,动态分配地址 p,将参数拷贝到 p 指向的内存空间,再使用 p 初始化 str;show()成员函数将 str 指向的字符串输出;reverse()成员函数将 str 指向的字符串反转;重载的运算符函数调用形参 m 对象的show()函数输出 str 指向的字符串。 main()函数首先构造一个 MyString 类对象 m1,构造过程

26、中传入字符串“This is a string”,所以 m1对象中的 str 指向一个动态分配的内存空间,该段内存存放拷贝的字符串“This is a string”,输出m1 后,再调用 m1.reverse()函数,将 str 指向的字符串进行反转,再输出 m1,题意要求我们完成reverse()成员函数的定义,完成反转字符串的功能。 由题意,我们仅有的信息就是 str 指向一个字符串,它是 char*类型,需要对 str 指向的字符串进行反转,需要获得字符串的长度,然后将首尾字符进行交换,直到交换到中间字符串为止,所以我们定义两个变量i、j,表示元素下标,分别从首(i=0)和尾(长度-1)向中间遍历该字符串,每次遍历,首先判断 i 是否小于 j,若 i 小于 j 则将 i 和 j 下标对应的字符进行交换,交换完成后 i 自增 1,j 自减 1,直到 i 不小于 j为止。

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

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

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