1、国家二级 C+机试(操作题)-试卷 23 及答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6 打开考生文件夹下的源程序文件 modi1cpp,但该程序运行时有错,请改正 main()函数中的错误,使程序的输出结果如下:ConstructorDefault constructorArea is12Area is0Area is12 注意:错误的语句在*error*的下面,修改该语句即可。#includeiostreamhclass CRectangleprivate:double length,width;public:CRectang
2、le()cout“Defaultconstructorn“;CRectangle(double1,double w)length=1;width=w;cout“Constructorn“;void Set(double1,double w)this-length=1;this-width=w;void GetArea()cout“Area is“length*widthendl;void main()CRectangle Rectl(30,40);*error*CRectangle Rect2(1);*error*CRectangle Rect3;Rect1GetArea();*error*R
3、ect2Set(0);Rect2GetArea();Rect3GetArea();(分数:2.00)_二、简单应用题(总题数:1,分数:2.00)2.使用 VC6 打开考生文件夹下的源程序文件 modi2cpp。阅读下列函数说明和代码。补充函数convert(long s,long*str),使之从低位开始取出长整型变量 S 中奇数位上的数,依次存放在数 str 中。例如,当 S 中的数为:7654321 时,str 中的数为:7531。注意:请勿改动主函数。#includeiostreamhvoid convert(long s,long *str)void main()long s,res
4、;cout“please enter s:“endl;cins;convert(s,&res);cout“The result is:“resendl:return;(分数:2.00)_三、综合应用题(总题数:1,分数:2.00)3.使用 VC6 打开考生文件夹下的源程序文件 modi3cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)完成构造函数的定义,使得数据成员变量 filename 和 contex为空,内容长度 FileLength 为 0。请在注释*1*后添加适当的语句。(2)完成析构函数,对 contex 内存的释放。请在注释*2*后添加适
5、当的语句。(3)完成 WriteFile()函数,用来对 contex 内容进行设置填充内容,如果以前有内容,则删除以前的内容,重新填写。请在注释*3*后添加适当的语句。(4)完成 CopyFileO 函数,完成将参数对象的 contex 的内容拷贝到当前的文件内容中,请在注释*4*后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#includeiostreamhclassM yFileClassprivate:char filename64;char* contex;int FileLength;public:MyFileClass()*1*contex=NUL
6、L;FileLength=0;MyFileClass()if(FileLength)*2*void OpenFile(char* str,int len)for(int i=0;ilen;i+)filenamei=stri;fiienamelen=0;FileLength=0;if(contex)deletecontex;contex=NULL;void WriteFlie(char* str, int len)if(contex!=NULL)*3*contex=new charlen+1;for(int i=0;ilen;i+)contexi=stri;contexlen=0;FiieLen
7、gth=len;void CopyFile(MyFileClass&file)*4*void display()coutcontexendl;int main()MyFileClass file1,file2;file1OpenFile(“txt1“,4);file2OpenFile(“txt2“,4);file1WriteFlie(“hello!“,6);file2CopyFile(filel);file1display();file2display();return0;(分数:2.00)_国家二级 C+机试(操作题)-试卷 23 答案解析(总分:6.00,做题时间:90 分钟)一、基本操作
8、题(总题数:1,分数:2.00)1.使用 VC6 打开考生文件夹下的源程序文件 modi1cpp,但该程序运行时有错,请改正 main()函数中的错误,使程序的输出结果如下:ConstructorDefault constructorArea is12Area is0Area is12 注意:错误的语句在*error*的下面,修改该语句即可。#includeiostreamhclass CRectangleprivate:double length,width;public:CRectangle()cout“Defaultconstructorn“;CRectangle(double1,dou
9、ble w)length=1;width=w;cout“Constructorn“;void Set(double1,double w)this-length=1;this-width=w;void GetArea()cout“Area is“length*widthendl;void main()CRectangle Rectl(30,40);*error*CRectangle Rect2(1);*error*CRectangle Rect3;Rect1GetArea();*error*Rect2Set(0);Rect2GetArea();Rect3GetArea();(分数:2.00)_正
10、确答案:(正确答案:(1)CRectangle Rect2; (2)CRectangle Rect3(Rect1); (3)Rect2Set(0,0);)解析:解析:(1)构造函数 CRectangle()不带有参数,CRectangle(double1,double w)带有 2 个参数,在创建对象时会自动调用构造函数,但是参数必须匹配,第 1 个标识下定义 Rect2 对象时,有一个参数,而类 CRectangleoe()中并没有重载一个参数的构造函数,编译后出错,因此第 1 标识下应改为“CRectangle Rect2;”。 (2)Rect3GetArea()输出的结果和 Rect1G
11、etArea()输出结果一致,因此对象 Rect3 和对象 Rect1 两者具有相同的 length 和 width 值,除定义 Rect3 对象之外没有改变 Rect31的 length 和 width 值,因此 Rect3 成员变量的初始化是通过拷贝构造函数类实现的,即用 Rect1 对象去初始化 Rect3,因此第 2 个标识下应改为“CRectangle Rect3(Rect1):”。 (3)Rect2GetArea()的输出结果为 0,说明 Rect2 的成员变量 length 和 width 值至少一个为 0,而 Set()函数必须有两个参数,这里 Rect2Set(0)参数不匹配
12、,应改为“Rect2Set(0,0);”或者“Rect2Set(0,5)”,两个参数当中至少一个为 0 即可。二、简单应用题(总题数:1,分数:2.00)2.使用 VC6 打开考生文件夹下的源程序文件 modi2cpp。阅读下列函数说明和代码。补充函数convert(long s,long*str),使之从低位开始取出长整型变量 S 中奇数位上的数,依次存放在数 str 中。例如,当 S 中的数为:7654321 时,str 中的数为:7531。注意:请勿改动主函数。#includeiostreamhvoid convert(long s,long *str)void main()long s
13、,res;cout“please enter s:“endl;cins;convert(s,&res);cout“The result is:“resendl:return;(分数:2.00)_正确答案:(正确答案:long s1=10; *str=s10;先取个位数 while(s0) ( s=s100;每次除以 100, *str=s10*s1+*str;取除以 100 后的个位数,str 增加 1 位 s1=s1*10; )解析:解析:(1)由审题分析可知,奇数位即为个位数、百位数、万位数,取个位数比较简单,S10便得到了个位数。获取百位数可由 s100 后,再和 10 取余得到,同样万
14、位数可由 s10000,再和 10 取余得到,从百位数开始后面的奇数位都是在上次的基础上再除以 100 后再取余数,因此可利用循环。 (2)在循环外先取 S 的个位数,然后进入循环,s=s100,获取除以 100 的个位数方法为 s10,这个数是*str 的高一位,乘以它所在*str 中的位置的权值,然后和以前的值相加便得到这次*str 值,如此循环,直到 s 为 0 为止。三、综合应用题(总题数:1,分数:2.00)3.使用 VC6 打开考生文件夹下的源程序文件 modi3cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)完成构造函数的定义,使得数据成
15、员变量 filename 和 contex为空,内容长度 FileLength 为 0。请在注释*1*后添加适当的语句。(2)完成析构函数,对 contex 内存的释放。请在注释*2*后添加适当的语句。(3)完成 WriteFile()函数,用来对 contex 内容进行设置填充内容,如果以前有内容,则删除以前的内容,重新填写。请在注释*3*后添加适当的语句。(4)完成 CopyFileO 函数,完成将参数对象的 contex 的内容拷贝到当前的文件内容中,请在注释*4*后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#includeiostreamhclassM
16、 yFileClassprivate:char filename64;char* contex;int FileLength;public:MyFileClass()*1*contex=NULL;FileLength=0;MyFileClass()if(FileLength)*2*void OpenFile(char* str,int len)for(int i=0;ilen;i+)filenamei=stri;fiienamelen=0;FileLength=0;if(contex)deletecontex;contex=NULL;void WriteFlie(char* str, int
17、len)if(contex!=NULL)*3*contex=new charlen+1;for(int i=0;ilen;i+)contexi=stri;contexlen=0;FiieLength=len;void CopyFile(MyFileClass&file)*4*void display()coutcontexendl;int main()MyFileClass file1,file2;file1OpenFile(“txt1“,4);file2OpenFile(“txt2“,4);file1WriteFlie(“hello!“,6);file2CopyFile(filel);fil
18、e1display();file2display();return0;(分数:2.00)_正确答案:(正确答案:(1)添加语句:filename0=0;或 filename0=NULL; (2)添加语句:delete contex; (3)添加语句:delete contex; (4)添加语句:WriteFlie(filecontex,fileFileLength);)解析:解析:(1)构造函数 MyFileClass()完类成员变量的初始化,将数据成员变量 filename 和 contex为空,内容长度 FileLength 为 0,因此第 1 个标识下应添加“filename0=0;”或
19、“filename0=NULL;”。 (2)析构函数用来释放一个对象,在对象删除前,用它来做一些清理工作,WriteFlie()函数会申请内存空间,因此在对象删除的时候必须将这些内存空间释放,这个工作可以放在析构函数中来处理,第 2 个标识下是释放由 new 申请的内存空间,因此第 2 个标识下应添加“deletecontex;”。 (3)WriteFile()函数首先对 contex 内容清空,第 3 个标识下应添加“delete contex;”。 (4)CopyFile(MyFileClass&file)函数完成将参数 file 对象的 contex 的内容拷贝到当前的文件内容中,可直接调用 WriteFlie()函数,因此第 4 个标识下应添加“WriteFlie(filecontex,fileFileLength);”。