[计算机类试卷]国家二级C++机试(操作题)模拟试卷265及答案与解析.doc

上传人:eventdump275 文档编号:497338 上传时间:2018-11-28 格式:DOC 页数:7 大小:32KB
下载 相关 举报
[计算机类试卷]国家二级C++机试(操作题)模拟试卷265及答案与解析.doc_第1页
第1页 / 共7页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷265及答案与解析.doc_第2页
第2页 / 共7页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷265及答案与解析.doc_第3页
第3页 / 共7页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷265及答案与解析.doc_第4页
第4页 / 共7页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷265及答案与解析.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

1、国家二级 C+机试(操作题)模拟试卷 265及答案与解析 一、基本操作题 1 使用 VC6打开考生文件夹下的源程序文件 modi1 cpp,但该程序运行时有错,请改正程序中的错误,使该程序的输出结果为: 20, 15 15, 20 注意:错误的语句在 *error*的下面,修改该语句即可。 #include iostream h *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=

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

3、e iostream h #define MAXLEN1024 void convert(char*des, char*str) void main() char sourMAXLEN; char destMAXLEN; cout “Pleaseinput a string: “ endl: cin getline(sour, MAXLEN); convert(dest, sour); cout dest endl; return; 三、综合应用题 3 使用 VC6打开考生文件夹下的源程序文件 modi3 cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能: (

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

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

6、ginary=imag; friend bool Equal(CComplex&c1, CComplex&c2); ; bool Equal(CComplex&c1, CComplex&c2) *4* int main() CComplex complex1(5); CCompiex complex2; cout Equal(complex1, complex2) endl; complex2 Set(5, 0); cout Equal(complex1, complex2) endl; return0; 国家二级 C+机试(操作题)模拟试卷 265答案与解析 一、基本操作题 1 【正确答案】

