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

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

1、国家二级 C+机试(操作题)模拟试卷 241及答案与解析 一、基本操作题 1 使用 VC6打开考生文件夹下的源程序文件 modi.cpp,该程序运行时有错误,请改正其中的错误,使得程序正确运行。 程序输出: s1: n=20 s2: n=10 执行 s3 add(s1, s2) s3: n=30 s4: n=30 注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构,错误的语句在 *error*的下面。 #include iostream h Class TeStClass int n; public: TestClass() TestClasS(int m) n=m; *err

2、or* TestClass add(TestClass =s1, s2) this- n=s1 n+s2 n; *error* return(this); void disp() tout “nn“= n endl; *error* void main() TestClass s1(20), s2(10), s3, s4; cout “s1: “; s1 disp(); cout “s2: “; s2 disp(); s4=s3 add(s1, s2); cout “执行 s3 add(s1, s2) ns3: “; s3 disp(); cout “s4: “; s4 disp(); 二、简

3、单应用题 2 使用 VC6打开考生文件夹下的源程序文件 modi2 cpp。完成 fun()函数,其功能是:将两个从小到大有序数组 a和 b,复制合并出一个有序整数序列 c,其中形参n和 m分别是数组 a和 b的元素个数。 注意:不能修改程序的其他部分,只能修改 fun()函数。 #include iostream h void fun (int a, int n, int b, int m, int *c) void main() int A=3, 5, 7, 9, 11, 18, 21; int B=6, 15, 19, 21, 39; int C25, i; for(i=0; i 25;

4、 i+)Ci=0; cout “A=“; for(i=0; i 7; i+) cout Ai , ; cout endl; cout “B=“; for(i=0; i 5; i+) cout Bi , ; cout endl; fun(A, 7, B, 5, C); cout “C=“; for(i=0; i 12; i+) cout Ci , ; cout endl; returnj; 三、综合应用题 3 使用 VC6打开考生文件夹下的源程序文件 modi3 cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)对文件以追加的方式打开文件 。请在注释 *1*后添加适当

5、的语句。 (2)定义 m、 n为类 TestClass的公有 int型数据成员,请在注释*2*后添加适当的语句。 (3)定义 p为类 TestClass的数据成员指针,并指向类 TestClass数据成员 m,请在注释 *3*后添加适当的语句。 (4)定义 P指向类 TestClass数据成员 n,请在注释 *4*后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示 出来。请不要修改其他的程序代码。 #include iostream h #include fStream #include iomanip #include cmath using namespace std;

6、void WriteFile(int x) ofstream out1; *1* out1 open(“modi3 txt“, ); out1 X ; out1 close(); void ClearFile() ofstream out1; out1 open(“modi3 txt“); out1 close(); class TestClass public: void disp() cout “m=“ m endl; WriteFile(m); cout “n=“ n endl; WriteFile(n); *2* ; void main() *3* ClearFile(); TestC

7、lass a; a *p=30; *4* a *p=45; a disp(); 国家二级 C+机试(操作题)模拟试卷 241答案与解析 一、基本操作题 1 【正确答案】 (1)TestClass add(TestClass s1, TestClass s2) (2)return (*this); (3); 【试题解析】 (1)打开 modi1 cpp,调试程序,根据显示的错误提示,知道语句“TestClass add(TestClass s1, s2)”中的变量 “s2”没有声明类型,所以补充 “s2”的类型,即 “TestClass add(TestClass s1, TestClass s

8、2)”。 (2)“this- n=s1 n+s2 n: ”语句中的 this变量为指针变量,所以第 2个标识下的“return(this); ”返回语句中的 this变量,应该返回指针型,所以修改为“return(*this); ”。 (3)C+中类的定义格式应为 “class类名 ; ”,以分号为结尾,所以第 3个标识下,类结尾处补充分号 “; ”,即修改为 “; ”。 二、简单应用题 2 【正确答案】 int i, j; for(i=j=0; i n&j m; ) *c+=ai bj?ai+: bj+;比较两个数组中元素大小 while(i n)*c+=ai+; while(j m)*c+

9、=bj+; 【试题解析】 (1)首先都指向数组的第一个元素。 (2)然后利用 for循环对要合并的两个数组中的元素进行比较,直到两个数组结束。 (3)取比较结果小的元素,并将指向这个数组的位置向后移动一位。 三、综合应用题 3 【正确答案】 (1)将 “out1 open(“modi3 txt“, ); ”补充完整为:out1 open(“modi3 txt“, ios_base: app); (2)添加语句: int m, n; (3)添加语句: int TestClass: *p=&(TestClass: m); (4)添加语句: p=&(TestClass: n); 【试题解析】 (1)

10、题目 1要求 “对文件以追加的方式打开文件 ”。文件输出输入方式的设置值,以 ios: app方式是以输出方式打开文件,写入的数据添加在文件末尾,即第 1个标识下语句补全为 “out1 open(“modi3 txt“, ios_base:app); ”。 (2)题目 2要求 “定义 m、 n为类 TestClass的公有 int型数据成员 ”。只需在程序中的 TestClass类中的 public区域添加 m、 n的定义即可。即在第 2个标识下添加“int m, n; ”。 (3)题目 3要求 “定义 P为类 TestClass的数据成员指针,并指向类 TestClass数据成员 m”。程序中类 TestClass数据成员 m为: TestClass: m,定义 p为类TestClass的数据成员指针语句为: TestClass: *p,将其指向前面的数据成员m,即为 “int TestClass: *p=&(TestClass: m): ”。 (4)题目 4要求 “定义 P指向类 TestClass数据成员 n”。类 TestClass数据成员 n语句为 TestClass: n,用 p指向类 TestClass数据成员 n,添加的语句即“p=&(TestClass: n); ”。

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

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

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