【计算机类职业资格】国家二级C++机试(操作题)-试卷25及答案解析.doc

上传人:花仙子 文档编号:1331749 上传时间:2019-10-17 格式:DOC 页数:3 大小:35KB
下载 相关 举报
【计算机类职业资格】国家二级C++机试(操作题)-试卷25及答案解析.doc_第1页
第1页 / 共3页
【计算机类职业资格】国家二级C++机试(操作题)-试卷25及答案解析.doc_第2页
第2页 / 共3页
【计算机类职业资格】国家二级C++机试(操作题)-试卷25及答案解析.doc_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

1、国家二级 C+机试(操作题)-试卷 25 及答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6 打开考生文件夹下的源程序文件 modi1cpp,但该程序运行时有错,请改正程序中的错误,使该程序的输出结果为:20,1515,20 注意:错误的语句在*error*的下面,修改该语句即可。#includeiostreamh*error*void Exchangel(int m,int n)int t=m;m=n;n=t;*error*void Exchange2(int m,int n)int t=*m;*m=*n;*n=t;void mai

2、n()int b=20;int a=15;Exchangel(a,b);couta,bendl;*error*Exchange2(a,b);couta,bendl;(分数:2.00)_二、简单应用题(总题数:1,分数:2.00)2.使用 VC6 打开考生文件夹下的源程序文件 modi2cpp。阅读下列函数说明和代码,补充空出的代码。函数 convert(char*des,char*str)的功能是去掉字符串 str 中相同的字母,并将处理后的结果存到 des所指的字符串中。例如:输入:This is great!输出:This grea!注:卷:不能修改程序的其他部分,只能补充 fun()函数

3、。#includeiostreamh#define MAXLEN1024void convert(char*des,char*str)void main()char sourMAXLEN;char destMAXLEN;cout“Pleaseinput a string:“endl:cingetline(sour,MAXLEN);convert(dest,sour);coutdestendl;return;(分数:2.00)_三、综合应用题(总题数:1,分数:2.00)3.使用 VC6 打开考生文件夹下的源程序文件 modi3cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整

4、。完成以下功能:(1)定义复数类 CComplex 的私有成员变量 real 和 imaginary,分别用来表示复数的实部和虚部,都是 double 类型的变量。请在注释*1*后添加适当的语句。(2)添加复数类 CComplex 的带一个参数的构造函数,分别将 real 和 imaginary 赋值为参数 r 和0。请在注释*2*后添加适当的语句。(3)完成对运算符“+”的重载,分别对复数的实部和虚部相加。请在注释*3*后添加适当的语句。(4)完成复数的友元函数Equal(CComplex&c1,CComplex&c2)的定义,如果两个数的实部和虚部都相等,则返回 1,否则返回 0,请在注释

5、*4*后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#includeiostreamhclass CComplexprivate:*1*publiC:CComplex()real=imaginary=0;CComplex(double r)*2*CComplex operator+(CComplex&c1)*3*tempreal=real+c1real;tempimaginary=imaginary+c1imaginary;return temp;void Set(int re,int imag)real=re;imaginary=imag;friend boo

6、l Equal(CComplex&c1,CComplex&c2);bool Equal(CComplex&c1,CComplex&c2)*4*int main()CComplex complex1(5);CCompiex complex2;coutEqual(complex1,complex2)endl;complex2Set(5,0);coutEqual(complex1,complex2)endl;return0;(分数:2.00)_国家二级 C+机试(操作题)-试卷 25 答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6 打开考

7、生文件夹下的源程序文件 modi1cpp,但该程序运行时有错,请改正程序中的错误,使该程序的输出结果为:20,1515,20 注意:错误的语句在*error*的下面,修改该语句即可。#includeiostreamh*error*void Exchangel(int m,int n)int t=m;m=n;n=t;*error*void Exchange2(int m,int n)int t=*m;*m=*n;*n=t;void main()int b=20;int a=15;Exchangel(a,b);couta,bendl;*error*Exchange2(a,b);couta,bend

8、l;(分数:2.00)_正确答案:(正确答案:(1)void Exchangel(int& m,int& n) (2)void Exchange2(int*m,int*n) (3)Exchange2(&a,&b);)解析:解析:(1)由程序的运行结果可知,分别调用 Exchange1()和 Exchange20 后,a 和 b 的值两次发生交换,因此 Exchange1()函数和 Exchange2()数均为实现交换的函数。参数传递方式有多种,将变量名作为实参和形参、传递变量的指针和传送引用方式,将变量名作为实参和形参方式,这种传递是单向的,在函数执行过程中形参的值发生变化不会传回实参,而指针

