【计算机类职业资格】二级C++分类模拟154及答案解析.doc

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

1、二级 C+分类模拟 154 及答案解析(总分:100.00,做题时间:90 分钟)一、程序改错题(总题数:1,分数:30.00)1.使用 VC6 打开 下的源程序文件 modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: 1 0 注意:错误的语句在/*error*的下面,修改该语句即可。 #includeiostream.h struct Struct union int a; char c4; ; int b; /*error* void main() Struct m; /*error* m.c0=0; m.c1=0; m.c2=0; /*error* m.c3

2、1; m.b=m.c3; cout m, a endl m, b endl; (分数:30.00)_二、程序填空题(总题数:1,分数:40.00)2.使用 VC6 打开 下的源程序文件 modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char* des,char* str,char c,char* str2)的功能是: 如果 str 中包含字符“!”,则替换成“a“; 如果 str 中包含字符“ char* str =“! char* str2=“jklm“; convert(dest,str,str2); cout dest endl; return; (分

3、数:40.00)_三、程序设计题(总题数:1,分数:30.00)3.使用 VC6 打开 下的源程序文件 modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能: (1)定义私有常量 PI,请在注释/*1*后添加适当的语句。 (2)完成类的私有常量 PI 的赋值,以及完成对半径 radius 的赋值。请在注释/*2*后添加适当的语句。 (3)完成计算圆面积的函数 GetArea()。请在注释/*3*后添加适当的语句。 (4)完成计算圆周长的函数 GetGirth()。请在注释/*4*后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他

4、内容。 #includeiostream.h #define CONST_PI 3.141592 class CCircle private: double radius; /*1* public: /*2* CCircle(int radius) this-radius=radius; double GetArea() /*3* double GetGirth() /*4* void SetRadius(int m) radius=m; ; int main() CCircle circle(5); cout circle.GetArea() endl; cout circle.GetGir

5、th() endl; circle. SetRadius(10); cout circle. GetArea() endl; cout circle.GetGirth() endl; return 0; (分数:30.00)_二级 C+分类模拟 154 答案解析(总分:100.00,做题时间:90 分钟)一、程序改错题(总题数:1,分数:30.00)1.使用 VC6 打开 下的源程序文件 modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: 1 0 注意:错误的语句在/*error*的下面,修改该语句即可。 #includeiostream.h struct Str

6、uct union int a; char c4; ; int b; /*error* void main() Struct m; /*error* m.c0=0; m.c1=0; m.c2=0; /*error* m.c3=1; m.b=m.c3; cout m, a endl m, b endl; (分数:30.00)_正确答案:()解析:(1); (2)m.c0=1; (3)m.c3=0; 答案考生文件夹 解析 程序当中定义了一个结构体 Struct,Struct 内含有 2 个成员变量:共用体和整型变量 b,共用体由整型变量 a 和字符数组 c4组成,int 型变量 32 位,而 c4

7、刚好也是 32 位,因此 a和字符数组 c4存放在同一内存单元中。 (1)第 1 表示在结构体定义内,结构体定义的一般形式为: struct结构体名 成员列表 变量名列表; 这里定义的结构体并没有变量名列表,但是结构体定义一定是以“;”结束的,因此第 1 个标识下应改为“;”。 (2)根据题目要求程序输出结果分别为“1”和“0”,即结构体变量 m 的成员变量 a 的值为 1,b 的值为0,又 a 和字符数组 c4存放在同一段内存单元中,因此 a 的值可由数组 c4决定(两者使用同一地址单元),故 c0表示 a 的低 8 位,c1为 a 的第二个 8 位,c2为 a 的第三个 8 位,c3为 a

8、 的最高 8 位,a=0,所以 c0=1,c1=c2=c3=0,故第 2 个标识下应改为“m.c0=1;”。 (3)由(2)分析可知第 3 个标识下应改为“m.c3=0;”。二、程序填空题(总题数:1,分数:40.00)2.使用 VC6 打开 下的源程序文件 modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char* des,char* str,char c,char* str2)的功能是: 如果 str 中包含字符“!”,则替换成“a“; 如果 str 中包含字符“ char* str =“! char* str2=“jklm“; convert(dest,s

9、tr,str2); cout dest endl; return; (分数:40.00)_正确答案:()解析:des0=0; char temp2=0,0; for(int i=0; stri!=NULL;i+) if(stri=“!“) temp0=“a“; /替换成“a strcat(des,temp); else if(stri=“ strcat(des,temp); /替换成“b“ else if(stri=“*“) Strcat(deS,str2); /替换成 str2 else temp0=stri; strcat(des,temp); /其他情况则添加在后面 答案考生文件夹 解析

10、 函数 convert(char* des,char* str,char c,char* str2)的功能是将 str 中包含的特定字符替换成其他字符,因此可利用循环搜索 str 字符串,找到特定字符后进行替换。 (1)由审题分析可知,利用循环中不断检索 str 字符串每一个字符,循环变量 i 从 0 开始,直到到了 str字符结束,即 srti=NULL 为循环判断结束条件。 (2)在循环体内,用 if else 语句判断是不是“!“ /*1* public: /*2* CCircle(int radius) this-radius=radius; double GetArea() /*3*

11、 double GetGirth() /*4* void SetRadius(int m) radius=m; ; int main() CCircle circle(5); cout circle.GetArea() endl; cout circle.GetGirth() endl; circle. SetRadius(10); cout circle. GetArea() endl; cout circle.GetGirth() endl; return 0; (分数:30.00)_正确答案:()解析:(1)添加语句:const double PI; (2)将“CCircle(int r

12、adius)”补充完整为:CCircle(int radius):PI(CONST_PI) (3)添加语句:return radius9*radius*PI; (4)添加语句:return 2*radius*PI; 答案考生文件夹 解析 程序当中定义了 CCircle 类,含有成员变量 radius 和常成员 PI,成员函数GetArea 返回圆面积,成员函数 GetGirth 返回圆周长,SetRadius(intm)函数改变 radius 大小。 (1)第 1 个标识下定义私有常量 PI,常数据成员是使用 const 说明的数据成员,因此第 1 个标识下应添加“const double PI;”。 (2)常数据成员的初始化只能通过构造函数的成员初始化列表进行,构造函数 CCircle(int radius)应对常数据成员 PI 进行初始化,第 2 个标识下应改为“CCircle(int radius): PI(CONST_PI)”。 (3)第 3 个标识下返回圆面积值,圆面积为半径平方与 PI 的乘积值,即第 3 标识下应添加“retuurn radius*radius*PI;”。 (4)第 4 个标识下返回周长,即第 4 表示下应添加“return 2*radius*PI;”。

展开阅读全文
相关资源
猜你喜欢
  • DIN EN 15009-2006 Aerosol containers - Compartmented aerosol containers English version of DIN EN 15009 2006-12《空气溶胶容器 双层空气溶胶容器》.pdf DIN EN 15009-2006 Aerosol containers - Compartmented aerosol containers English version of DIN EN 15009 2006-12《空气溶胶容器 双层空气溶胶容器》.pdf
  • DIN EN 1501-1-2016 Refuse collection vehicles - General requirements and safety requirements - Part 1 Rear loaded refuse collection vehicles German version EN 1501-1 2011+A1 2015《垃.pdf DIN EN 1501-1-2016 Refuse collection vehicles - General requirements and safety requirements - Part 1 Rear loaded refuse collection vehicles German version EN 1501-1 2011+A1 2015《垃.pdf
  • DIN EN 1501-2-2010 Refuse collection vehicles and their associated lifting devices - General requirements and safety requirements - Part 2 Side loaded refuse collection vehicles Ge.pdf DIN EN 1501-2-2010 Refuse collection vehicles and their associated lifting devices - General requirements and safety requirements - Part 2 Side loaded refuse collection vehicles Ge.pdf
  • DIN EN 1501-3-2010 Refuse collection vehicles and their associated lifting devices - General requirements and safety requirements - Part 3 Front loaded refuse collection vehicles G.pdf DIN EN 1501-3-2010 Refuse collection vehicles and their associated lifting devices - General requirements and safety requirements - Part 3 Front loaded refuse collection vehicles G.pdf
  • DIN EN 1501-4-2008 en 4355 Refuse collection vehicles and their associated lifting devices - General requirements and safety requirements - Part 4 Noise test code for refuse collec.pdf DIN EN 1501-4-2008 en 4355 Refuse collection vehicles and their associated lifting devices - General requirements and safety requirements - Part 4 Noise test code for refuse collec.pdf
  • DIN EN 1501-5-2012 Refuse collection vehicles - General requirements and safety requirements - Part 5 Lifting devices for refuse collection vehicles German version EN 1501-5 2011《垃.pdf DIN EN 1501-5-2012 Refuse collection vehicles - General requirements and safety requirements - Part 5 Lifting devices for refuse collection vehicles German version EN 1501-5 2011《垃.pdf
  • DIN EN 15010-2006 Aerosol containers - Aluminium containers - Tolerances of the fundamental dimensions in connection with the clinch English version of DIN EN 15010 2006-12《空气溶胶容器 .pdf DIN EN 15010-2006 Aerosol containers - Aluminium containers - Tolerances of the fundamental dimensions in connection with the clinch English version of DIN EN 15010 2006-12《空气溶胶容器 .pdf
  • DIN EN 15011-2014 Cranes - Bridge and gantry cranes German version EN 15011 2011+A1 2014《起重机 桥式起重机和龙门起重机 德文版本EN 15011-201+A-2014》.pdf DIN EN 15011-2014 Cranes - Bridge and gantry cranes German version EN 15011 2011+A1 2014《起重机 桥式起重机和龙门起重机 德文版本EN 15011-201+A-2014》.pdf
  • DIN EN 15012-2008 Plastics piping systems - Soil and waste discharge systems within the building structure - Performance characteristics for pipes fittings and their joints English.pdf DIN EN 15012-2008 Plastics piping systems - Soil and waste discharge systems within the building structure - Performance characteristics for pipes fittings and their joints English.pdf
  • 相关搜索

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

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