1、二级 C+机试-76 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test40_1,此工程包含一个源程序文件 test40_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:The first result is 5The second result is 2The third result is 2The fourth result is 6源程序文件 test40_1.cpp 清单如下:#include iostream.hint subtraction (int a,
2、int b)int r;r=a-b;/* found */return int main ()int x=5, y=3, z;z = subtraction (7,2);cout “The first result is “ z /n;cout “The second result is “ subtraction(7,subtraction (7,2) /n;/* found */cout “The third result is “ subtraction (/* found */z= 4 + *subtraction (x,y);cout “The fourth result is “
3、z /n;return 0;(分数:30.00)填空项 1:_二、2简单应用题(总题数:1,分数:40.00)2.请编写一个函数 printdate(int year,int month,int day),该函数实现将输入的 3 个数字转换成英语数字纪年输出的功能,如输入 March9,1978,则输出 1978 3 9。注意:使用 switch 结构实现该函数的基本功能并应该能够判断错误的输入。部分源程序已存在文件 test40_2.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 printdate 的花括号中填写若干语句。源程序文件 rest40_2.cpp 清单如
4、下:#includeiostream.hvoid printdate(int year, int month, int day)void main()printdate(1978,3,9);(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test40_3。此工程包含一个 test40_3.cpp,其中定义了类CVector,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)完成类 CVector 构造函数的定义,把传入的 int 型参数 a 和 b 分别赋值给类的成员变量 x 和 y。请在注释“/*1*”之后添加适
5、当的语句。(2)完成类 CVector 的“+”运算符重载函数的定义,使 Cvector 类的两个对象相加时可以直接使用符号“+”,并且结果仍然为类 Cvector 的对象,请在注释“/*2*”之后添加适当的语句。(3)在主函数中添加适当的语句,把类 Cvector 的对象 a 和 b 相加,并且把结果保存在对象 c 中,最后以“x,y”的形式输出在屏幕上,请在注释“/*3*”之后添加适当的语句。源程序文件 test40_3.cpp 清单如下:#include iostream.hclass CVectorpublic:int x,y;CVector () ;CVector (int,int)
6、;CVector operator + (CVector);CVector:CVector (int a, int b)/*1*CVector CVector:operator+ (CVector param)/*2*temp.y = y + param.y;return (temp);int main ()CVector a (3,1);CVector b (1,2);CVector c;/*3*return 0;(分数:30.00)_二级 C+机试-76 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程
7、 test40_1,此工程包含一个源程序文件 test40_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:The first result is 5The second result is 2The third result is 2The fourth result is 6源程序文件 test40_1.cpp 清单如下:#include iostream.hint subtraction (int a, int b)int r;r=a-b;/* found */return int main ()int x=5, y=3, z;z = subtraction (
8、7,2);cout “The first result is “ z /n;cout “The second result is “ subtraction(7,subtraction (7,2) /n;/* found */cout “The third result is “ subtraction (/* found */z= 4 + *subtraction (x,y);cout “The fourth result is “ z /n;return 0;(分数:30.00)填空项 1:_ (正确答案:(1) 错误;return (分数:40.00)_正确答案:(void printd
9、ate(int year, int month, int day)if(year0|month1|month12|day1|day31)cout“ERROR“;return;switch(month)case 1:cout“January“;break;case 2:cout“February“;break;case 3:cout“March“;break;case 4:eout“April“;break;case 5:cout“May“;break;case 6:cout“June“;break;case 7:cout“July“;break;case 8:cout“Auguest“;bre
10、ak;case 9:cout“September“;break;case 10:cout“October“;break;case 11:cout“November“;break;case 12:cout“December“;break;cout“ “day“,“yearendl;)解析:解析 本题考查的是考生对 switch 结构的应用。switch 分支结构也是常用的选择结构,对于每个 case 结构,只有遇到 break 才会中止并且跳出 switch 结构,否则会一直执行到下一个 break 或者switch 的结尾,而对于参数的预处理应该是程序健壮性的基本要求。三、3综合应用题(总题数
11、:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test40_3。此工程包含一个 test40_3.cpp,其中定义了类CVector,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)完成类 CVector 构造函数的定义,把传入的 int 型参数 a 和 b 分别赋值给类的成员变量 x 和 y。请在注释“/*1*”之后添加适当的语句。(2)完成类 CVector 的“+”运算符重载函数的定义,使 Cvector 类的两个对象相加时可以直接使用符号“+”,并且结果仍然为类 Cvector 的对象,请在注释“/*2*”之后添加适当的语句。(3)在主函数中添加适
12、当的语句,把类 Cvector 的对象 a 和 b 相加,并且把结果保存在对象 c 中,最后以“x,y”的形式输出在屏幕上,请在注释“/*3*”之后添加适当的语句。源程序文件 test40_3.cpp 清单如下:#include iostream.hclass CVectorpublic:int x,y;CVector () ;CVector (int,int);CVector operator + (CVector);CVector:CVector (int a, int b)/*1*CVector CVector:operator+ (CVector param)/*2*temp.y =
13、y + param.y;return (temp);int main ()CVector a (3,1);CVector b (1,2);CVector c;/*3*return 0;(分数:30.00)_正确答案:(1)x=a;y=b;(2)CVector temp;temp.x=x+param.x;(3)c=a+b;toutc.x“,“c.y;)解析:解析 主要考查考生对于类的数据成员赋值与定义重载为成员函数的运算符函数的掌握,其中(2)就是运算符函数的实现,实际上就是通过已有的函数和符号实现复杂的运算功能,并最终返回该类的对象,注意最后的返回值必须是对象才能满足要求,(4)中使用对象调用类的数据成员用符号“.”。