9、和引用方式在函数执行过程中形参会影响实参。显然这里不是采用变量名作为实参和形参的单向传递方式,而是采用的传地址或传引用的方式,在Exchange1()体内没有出现指针运算符“*”,因此可知 Exchange1()函数是采用的传引用的方式,因此第1 个标识下应改为“void Exchange1(int&m,int&n)”。 (2)由 1 分析可知,Exchange2()函数内有指针运算符“*”,可知其是采用的传指针的方式作为参数传递,因此第 2 个标识下应改为“void Exchange2(int*m,int*n)”。 (3)Exchange2()参数为 int 型数据指针,因此调用 Excha

10、nge2()时应传入指针而不是变量名,故第 3 个标识下应改为“Exchange2(&a,&b);”。二、简单应用题(总题数:1,分数:2.00)2.使用 VC6 打开考生文件夹下的源程序文件 modi2cpp。阅读下列函数说明和代码,补充空出的代码。函数 convert(char*des,char*str)的功能是去掉字符串 str 中相同的字母,并将处理后的结果存到 des所指的字符串中。例如:输入:This is great!输出:This grea!注:卷:不能修改程序的其他部分,只能补充 fun()函数。#includeiostreamh#define MAXLEN1024void

11、convert(char*des,char*str)void main()char sourMAXLEN;char destMAXLEN;cout“Pleaseinput a string:“endl:cingetline(sour,MAXLEN);convert(dest,sour);coutdestendl;return;(分数:2.00)_正确答案:(正确答案:int i,j; bool ToAdd; des0=0; for(i=0;stri!=NULL;i+) ToAdd=true; for(j=0;desj!=NULL;j+) if(stri=desj) ToAdd=false;de

12、s 数组中已存在该元素,将标志位设置为假,说明不用再添加 break;des 数组中已存在该元素,跳出循环 if(ToAdd)des 数组中不存在添加进来 desj=stri; desj+1=0; )解析:解析:(1)由审题分析可知,需要两重循环,外层循环不断读取 str 中的元素,内循环在 des 数组中查找读到的 str 中的字符,如果找到了 des 数组中已存在该元素不需要添加,如果没找到则添加到 des数组中。 (2)在外循环体内,先设置 bool 型的标志变量 ToAdd 为真,假设 stri不存在在 des 数组中,然后再内循环中,用 stri和 desj比较,如果相同说明已经存在

13、了,将 ToAdd 设置为假,desj后面的元素不需要再比较了,跳出内循环。 (3)通过判断 ToAdd 可知,stri是否存在 des 数组中,如果三、综合应用题(总题数:1,分数:2.00)3.使用 VC6 打开考生文件夹下的源程序文件 modi3cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)定义复数类 CComplex 的私有成员变量 real 和 imaginary,分别用来表示复数的实部和虚部,都是 double 类型的变量。请在注释*1*后添加适当的语句。(2)添加复数类 CComplex 的带一个参数的构造函数,分别将 real 和 i

14、maginary 赋值为参数 r 和0。请在注释*2*后添加适当的语句。(3)完成对运算符“+”的重载,分别对复数的实部和虚部相加。请在注释*3*后添加适当的语句。(4)完成复数的友元函数Equal(CComplex&c1,CComplex&c2)的定义,如果两个数的实部和虚部都相等,则返回 1,否则返回 0,请在注释*4*后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#includeiostreamhclass CComplexprivate:*1*publiC:CComplex()real=imaginary=0;CComplex(double r)*2*C

15、Complex operator+(CComplex&c1)*3*tempreal=real+c1real;tempimaginary=imaginary+c1imaginary;return temp;void Set(int re,int imag)real=re;imaginary=imag;friend bool Equal(CComplex&c1,CComplex&c2);bool Equal(CComplex&c1,CComplex&c2)*4*int main()CComplex complex1(5);CCompiex complex2;coutEqual(complex1,c

16、omplex2)endl;complex2Set(5,0);coutEqual(complex1,complex2)endl;return0;(分数:2.00)_正确答案:(正确答案:(1)添加语句:double real,imaginary; (2)添加语句:real=r;imaginary=0; (3)添加语句:CCompiex temp: (4)添加语句:return(c1real=c2real)&(c1imaginary=c2imaginary);)解析:解析:(1)第 1 个标识下完成定义复数类 CComplex 的私有成员变量 real 和 imaginary,均为double 类

