1、二级 C+机试-27 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test31_1,此工程包含一个源程序文件 test31_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:max(1,2)2max(1,4)4源程序文件 test31_1.cpp 清单如下:#include iostream.h/* found */template class TT GetMax (T a, T b)T result;result = (ab)?a:b;return result;void
2、main()int i=1, j=2;double k=4;cout“max(“i“,“j“)=“GetMax(i,j)end1;/* found */cout“max(“i“,“k“)=“GetMax(i,k)end1;/* found */return 0;(分数:30.00)填空项 1:_二、2简单应用题(总题数:1,分数:40.00)2.请编写一个函数 fun(int score 3,int num),该函数返回有一门成绩以上课程成绩在 85 分以上,其余课程成绩不低于 70 分的人数。数组 score 按行存放 num 名考生各自的三门期末考试成绩。注意:部分源程序已存在文件 tes
3、t31_2.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填写若干语句。程序输出结果如下:3文件 test31_2.cpp 清单如下:#include iostream.hint fun(int score 3,int num)void main ( )int score4 3=70,89,92,70,76,93,(80,86,98,65,73,45);coutfun(score,4)end1;(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test31_3。此工程包含一个 test31
4、_3.cpp,其中定义了可以动态分配的字符串类,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)定义类 tstring 的私有数据成员 length 和 p,它们分别是血型的数据和字符型的指针,其中 length表示一个字符串的长度。请在注释“/*1*”之后添加适当的语句。(2)完成类 tstring 的构造函数,使 length 等于字符串 m 的长度,并申请由指针 p 指向的 length 长的字符型空间,将 m 所指的字符串复制到该空间。请在注释“/*2*”之后添加适当的语句。(3)完成类 tstring 的成员函数 strcopy(tstringn)的定义,该函数将对
5、象 n 的值(包括字符串长度和字符串本身)复制给调用该函数的对象(对象 n 的字符串长度任意)。请在注释“/*3*”之后添加适当的语句。(4)完成类 tstring 的友元函数 strlink(tstring m,tstring n)的定义,该函数将可动态分配的字符串类对象 m 和 n 的字符串成员连接在一起(对象 m 和 n 的字符串长度任意),并返回该串。请在注释“/*4*”之后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。程序输出结果如下:hello the world!源程序文件 test31_3.cpp 清单如下:#includeiostream.h#i
6、ncludeconio.h#include string.hclass tstringpublic:/* 1 *tstring(char *m)/* 2 *strcpy(p,m);void strcopy(tstring n);friend char *strlink(tstring m,tstring n);void tstring:strcopy(tstring n)/* 3 *char *q;q=new charlength;strcpy(q,tstring:p);q=strcat(tstring:p,n.p);tstring:p=q;tstring:length=strlen(tstr
7、ing:p);char *strlink(tstring m, tstring n)int length=strlen(m.p)+strlen(n.p);char *p;/* 4 *strcpy(p,m.p);p=strcat(p,n.p);return p;void main ( )tstring a(“hello “);tstring b(“the world“);tstring c(“!“);a.strcopy(b);coutstrlink(a,c)end1;(分数:30.00)_二级 C+机试-27 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:3
8、0.00)1.使用 VC6 打开考生文件夹下的工程 test31_1,此工程包含一个源程序文件 test31_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:max(1,2)2max(1,4)4源程序文件 test31_1.cpp 清单如下:#include iostream.h/* found */template class TT GetMax (T a, T b)T result;result = (ab)?a:b;return result;void main()int i=1, j=2;double k=4;cout“max(“i“,“j“)=“GetMax
9、(i,j)end1;/* found */cout“max(“i“,“k“)=“GetMax(i,k)end1;/* found */return 0;(分数:30.00)填空项 1:_ (正确答案:(1)错误:template class T正确:templateclass T(2) 错误:cout“max(“i“,“k“)“GetMax(i,k)end1;正确:cout“max(“i“,“k“)“GetMaxdouble(i,k)end1;(3) 错误:return 0;正确:应将“return 0;”删除)解析:解析 (1)函数模板的格式是 template模板形参表声明函数声明,所以
10、class T 应该由“”括起来;(2)变量 i 为 int 型,而变量 k 为 double 型,编译系统找不到相应的函数,所以需要显式的给出模板实参,强制生成对特定实例的调用;(3)由 void main()可看出 main 函数不需要返回值,所以必须把“return 0;”删除。二、2简单应用题(总题数:1,分数:40.00)2.请编写一个函数 fun(int score 3,int num),该函数返回有一门成绩以上课程成绩在 85 分以上,其余课程成绩不低于 70 分的人数。数组 score 按行存放 num 名考生各自的三门期末考试成绩。注意:部分源程序已存在文件 test31_2
11、.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填写若干语句。程序输出结果如下:3文件 test31_2.cpp 清单如下:#include iostream.hint fun(int score 3,int num)void main ( )int score4 3=70,89,92,70,76,93,(80,86,98,65,73,45);coutfun(score,4)end1;(分数:40.00)_正确答案:(int fun(int score 3,int num)int total=0;int flag=0;for (int i=O;inu
12、m;i+)for(int j=O;j3;j+)if (scorei j70)flag=-1;j=3;else if(scorei j=85) flag=1;if (flag=1) total=total+1;flag=0;return total;)解析:解析 本题主要考查考生对数组和基本控制语句的熟练程度。对于 4*3 的二维数组 score,其下标的范围是从(0,0)到(3,2),这一点是需要特别注意的。另外程序中通过设置,临时变量 flag 来标志每个人的分数信息,以得到所要统计的人数的方法是需要在编程中灵活掌握的。三、3综合应用题(总题数:1,分数:30.00)3.使用 VC6 打开考
13、生文件夹下的工程 test31_3。此工程包含一个 test31_3.cpp,其中定义了可以动态分配的字符串类,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)定义类 tstring 的私有数据成员 length 和 p,它们分别是血型的数据和字符型的指针,其中 length表示一个字符串的长度。请在注释“/*1*”之后添加适当的语句。(2)完成类 tstring 的构造函数,使 length 等于字符串 m 的长度,并申请由指针 p 指向的 length 长的字符型空间,将 m 所指的字符串复制到该空间。请在注释“/*2*”之后添加适当的语句。(3)完成类 tstring
14、的成员函数 strcopy(tstringn)的定义,该函数将对象 n 的值(包括字符串长度和字符串本身)复制给调用该函数的对象(对象 n 的字符串长度任意)。请在注释“/*3*”之后添加适当的语句。(4)完成类 tstring 的友元函数 strlink(tstring m,tstring n)的定义,该函数将可动态分配的字符串类对象 m 和 n 的字符串成员连接在一起(对象 m 和 n 的字符串长度任意),并返回该串。请在注释“/*4*”之后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。程序输出结果如下:hello the world!源程序文件 test31
15、_3.cpp 清单如下:#includeiostream.h#includeconio.h#include string.hclass tstringpublic:/* 1 *tstring(char *m)/* 2 *strcpy(p,m);void strcopy(tstring n);friend char *strlink(tstring m,tstring n);void tstring:strcopy(tstring n)/* 3 *char *q;q=new charlength;strcpy(q,tstring:p);q=strcat(tstring:p,n.p);tstrin
16、g:p=q;tstring:length=strlen(tstring:p);char *strlink(tstring m, tstring n)int length=strlen(m.p)+strlen(n.p);char *p;/* 4 *strcpy(p,m.p);p=strcat(p,n.p);return p;void main ( )tstring a(“hello “);tstring b(“the world“);tstring c(“!“);a.strcopy(b);coutstrlink(a,c)end1;(分数:30.00)_正确答案:(1)int length;char *p;(2)length=strlen(m);p=new charlength;(3)int length=strlen(tstung:p)+strlen(n.p);(4)p=new charlength;)解析:解析 本题是对类与字符串常用函数的综合考查。字符串常用函数 strcpy、strcat 和 strlen 的调用格式和调用限制是需要熟练掌握的。程序中通过申请新的空间和改变指针指向解决了原有字符串函数strcpy 的目标字符串空间必须足够大的问题。根据实际情况利用以有类建立新类的能力是需要培养的。