7、 (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()数均为实现交换的函数。参数传递方式有多种,将变量名作为实参和形参、传递变量的指针和传送引用方式,将变量名作为实参和形参方式,这种传递是单向的,在函数执行过程中形参的值发生变化不会传回实参,而指针和引用方式在函数执行过程中形参会 影响实

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

9、是变量名,故第 3个标识下应改为 “Exchange2(&a, &b); ”。 二、简单应用题 2 【正确答案】 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; des数组中已存在该元素,将标志位设置为假,说明不用再添加 break; des数组中已存在该元素,跳出循环 if(ToAdd) des数组中不存在添加进来 desj=stri; desj+1=0; 【试题解析】 (1)由审题分析可知,需要两重循环

10、外层循环不断读取 str中的元素,内循环在 des数组中查找读到的 str中的字符,如果找到了 des数组中已存在该元素不需要添加,如果没找到则添加到 des数组中。 (2)在外循环体内,先设置 bool型的标志变量 ToAdd为真,假设 stri不存在在des数组中,然后再内循环中,用 stri和 desj比较,如果相同说明已经存在了,将 ToAdd设置为假, desj后面的元素不需要再比较了,跳出内循环。 (3)通过判断 ToAdd可知, stri是否存在 des数组中,如果 三、综合应用题 3 【正确答案】 (1)添加语句: double real, imaginary; (2)添加语

11、句: real=r; imaginary=0; (3)添加语句: CCompiex temp: (4)添加语句: return(c1 real=c2 real)&(c1 imaginary=c2 imaginary); 【试题解析】 (1)第 1个标识下完成定义复数类 CComplex的私有成员变量 real和 imaginary,均为 double类型的变量,因此第 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(c1 real=c2 real)&(c1 imaginary=c2 imaginary); ”。

展开阅读全文
相关资源
猜你喜欢
  • ASD-STAN PREN 2436-06-2005 Aerospace series Paints and varnishes Corrosion resistant chromate-free two component cold curing epoxy primer Part 06 High corrosion resistance for mili.pdf ASD-STAN PREN 2436-06-2005 Aerospace series Paints and varnishes Corrosion resistant chromate-free two component cold curing epoxy primer Part 06 High corrosion resistance for mili.pdf
  • ASD-STAN PREN 2437-1998 Aerospace Series Chromate Conversion Coatings (Yellow) for Aluminium and Aluminium Alloys (Edition 1)《航空航天系列 铝和铝合金用铬酸盐转化涂层(黄色) 第1版》.pdf ASD-STAN PREN 2437-1998 Aerospace Series Chromate Conversion Coatings (Yellow) for Aluminium and Aluminium Alloys (Edition 1)《航空航天系列 铝和铝合金用铬酸盐转化涂层(黄色) 第1版》.pdf
  • ASD-STAN PREN 2438-1981 Steel FE-PL62 900 MPa Less Than or Equal to Rm Less Than or Equal to 1100 MPa Bars De Less Than or Equal to 40 mm Aerospace Series (Edition 1)《航空航天系列 钢FE-PL.pdf ASD-STAN PREN 2438-1981 Steel FE-PL62 900 MPa Less Than or Equal to Rm Less Than or Equal to 1100 MPa Bars De Less Than or Equal to 40 mm Aerospace Series (Edition 1)《航空航天系列 钢FE-PL.pdf
  • ASD-STAN PREN 2439-1981 Steel FE-PL62 900 MPa Less Than or Equal to Rm Less Than or Equal to 1100 MPa Forgings De Less Than or Equal to 40 mm Aerospace Series (Edition 1)《航空航天系列 钢F.pdf ASD-STAN PREN 2439-1981 Steel FE-PL62 900 MPa Less Than or Equal to Rm Less Than or Equal to 1100 MPa Forgings De Less Than or Equal to 40 mm Aerospace Series (Edition 1)《航空航天系列 钢F.pdf
  • ASD-STAN PREN 2442-1981 Steel FE-PL711 1100 MPa Less Than or Equal to Rm Less Than or Equal to 1300 MPa Bars and Wires De Less Than or Equal to 25 mm Aerospace Series (Edition 1)《航.pdf ASD-STAN PREN 2442-1981 Steel FE-PL711 1100 MPa Less Than or Equal to Rm Less Than or Equal to 1300 MPa Bars and Wires De Less Than or Equal to 25 mm Aerospace Series (Edition 1)《航.pdf
  • ASD-STAN PREN 2443-1981 Steel FE-PL711 1100 MPa Less Than or Equal to Rm Less Than or Equal to 1300 MPa Forgings De Less Than or Equal to 25 mm Aerospace Series (Edition 1)《航空航天系列 .pdf ASD-STAN PREN 2443-1981 Steel FE-PL711 1100 MPa Less Than or Equal to Rm Less Than or Equal to 1300 MPa Forgings De Less Than or Equal to 25 mm Aerospace Series (Edition 1)《航空航天系列 .pdf
  • ASD-STAN PREN 2444-1981 Steel FE-PL711 900 MPa Less Than or Equal to Rm Less Than or Equal to 1100 MPa Bars and Wires De Less Than or Equal to 45 mm Aerospace Series (Edition 1)《航空.pdf ASD-STAN PREN 2444-1981 Steel FE-PL711 900 MPa Less Than or Equal to Rm Less Than or Equal to 1100 MPa Bars and Wires De Less Than or Equal to 45 mm Aerospace Series (Edition 1)《航空.pdf
  • ASD-STAN PREN 2446-1981 Steel FE-PL45 1100 MPa Less Than or Equal to Rm Less Than or Equal to 1300 MPa Bars De Less Than or Equal to 25 mm Aerospace Series (Edition 1)《航空航天系列 钢FE-P.pdf ASD-STAN PREN 2446-1981 Steel FE-PL45 1100 MPa Less Than or Equal to Rm Less Than or Equal to 1300 MPa Bars De Less Than or Equal to 25 mm Aerospace Series (Edition 1)《航空航天系列 钢FE-P.pdf
  • ASD-STAN PREN 2447-1981 Steel FE-PL45 1100 MPa Less Than or Equal to Rm Less Than or Equal to 1300 MPa Forgings De Less Than or Equal to 25 mm Aerospace Series (Edition 1)《航空航天系列 钢.pdf ASD-STAN PREN 2447-1981 Steel FE-PL45 1100 MPa Less Than or Equal to Rm Less Than or Equal to 1300 MPa Forgings De Less Than or Equal to 25 mm Aerospace Series (Edition 1)《航空航天系列 钢.pdf
  • 相关搜索

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

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