17、型的变量,因此第 1 个标识下应添加“double real,imaginary;”。 (2)构造函数CComplex(double r)完成类的初始化,该构造函数有一个参数,分别将 real 和 imaginary 赋值为参数 r和 0,因此第 2 个标识下应添加“real=r;imaginary=0;”。 (3)编译程序可知,变量 temp 没有定义,由 operator+(CComplex& c1)函数体的语句可知,temp 为函数的返回值,而 operator+(CComplex& c1)函数体函数返回 CComplex 类对象,故在第 3 个标识下补充 temp 定义,即“CComplex temp;”。 (4)即判断两个对象 c1 和 c2 的实部和虚部是否均相等,两个逻辑结果相与即可,因此第 4 个标识下应添加“return(c1real=c2real)&(c1imaginary=c2imaginary);”。

展开阅读全文
相关资源
猜你喜欢
  • GOST ISO 22119-2013 Microbiology of food and animal feeding stuffs Real-time polymerase chain reaction (PCR) for the detection of food-borne pathogens General requirements and defi.pdf GOST ISO 22119-2013 Microbiology of food and animal feeding stuffs Real-time polymerase chain reaction (PCR) for the detection of food-borne pathogens General requirements and defi.pdf
  • GOST ISO 2230-2013 Rubber products Guidelines for storage《橡胶制品 存储指南》.pdf GOST ISO 2230-2013 Rubber products Guidelines for storage《橡胶制品 存储指南》.pdf
  • GOST ISO 2244-2013 Packaging Complete filled transport packages and unit loads Horizontal impact test methods《包装 满运输包装和单位负载 水平冲击试验方法》.pdf GOST ISO 2244-2013 Packaging Complete filled transport packages and unit loads Horizontal impact test methods《包装 满运输包装和单位负载 水平冲击试验方法》.pdf
  • GOST ISO 22448-2013 Earth-moving machinery Anti-theft systems Classification and performance《土方机械 防盗系统 分类和性能》.pdf GOST ISO 22448-2013 Earth-moving machinery Anti-theft systems Classification and performance《土方机械 防盗系统 分类和性能》.pdf
  • GOST ISO 22867-2014 Mechanical vibration Evaluation of vibration emission of hand-held power tools Forestry machines with internal combustion engine《机械振动 手持式电动工具的振动辐射评估 装有内燃机的林业机械》.pdf GOST ISO 22867-2014 Mechanical vibration Evaluation of vibration emission of hand-held power tools Forestry machines with internal combustion engine《机械振动 手持式电动工具的振动辐射评估 装有内燃机的林业机械》.pdf
  • GOST ISO 22972-2014 Essential oils Analysis by gas chromatography on chiral capillary columns General method《精油 毛细管气相色谱分析通用方法》.pdf GOST ISO 22972-2014 Essential oils Analysis by gas chromatography on chiral capillary columns General method《精油 毛细管气相色谱分析通用方法》.pdf
  • GOST ISO 2303-2013 Isoprene rubber (IR) solution-polymerized types non-oil-extended Evaluation methods《异戊二烯橡胶 (IR) 非充油溶液聚合型 评价方法》.pdf GOST ISO 2303-2013 Isoprene rubber (IR) solution-polymerized types non-oil-extended Evaluation methods《异戊二烯橡胶 (IR) 非充油溶液聚合型 评价方法》.pdf
  • GOST ISO 2322-2013 Styrene-butadiene rubber (SBR) emulsion- and solution- polymerized types Evaluation methods《乳液和溶液聚合型苯乙烯-丁二烯橡胶 (SBR) 评价方法》.pdf GOST ISO 2322-2013 Styrene-butadiene rubber (SBR) emulsion- and solution- polymerized types Evaluation methods《乳液和溶液聚合型苯乙烯-丁二烯橡胶 (SBR) 评价方法》.pdf
  • GOST ISO 2332-2013 Tractors and agricultural machinery Connection of implements via three-point linkage Clearance zone around implement《拖拉机和农业机械 三点悬挂机具的联接装置 机具上的间隙范围》.pdf GOST ISO 2332-2013 Tractors and agricultural machinery Connection of implements via three-point linkage Clearance zone around implement《拖拉机和农业机械 三点悬挂机具的联接装置 机具上的间隙范围》.pdf
  • 相关搜索

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

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