1、国家二级 C+机试(操作题)模拟试卷 308及答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6打开考生文件夹下的源程序文件 modi1cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: This is a greart! Hello Hello 注意:错误的语句在*error*的下面,修改该语句即可。#includeiostreamhclass CMyClasspublic: void displayl() cout“This is great!“endl; void display2() *error* char
2、str5=“Hello“; coutstrendl; ) *error* void display3(char string) *error* coutstringendl ;void main() CMyClass myclass; myclassdisplayl(); myclassdisplay2(); myclassdisplay3(“Hello“);(分数:2.00)_二、简单应用题(总题数:1,分数:2.00)2.使用 VC6打开考生文件夹下的源程序文件 modi2cpp。阅读下列函数说明和代码,完成空出部分程序。函数 sum(int ANUMNUM,int n)实现的功能是计算矩
3、阵中所有非质数数字的和。 提示:函数isPrime(int n)的功能是判定当前数字是否为质数,如果是则返回 true。 注意:不能修改程序的其他部分,只能修改 sum()函数。#includeiostreamh#includecmath#define NUM 50int ANUMNUM= 10,13,59,70,6, 2,40,89,92,9, 14,55,71,11,19, 79,68,83,97,101, 102,10001,23,45;bool isPrime(int n) if(n=1) return false; if(n=2) return true; for(int i=2;i
4、n2;i+) if(ni=0) return false; return true;int sum(int ANUMNUM,int n)int main() coutsum(A,5)endl; return 0;(分数:2.00)_三、综合应用题(总题数:1,分数:2.00)3.使用 VC6打开考生文件夹下的源程序文件 modi3cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有数据成员 year、month、day,分别用于年、月、日,它们都是 int型的数据,请在注释*1*后添加适当的语句。 (2)完成判断数据成员 date2是否是闰年函数 intDat
5、e:IsLeapYear0 的定义,返回数据成员 date2是否是闰年的值,是则返回 1,否则返回0。请在注释*2*后添加适当的语句。 (3)设置 SetData0的默认时间为:2004-12-1,请在注释*3*后添加适当的语句。 (4)完成主函数 main(),完成输出 date2 is aleapyear,请在注释*4*后添加适当的语句。 提示:公元纪年的年数可以被四整除,即为闰年;被 100整除而不能被 400整除为平年;被 100整除也可被 400整除的为闰年。 注意:增加或修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#includeiostreamusing name
6、space std;Class Datepublic: *3* void SetDate(int y,intm,int n); int IsLeapYear(); Void Print() coutyear“-“month “-“ dayendl;Prlvate: *1*;void Date:SetDate(int y,intm,int d) year=y; month=m; day=d;int Date:IsLeapYear() *2* retUrn int main() Date datel , date2; datelSetDate(); date2SetDate(2004,12,1);
7、 cout“datel:“; datelPrint(); cout“date2:“; date2Print(); *4* Cout “date2 is“ “:“not“)“a leapyear“endl; return 0;(分数:2.00)_国家二级 C+机试(操作题)模拟试卷 308答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6打开考生文件夹下的源程序文件 modi1cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: This is a greart! Hello Hello 注意:错误的语句在*error*的
8、下面,修改该语句即可。#includeiostreamhclass CMyClasspublic: void displayl() cout“This is great!“endl; void display2() *error* char str5=“Hello“; coutstrendl; ) *error* void display3(char string) *error* coutstringendl ;void main() CMyClass myclass; myclassdisplayl(); myclassdisplay2(); myclassdisplay3(“Hello“
9、);(分数:2.00)_正确答案:(正确答案:(1)char str6=“Hello“; 或 char str=“Hello“; (2)void display3(char*string) 或 void display3(char string) (3)coutstringendl;)解析:解析:(1)“Hello“字符串的长度为 5,存放在字符数组中的话,字符数组的长度至少为 6,因为字符数组的最后一个字符为 NULL,故第 1个标识下字符数组长度为 5太短,应改成“char str6=“Hello“;”或“char str1=“Hello“;”。 (2)char string 表示 str
10、ing为一个 char变量,代表一个字符而不是一串字符,程序要求输出一串字符显然字符变量做不到,应该为字符指针或字符数组,故第 2个标识下应改为“void display3(char*string)”或“void display3(char string)”。 (3)编译程序发现第 3标识下有错误,观察程序可知第 3个标识下语句结束没有分号,而 C+语句必须以分号结束,应加上分号,即第 3表示下应改为“coutstringendl;”。二、简单应用题(总题数:1,分数:2.00)2.使用 VC6打开考生文件夹下的源程序文件 modi2cpp。阅读下列函数说明和代码,完成空出部分程序。函数 su
11、m(int ANUMNUM,int n)实现的功能是计算矩阵中所有非质数数字的和。 提示:函数isPrime(int n)的功能是判定当前数字是否为质数,如果是则返回 true。 注意:不能修改程序的其他部分,只能修改 sum()函数。#includeiostreamh#includecmath#define NUM 50int ANUMNUM= 10,13,59,70,6, 2,40,89,92,9, 14,55,71,11,19, 79,68,83,97,101, 102,10001,23,45;bool isPrime(int n) if(n=1) return false; if(n=
12、2) return true; for(int i=2;in2;i+) if(ni=0) return false; return true;int sum(int ANUMNUM,int n)int main() coutsum(A,5)endl; return 0;(分数:2.00)_正确答案:(正确答案:int re=0; for(int i=0;in;i+) for(int j=0;jn;j+) if(!isPrime(Aij) 判断是否是素数,不是素数则累加 Ee=Ee+Aij; return re;)解析:解析:(1)矩阵 ANUMNUM是 2维矩阵,SHIn 函数的参数 n给出了
13、矩阵 A的具体维数,因此矩阵A中共含有 nn个有效数。 (2)ANUMNUM中元素可表示为 Aij,其中 i表示元素所在行数,i 表示元素所在的列数,i、j 分别从 0到 n1 范围内变化,因此设置内外两层循环可逐个遍历整个矩阵寻找非质数。 (3)函数 isprime提供了判断质数的方法,对每个元素 Aij,调用函数 isprime(Aij),判断函数的返刚值是否为假,如果为假说明不是质数,应该累加起来。三、综合应用题(总题数:1,分数:2.00)3.使用 VC6打开考生文件夹下的源程序文件 modi3cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有数据成
14、员 year、month、day,分别用于年、月、日,它们都是 int型的数据,请在注释*1*后添加适当的语句。 (2)完成判断数据成员 date2是否是闰年函数 intDate:IsLeapYear0 的定义,返回数据成员 date2是否是闰年的值,是则返回 1,否则返回0。请在注释*2*后添加适当的语句。 (3)设置 SetData0的默认时间为:2004-12-1,请在注释*3*后添加适当的语句。 (4)完成主函数 main(),完成输出 date2 is aleapyear,请在注释*4*后添加适当的语句。 提示:公元纪年的年数可以被四整除,即为闰年;被 100整除而不能被 400整除
15、为平年;被 100整除也可被 400整除的为闰年。 注意:增加或修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#includeiostreamusing namespace std;Class Datepublic: *3* void SetDate(int y,intm,int n); int IsLeapYear(); Void Print() coutyear“-“month “-“ dayendl;Prlvate: *1*;void Date:SetDate(int y,intm,int d) year=y; month=m; day=d;int Date:IsLeapY
16、ear() *2* retUrn int main() Date datel , date2; datelSetDate(); date2SetDate(2004,12,1); cout“datel:“; datelPrint(); cout“date2:“; date2Print(); *4* Cout “date2 is“ “:“not“)“a leapyear“endl; return 0;(分数:2.00)_正确答案:(正确答案:(1)int year,month,day; (2)将“return”补充完整为:returnfyear400=0)(year4=0year100!=0);
17、(3)将“void SetDate(int y,int m,int n);”补充完整为:void SetDate(int y=2004,int m=12,int n=1); (4)将“cout“date2 is“(“:“not“)“a leapyear“endl;“补充完整为:cout“date2 is“(date2IsLeapYear()?“:“not“)“a leapyear“endl;)解析:解析:(1)题目 1要求“定义私有数据成员 year、month、day,分别用于年、月、日,它们都是int型的数据”。在程序中的 Date类中的 priate区域添加数据成员 year、month
18、、day 的定义即可,其中它们的类型根据题目要求定义为 int型。即在第 1个标识下添加“int year,month,day;”。 (2)题目 2要求“完成判断数据成员 date2是否是闰年函数 int Date:IsLeapYear()的定义”。公元纪年的年数可以被 100整除而不能被 400整除为平年;被 100整除也可被 400整除的为闰年。用程序语言表达为“(year400=0)(year4=0year100!=0)”,即在第 2个标识补充的返回值语句为“return(year400=0)(year4=0year100!=0);”。 (3)题目 3要求“设置 SetData0的默认时
19、间为:2004-12-1”。程序中类 Date的成员函数定义为“voidSetDate(int y,int m,int n);”,设置默认时间为:2004121,就是设置 SetDate函数中的变量值分别为 2004,12,1。即成员函数的定义语句修改为“voidSetDate(int y=2004,int m=12,int n=1);”。 (4)题目 4要求“完成主函数main(),完成输出 date2 is a leapyear”。标识 4下的语句“cout“date2is“(“:“not”)“a leapyear”“date2 is“(date2IsLeapYear07()?:“not“)“a leapyear“endl;”。