1、二级 C+机试 23 及答案解析(总分:100.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test36_1,此工程包含一个源程序文件 test36_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为: 2 源程序文件 test36_1.cpp 清单如下: #includeiostream.h class amount; class coins /* found */ enum units penny, nickel, dime, quarter, half_dollar /* found */
2、class amount; ; class amount /* found */ coins:units money; public: void setm(); int germ(); ; void amount:setm() money = coins:dime; int amount:getm() return money; int main ( ) amount ob; ob.setm(); cout ob.getm()end1; return 0; (分数:30.00)二、B2简单应用题/B(总题数:1,分数:40.00)2.请编写一个函数 prim(int num),该函数实现判别参
3、数 num 是否为素数,在主函数中利用 prime()函数验证哥德巴猜想任何比 2 大的偶数都可表示为两个素数之和基本功能,根据 main 函数的调用情况给出正确的返回值。 注意:部分源程序已存在文件 test36_2.cpp 中。 请勿修改主函数 main 和其他函数中的任何内容,仅在函数 prim 的花括号中填写若干语句。 文件 test36_1.cpp 的内容如下: #include iostream.h const LEN=100; int prim(int num) void main() int a=7; int cnt=0; cout“a is 7:/n“; int *s; s=
4、new intLEN; for(int i=2;ia;i+) if(!prim(i) scnt=i; cnt+; for (i=0;icnt;i+) for (int j=i+1;jcnt;j+) if (s i +s j =a) coutsi/tsj/tend1; (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test36_3。此工程包含一个 test36_3.cpp,其中定义了类CRectangle,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)定义类CRectangle 的数据成员 width
5、和 height,它们都是 int 型的数据。请在注释“/*1*”之后添加适当的语句。 (2)添加类 CRectangle 的友元函数 duplicate()的声明,其返回值类型和参数类型均为Crectangle 的,请在注释“/*2*”之后添加适当的语句。 (3)完成类 Crectangle 的成员函数set_values 的定义,把传入的参数 a 和 b 分别赋值为数据成员 width 和 height,请在注释“/*3*”之后添加适当的语句。 (4)完成派生类 Crectangle 的友元函数 duplicate 的定义,把函数中的临时对象rectres 的值返回主函数,请在注释“/*
6、4*”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件 test36_3.cpp 清单如下: #include iostream.h class CRectangle / * 1 * public: void set_values (int, int); int area (void) return (width * height); / * 2 * ; void CRectangle:set_values (int a, int b) / * 3 * CRectangle duplicate (CRectangle rectparam) CRec
7、tangle rectres; rectres.width = rectparam.width*2; rectres.height = rectparam.height*2; / * 4 * void main ( ) CRectangle rect, rectb; rect.set_values (2,3); rectb = duplicate (rect); cout rectb.area()end1; (分数:30.00)_二级 C+机试 23 答案解析(总分:100.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程
8、test36_1,此工程包含一个源程序文件 test36_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为: 2 源程序文件 test36_1.cpp 清单如下: #includeiostream.h class amount; class coins /* found */ enum units penny, nickel, dime, quarter, half_dollar /* found */ class amount; ; class amount /* found */ coins:units money; public: void setm(); int
9、 germ(); ; void amount:setm() money = coins:dime; int amount:getm() return money; int main ( ) amount ob; ob.setm(); cout ob.getm()end1; return 0; (分数:30.00)解析:(1) 错误;enum unitspenny,nickel,dime,quarter,half_dollar 正确:enum units penny,nickel,dime,quarter,half_dollar; (2) 错误:class amount; 正确:friend c
10、lass amount; (3) 错误:coins:units money; 正确:coins:units money; 解析 (1)enum 是枚举类型,units 是本题中该类型的一个变量,后面为枚举表,大括号中为枚举符,枚举变量的值实际上是一个有名字的常量,所以它的定义结束时也需要一个分号; (2)类 amount 是类 coins 中定义的成员,要是想要访问 coins 类中的私有成员,把 amount 定义成普通的数据成员是不能做到的,只能把它定义成友元类,即可以访问 coins 类中的所有成员(提示:units 是使用的默认定义,即私有成员); (3)units 是类coins 中
11、的成员,在它的友元类中访问它时应该使用作用域符“:”,本题的错误是使用了冒号。二、B2简单应用题/B(总题数:1,分数:40.00)2.请编写一个函数 prim(int num),该函数实现判别参数 num 是否为素数,在主函数中利用 prime()函数验证哥德巴猜想任何比 2 大的偶数都可表示为两个素数之和基本功能,根据 main 函数的调用情况给出正确的返回值。 注意:部分源程序已存在文件 test36_2.cpp 中。 请勿修改主函数 main 和其他函数中的任何内容,仅在函数 prim 的花括号中填写若干语句。 文件 test36_1.cpp 的内容如下: #include iostr
12、eam.h const LEN=100; int prim(int num) void main() int a=7; int cnt=0; cout“a is 7:/n“; int *s; s=new intLEN; for(int i=2;ia;i+) if(!prim(i) scnt=i; cnt+; for (i=0;icnt;i+) for (int j=i+1;jcnt;j+) if (s i +s j =a) coutsi/tsj/tend1; (分数:40.00)_正确答案:()解析:int prim(int num) int half,flag; flag =0; half=
13、num/2; for (int i=2;i=half;i+) if(num%i=O) flag=1; else continue; if (flag=1) return 1; else return 0; 解析 本题考查的是考生对一般应用的综合考查,主要是对于 for 函数使用的应用。其基本算法如下:从 2 开始到该数的一半进行穷举,每个数都对参数 nam 进行整除,如果发现有任何一个数能够整除 num,则标志变量 flag变为 1,最后返回的时候,返回值根据标志 flag 分别返回能整除 flag 为 1,返回“不能整除 flag为 0,返回 0。三、B3综合应用题/B(总题数:1,分数:3
14、0.00)3.使用 VC6 打开考生文件夹下的工程 test36_3。此工程包含一个 test36_3.cpp,其中定义了类CRectangle,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)定义类CRectangle 的数据成员 width 和 height,它们都是 int 型的数据。请在注释“/*1*”之后添加适当的语句。 (2)添加类 CRectangle 的友元函数 duplicate()的声明,其返回值类型和参数类型均为Crectangle 的,请在注释“/*2*”之后添加适当的语句。 (3)完成类 Crectangle 的成员函数set_values 的定义
15、,把传入的参数 a 和 b 分别赋值为数据成员 width 和 height,请在注释“/*3*”之后添加适当的语句。 (4)完成派生类 Crectangle 的友元函数 duplicate 的定义,把函数中的临时对象rectres 的值返回主函数,请在注释“/* 4*”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件 test36_3.cpp 清单如下: #include iostream.h class CRectangle / * 1 * public: void set_values (int, int); int area (void)
16、return (width * height); / * 2 * ; void CRectangle:set_values (int a, int b) / * 3 * CRectangle duplicate (CRectangle rectparam) CRectangle rectres; rectres.width = rectparam.width*2; rectres.height = rectparam.height*2; / * 4 * void main ( ) CRectangle rect, rectb; rect.set_values (2,3); rectb = duplicate (rect); cout rectb.area()end1; (分数:30.00)_正确答案:()解析:(1) int width,height; (2) friend CRectangle duplicate(CRectangle); (3) widtha; heightb; (4) return(rectres); 解析 主要考查考生对于类的定义和友元函数的掌握,其中(2)中 friend 是定义友元的关键字,注意声明之后的分号不能缺少(4)中从子函数中返回应该使用关键字return,只要符合返回类型的要求就可以直接使用 return 返回。