1、二级 C+机试-165 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC+6.0 打开考生文件夹下的源程序文件 1.cpp,该程序运行时有错误,请改正程序中的错误,本题的功能是从键盘中输入字符串 str,然后输出字符串 str 中的字符个数。注意:错误的语句在/*error*/的下面,修改该语句即可。其他的语句不能修改。试题程序:#includeiostreamint main()/*error*/cout“please input a string:“end1;/*error*/namespace std;char str256
2、;cin.getline(str,256);coutstrlen(str)end1;return 0;(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.使用 VC+6.0 打开考生文件夹下的源程序文件 2.cpp。请完成函数 un(char*str1,char*str2),此函数的功能是计算 str1 中出现 str2 的个数,当不出现时,则返回 0。如str1 为“asdfsfdfg”str2 为“sf”则返回 1str2 为“df”则返回 3注意:不能修改函数的其他部分。试题程序:#includeiostream.h/注意只能使用 int 类型,不能类型转换int
3、fun(char*str1,char*str2)void main()char str1l024;char str21256;cout“please input a string:“end1;cin.getline(str1,1024);cout“please input other string:“end1;cin.getline(str2,256);coutfun(str1,str2);coutend1;return;(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)使用 VC+6.0 打开考生文件夹下的源程序文件 3.cpp,其中定义了用于表示日期的类 Date,但类
4、 Date 的定义并不完整。请按要求完成下列操作,将类 Date 的定义补充完成。(1)定义私有数据成员 year、month 和 day,分别用于表示年、月和日,它们都是 int 型的数据。请在注释 1 之后添加适当的语句。(2)完成默认构造函数 Date 的定义,使 Date 对象的默认值为 year=1,month=1,day=1,请在注释 2 之后添加适当的语句。(3)完成重载构造函数 Date(int y,int m,int d)的定义,把数据成员 year、month 和 day 分别初始化为参数 y、m 和 d 的值,请在注释 3 之后添加适当的语句。(4)完成成员函数 prin
5、t 的类外定义,使其以“年一月一日”的格式将 Date 对象的值输出到屏幕上,例如1949-10-i。请在注释 4 之后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。试题程序:#includeiostream.hclass Datepublic:/*1*。Date(int y,int m,int d)/*2*void print()const;private:/data member/*3*;void Date:print()const/*4*int main()Date Olympic_BJ(2008,8,8);Olympic_BJ.print();return
6、 0;(分数:30.00)填空项 1:_填空项 1:_填空项 1:_填空项 1:_二级 C+机试-165 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC+6.0 打开考生文件夹下的源程序文件 1.cpp,该程序运行时有错误,请改正程序中的错误,本题的功能是从键盘中输入字符串 str,然后输出字符串 str 中的字符个数。注意:错误的语句在/*error*/的下面,修改该语句即可。其他的语句不能修改。试题程序:#includeiostreamint main()/*error*/cout“please input a string:
7、“end1;/*error*/namespace std;char str256;cin.getline(str,256);coutstrlen(str)end1;return 0;(分数:30.00)_正确答案:(本题考查了考生对名字空间的了解情况。(1)“cout“please input a string:“end1;”应改为“std:cout“please input a string:“std:end1;”。(2)“namespace std;”应改为“using namespace std;”。)解析:解析 本题中程序使用的头文件是iostream,没有.h 后缀,该头文件中定义的
8、所有标识符都位于 C+标准库名字空间“std”内。所以语句“cout“please input a string:“end1;”中的cout 和 end1 都要加上 std:前缀才能使用。如果要使后面所有语句都能用到 std 名字空间中和内容,省略加 std:前缀的麻烦,可以使用语句“using namespace std;”。二、2简单应用题(总题数:1,分数:40.00)2.使用 VC+6.0 打开考生文件夹下的源程序文件 2.cpp。请完成函数 un(char*str1,char*str2),此函数的功能是计算 str1 中出现 str2 的个数,当不出现时,则返回 0。如str1 为“
9、asdfsfdfg”str2 为“sf”则返回 1str2 为“df”则返回 3注意:不能修改函数的其他部分。试题程序:#includeiostream.h/注意只能使用 int 类型,不能类型转换int fun(char*str1,char*str2)void main()char str1l024;char str21256;cout“please input a string:“end1;cin.getline(str1,1024);cout“please input other string:“end1;cin.getline(str2,256);coutfun(str1,str2);
10、coutend1;return;(分数:40.00)_正确答案:(int fun(char*str1,char*str2)int size=0;int strllen;int str2len;for(strlfen=0;str1strllen!=0;str1len+);for(str2len=0;str2str2len!=0;str2len+);for(int i=0;istrllen;i+)int j=0;for(;jstr2len;j+)if(str1i+j!=str2j)break;if(j=str2len)+size;i+=j;return size;)解析:解析 本题考查的是字符串查
11、找算法。首先求出两个字符串的长度,然后循环遍历第 1 个字符串,再使用一个内循环遍历第 2 个字符串,判断两个字符串相应位置是否一致。如果遍历完第 2 个字符串,所有位置都一致,则记数值 size 增 1,即找到一个子串。三、3综合应用题(总题数:1,分数:30.00)使用 VC+6.0 打开考生文件夹下的源程序文件 3.cpp,其中定义了用于表示日期的类 Date,但类 Date 的定义并不完整。请按要求完成下列操作,将类 Date 的定义补充完成。(1)定义私有数据成员 year、month 和 day,分别用于表示年、月和日,它们都是 int 型的数据。请在注释 1 之后添加适当的语句。
12、(2)完成默认构造函数 Date 的定义,使 Date 对象的默认值为 year=1,month=1,day=1,请在注释 2 之后添加适当的语句。(3)完成重载构造函数 Date(int y,int m,int d)的定义,把数据成员 year、month 和 day 分别初始化为参数 y、m 和 d 的值,请在注释 3 之后添加适当的语句。(4)完成成员函数 print 的类外定义,使其以“年一月一日”的格式将 Date 对象的值输出到屏幕上,例如1949-10-i。请在注释 4 之后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。试题程序:#includeio
13、stream.hclass Datepublic:/*1*。Date(int y,int m,int d)/*2*void print()const;private:/data member/*3*;void Date:print()const/*4*int main()Date Olympic_BJ(2008,8,8);Olympic_BJ.print();return 0;(分数:30.00)填空项 1:_ (正确答案:应添加“Date()year=1;month=1;day=1;)”或“Date():year(1),month(1),day(1)”。)解析:填空项 1:_ (正确答案:应
14、添加“year=y;month=m;day=d;”。)解析:填空项 1:_ (正确答案:应添加“int year,month,day;”。)解析:填空项 1:_ (正确答案:应添加“coutyear“-“month“-“dayend1;或 coutyear-“month-dayend1;”。)解析:解析 本题考查了类的定义。根据题意,此处应定义 3 个 int 型数据成员 year、month 和 day。根据题意,第 2 处应添加默认构造函数,并初始化 3 个变量,这里既可以使用赋值语句来实现,也可以使用初始化列表。第 3 处只能通过赋值语句初始化 3 个成员,因为注释 3 位于函数中,无法在此处使用初始化列表。添加输出语句,注意题目头包含的头文件是 iostream.h,不能添加 std:前缀。