1、二级 C+机试 18及答案解析(总分:100.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)1.使用 VC6打开考生文件夹下的工程 test21_1,此工程包含一个源程序文件 test21_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下: The grade is 3 源程序文件 test21_1.cpp清单如下: #includeiostream.h class student private: int grade; public: /* found*/ student(int thegra):(thegra) student() i
2、nt get_grade()return grade; ; void main() int thegra=3; /* found*/ student point=new student(thegra); /* found*/ cout“The grade is“point.get_grade()endl; delete point; (分数:30.00)二、B2简单应用题/B(总题数:1,分数:40.00)2.请编写一个函数 char*change(char instr),将输入字符串中的所有小写字母转换为大写字母输出。要求使用 for循环实现。如输入 jinfeiteng,则输出结果是 JI
3、NFEITENG。 注意:部分源程序已存在文件test21_2.cpp中。 请勿修改主函数 main和其他函数中的任何内容,仅在函数 change的花括号中填写若干语句。 文件 test21_2.cpp的内容如下: char*change(char instr); #include“iostream.h“ void main() char instr50; char *outstr; cout“Input a string:“endl; cininstr; outstr=change(instr); cout“Over graded string:“endl; coutoutstrendl;
4、char*change(char instr) (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)3.使用 VC6打开考生文件夹下的工程 test21_3,此工程包含一个源程序文件 test21_3.cpp,其中定义了用于表示长方形的类 CRectangle,但类 CRectangle的定义并不完整。请按要求完成下列操作,将类CRectangle的定义补充完整。 (1)定义 CRectangle的构造函数,函数含参数 dx,dy,da 和 db,它们都是 double型的数据,请将类数据成员 x,y, a 和 b初始化,并输出“CRectangle Construct
5、ed.”(另起一行输出该文字)。请在注释“/*1*之后添加适当的语句。 (2)完成类 CRectangle的成员函数getperimeter()的定义,将以 a和 b为边的矩形周长的值返回,请在注释“/*2*”之后添加适当的语句。 (3)完成类 CRectangle的成员函数 getarea()的定义,将以 a和 b为边的矩形面积的值返回,请在注释“/*3*”之后添加适当的语句。 (4)完成类 CRectangle的友元函数 friend double dist(CRectangle double y; double a; double b; public: CRectangle() cout
6、“/nCRectangle Constructed.“endl; CRectangle(double dx, double dy, double da, double db) /*1* a=da; b=db; cout“/nCRectangle Constructed.“endl; CRectangle ( ) cout“CRectangle Destructed.“endl; void putxy(double dx, double dy) x=dx; y=dy; void putab(double da, double db)( a=da; b=db; double getx() retu
7、rn x; double gety() return y; double geta() return a; double getb() return b; double getperimeter() /*2* double getarea() /*3* friend double dist(CRectangle ; double dist(CRectangle return sqrt(tx*tx+ty*ty); void main() CRectangle rect; rect.putxy(100.0, 50.0); rect.putab(1200.0, 700.0); cout“Down_L
8、eft corner point is: (“rect.getx() “, “rect.gety()“)“ endl; cout“a= “rect.geta()“, b=“rect.getb() endl; cout“Perimeter of this rectangle is: “rect.getperimeter()endl; cout“Area of this rectangle is:“rect.getarea()endl; cout“The Distance is:“dist(rect)endl; CRectangle recta(200,150,2000,800); cout“Do
9、wn_Left corner point is:(“recta.getx()“,“recta.gety()“)“endl; cout“a=“recta.geta()“, b=“recta.getb()endl; cout“Perimeter of this rectangle is: “recta.getperimeter()endl; cout“Area of this rectangle is: “recta.getarea()endl; cout“The Distance is :“dist(recta)endl; (分数:30.00)_二级 C+机试 18答案解析(总分:100.00,
10、做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)1.使用 VC6打开考生文件夹下的工程 test21_1,此工程包含一个源程序文件 test21_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下: The grade is 3 源程序文件 test21_1.cpp清单如下: #includeiostream.h class student private: int grade; public: /* found*/ student(int thegra):(thegra) student() int get_grade()return grade
11、; ; void main() int thegra=3; /* found*/ student point=new student(thegra); /* found*/ cout“The grade is“point.get_grade()endl; delete point; (分数:30.00)解析:(1)错误:student(int thegra):(thegra) 正确:student(int thegra):grade(thegra) (2)错误:student point=new student(thegra) 正确:student*point=new student(theg
12、ra); (3)错误:cout“The grade is“point.get_grade()endl; 正确:cout“The grade is“point-get_grade()endl; 解析 (1)主要考查考生对于构造函数使用参数列表初始化数据成员的掌握,可以使用参数列表的形式,也可以在函数内部实现,不过参数赋值的双方都必须出现,本题的错误就在于并没有把参数赋值给数据成员; (2)主要考查考生对于使用指针申请动态空间的方法的理解,new 运算符申请的空间返回值为指针类型,指针类型的定义是在变量名前加上“*”; (3)主要考查考生对于对象指针调用成员函数方法的掌握,必须使用符号“-”,而对
13、象本身使用符号“.”。二、B2简单应用题/B(总题数:1,分数:40.00)2.请编写一个函数 char*change(char instr),将输入字符串中的所有小写字母转换为大写字母输出。要求使用 for循环实现。如输入 jinfeiteng,则输出结果是 JINFEITENG。 注意:部分源程序已存在文件test21_2.cpp中。 请勿修改主函数 main和其他函数中的任何内容,仅在函数 change的花括号中填写若干语句。 文件 test21_2.cpp的内容如下: char*change(char instr); #include“iostream.h“ void main() c
14、har instr50; char *outstr; cout“Input a string:“endl; cininstr; outstr=change(instr); cout“Over graded string:“endl; coutoutstrendl; char*change(char instr) (分数:40.00)_正确答案:()解析:char *change(char instr) char *outstr=new char50; const char delta=A-a; int i; for(i=0;instri!=/0;i+) if(instri =a double
15、y; double a; double b; public: CRectangle() cout“/nCRectangle Constructed.“endl; CRectangle(double dx, double dy, double da, double db) /*1* a=da; b=db; cout“/nCRectangle Constructed.“endl; CRectangle ( ) cout“CRectangle Destructed.“endl; void putxy(double dx, double dy) x=dx; y=dy; void putab(doubl
16、e da, double db)( a=da; b=db; double getx() return x; double gety() return y; double geta() return a; double getb() return b; double getperimeter() /*2* double getarea() /*3* friend double dist(CRectangle ; double dist(CRectangle return sqrt(tx*tx+ty*ty); void main() CRectangle rect; rect.putxy(100.
17、0, 50.0); rect.putab(1200.0, 700.0); cout“Down_Left corner point is: (“rect.getx() “, “rect.gety()“)“ endl; cout“a= “rect.geta()“, b=“rect.getb() endl; cout“Perimeter of this rectangle is: “rect.getperimeter()endl; cout“Area of this rectangle is:“rect.getarea()endl; cout“The Distance is:“dist(rect)e
18、ndl; CRectangle recta(200,150,2000,800); cout“Down_Left corner point is:(“recta.getx()“,“recta.gety()“)“endl; cout“a=“recta.geta()“, b=“recta.getb()endl; cout“Perimeter of this rectangle is: “recta.getperimeter()endl; cout“Area of this rectangle is: “recta.getarea()endl; cout“The Distance is :“dist(recta)endl; (分数:30.00)_正确答案:()解析:(1) x=dx; y=dy; (2) return2*(a+b); (3) return a*b; (4) double tx,ty; tx=rt.x+rt.a/2.0; 解析本题主要考查考生对于类的定义和友元函数的定义的理解。注意(4)中使用了求开平方的数学函数 sqrt。