1、国家二级 C+机试(操作题)模拟试卷 401及答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6打开考生文件夹下的源程序文件 modilcpp,该程序运行时有错误,请改正程序中的错误。 本题的功能是:从键盘输入字符串 s,然后输出字符串 s中的字符个数。 注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构,错误的语句在*error*的下面。#includeiostreamint main()*error* cout“please input a string:”end1; *error* namespace std; c
2、har s2 5 6; cingetline(S,256); coutStrlen(S)end1; return 0,(分数:2.00)_二、简单应用题(总题数:1,分数:2.00)2.使用 VC6打开考生文件夹下的源程序文件 modi2cpp。请完成函数 fun(char*data),此函数的功能是,找出字符串数组中最小的 ASCII值,如果有相同变量,则输出最后一个所在的位置;如果字符串为空,则返回1;或者不存在时也返回1。 注意:请勿改动主函数 main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。计算数字如果第一个字母最小,则返回 0。依次增加。 #includ
3、e int fun(char*data) void main() char str1024; cout“请输入一行英文字符串:n“; cingetline(str,1024); cout”最小的字母出现在距离头部”fun(str)“个字母处”_三、综合应用题(总题数:1,分数:2.00)3.使用 VC6打开考生文件夹下的源程序文件 modi3cpp。请完成以下部分,实现在屏幕上输出为: TestClass3 TestClass2 这个程序需要修改的部分,请按照以下部分实现。(1)类 TestClass0不能被实例化,请定义一个纯虚函数 print,在注释*1*后添加适当的语句。(2)类 Tes
4、tClassl私有虚继承类 TestClass0,请在注释*2*后添加适当的语句。(3)类 TestClass2公有继承类 TestClass0,请在注释*3*后添加适当的语句。(4)类 TestClass3公有继承类TestClass2与 TcstClass1,请在注释*4*后添加适当的语句。 注意:仅在函数指定位置添加语句,请勿改动主函数 main与其他函数中的任何内容。includeiostreamhclass TestClass0 *1* ; *2* class TestClass1: public: void print() cout”TestClass1”end1; ;*3*cla
5、ss TestClass2:public: void print() cout“TestClass2“end1; ;*4*Class TestClass3:public: void print() cout”TestClass3”end1; ;void main() TestClass3 c3; TestClass2 c2; c3print(); c2print(); return;(分数:2.00)_国家二级 C+机试(操作题)模拟试卷 401答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6打开考生文件夹下的源程序文件 modilc
6、pp,该程序运行时有错误,请改正程序中的错误。 本题的功能是:从键盘输入字符串 s,然后输出字符串 s中的字符个数。 注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构,错误的语句在*error*的下面。#includeiostreamint main()*error* cout“please input a string:”end1; *error* namespace std; char s2 5 6; cingetline(S,256); coutStrlen(S)end1; return 0,(分数:2.00)_正确答案:(正确答案:(1)std:cout“please
7、 input a string:“解析:解析:(1)第 1个标识下实现在屏幕上输出“please input a string:”提示语句。在程序中用到C+标准库时,要使用 std标准命名空间进行限定。cout 为标准库函数,所以要声明 tout是在命名空间std中定义的流对象,即第 1个标识下的“std:cout“please input a string:”二、简单应用题(总题数:1,分数:2.00)2.使用 VC6打开考生文件夹下的源程序文件 modi2cpp。请完成函数 fun(char*data),此函数的功能是,找出字符串数组中最小的 ASCII值,如果有相同变量,则输出最后一个所
8、在的位置;如果字符串为空,则返回1;或者不存在时也返回1。 注意:请勿改动主函数 main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。计算数字如果第一个字母最小,则返回 0。依次增加。 #include int fun(char*data) void main() char str1024; cout“请输入一行英文字符串:n“; cingetline(str,1024); cout”最小的字母出现在距离头部”fun(str)“个字母处”_正确答案:(正确答案:int MinPos=0; 初始化最小值位置 if(data=NULL) 判断输入字符串是否为空 retur
9、n-1; char MinData=data0;设置字符串第一个字符为最小值 if(MinData=0) 判断第一个字符是否存在 return-1; for(int i=i;datai!=0;i+) if(datai=MinData) MinData=datai; 逐个判断每个字母是否小于标记字符 MinPos=i; 最小值位置 return MinPos;)解析:解析:(1)MinPos 变量首先指向字符串 data的第一个字符,同时将标志位 MinPos初始值赋为 0。 (2)然后利用 for循环对每一个当前字符与 MinPos标记的字符进行比较。 (3)循环中将求得的最小数据datai赋
10、值给 MinData,并将该数据项数组标志位赋给标志位 MinPos。三、综合应用题(总题数:1,分数:2.00)3.使用 VC6打开考生文件夹下的源程序文件 modi3cpp。请完成以下部分,实现在屏幕上输出为: TestClass3 TestClass2 这个程序需要修改的部分,请按照以下部分实现。(1)类 TestClass0不能被实例化,请定义一个纯虚函数 print,在注释*1*后添加适当的语句。(2)类 TestClassl私有虚继承类 TestClass0,请在注释*2*后添加适当的语句。(3)类 TestClass2公有继承类 TestClass0,请在注释*3*后添加适当的语
11、句。(4)类 TestClass3公有继承类TestClass2与 TcstClass1,请在注释*4*后添加适当的语句。 注意:仅在函数指定位置添加语句,请勿改动主函数 main与其他函数中的任何内容。includeiostreamhclass TestClass0 *1* ; *2* class TestClass1: public: void print() cout”TestClass1”end1; ;*3*class TestClass2:public: void print() cout“TestClass2“end1; ;*4*Class TestClass3:public: v
12、oid print() cout”TestClass3”end1; ;void main() TestClass3 c3; TestClass2 c2; c3print(); c2print(); return;(分数:2.00)_正确答案:(正确答案:(1)添加语句:virtual void print()=0; (2)将“class TestClass1:”补充完整为:class TestClass1:virtual private TestClass0 (3)将“class TestClass2:”补充完整为:class TestClass2:public TestClass0 (4)将
13、“class TestClass3:”补充完整为:class TestClass3:public TestClass2,public TestClass1)解析:解析:(1)题目 1要求“请定义一个纯虚函数 print”。在 C+中,虚函数在基类中用 virtual声明成员函数为虚函数。纯虚函数是在声明虚函数时被“初始化”为 0的函数,即“virtual void print()=0;”。 (2)题目 2要求“类 TestClass1私有虚继承类 TestClass0”。在 C+中声明一个派生类时将基类的继承方式指定为 private的,称为私有继承。同(1),声明 TestClass1虚继承
14、类“:virtual TestClass0”,题目要求为私有,即“class TestClass1:virtual privteTestClass0”。 (3)题目 3要求“类 TestClass2公有继承类 TestClass0。”同(2),公有继承是在定义一个派生类时将基类的继承方式指定为 public的,称为公用继承。所以这里补全“class TestClass2:public TestClass0”。 (4)题目 4要求“类 TestClass3公有继承类 TestClass2与 TestClass1。”同(3),所以这里补全“classTestClass3:public TestClass2,public TestClass1”。