1、国家二级 C+机试(操作题)模拟试卷 321及答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6打开考生文件夹下的源程序文件 modi1cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为: 1 1 2 1 注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构,错误的语句在*error*的下面。#includeiostreamhClass TestClass *error* *error* const int j;public: TestClass() *error* static int i
2、=0; cout+iendl; coutjendl; ;Void main() TestClass obj1; TestClass obj2; obj2j +=obj1j; return;(分数:2.00)_二、简单应用题(总题数:1,分数:2.00)2.使用 VC6打开考生文件夹下的源程序文件 modi2cpp。阅读下列函数说明和代码,完成空出部分程序。函数 convert(char*stroct)的功能是将八进制转换为十进制。 提示:要每一位转换,然后把转换后得到的数累加起来即可。 注意:不能修改其他部分的代码。 #include #include int convert(char*str
3、oct) int main() coutconvert(“7“)convert(“10“)convert(“1234“)_三、综合应用题(总题数:1,分数:2.00)3.使用 VC6打开考生文件夹下的源程序文件 modi3cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)完成构造函数的定义,请在注释*1*后添加适当的语句。 (2)定义类的友元函数 fun(),请在注释*2*后添加适当的语句。 (3)定义类的友元函数 main(),请在注释*3*后添加适当的语句。 (4)补充语句,释放内存。请在注释*4*后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程
4、序中的其他内容。#includeiostreamhClass TestClasspublic: *1* data=d; next=NULL; void SetNext(TestClass*p) next=p; private: int data; TestClass*next; *2* *3;TestClass*fun(TestClass*h) TestClass *t,*s,*u,*v; s=h-next; h-next=NULL; while(s!=NULL) for(t=s,v=h;v!=NULLv-data t-data;u=v,v=v-next); s=s 一next; if(v=h
5、) h=t; else u-next=v; t-next=v; return h;Void main() TestClass*h=NULL; TestClass*q=NULL; do int data; cout“please input anumber,and end with 0“endl; cindata; if(data=0)break; TestClass*p = newTestClass(data); if(h=NULL) h=q=p; else q-SetNext(p); q=p; while(1); h=fun(h); for(;h!=NULL;) q=h-next; cout
6、h-data“ “endl; delete h; *4* return;(分数:2.00)_国家二级 C+机试(操作题)模拟试卷 321答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6打开考生文件夹下的源程序文件 modi1cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为: 1 1 2 1 注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构,错误的语句在*error*的下面。#includeiostreamhClass TestClass *error* *error* const i
7、nt j;public: TestClass() *error* static int i=0; cout+iendl; coutjendl; ;Void main() TestClass obj1; TestClass obj2; obj2j +=obj1j; return;(分数:2.00)_正确答案:(正确答案:(1)添加语句:public: (2)int j; (3)添加语句:j=1;)解析:解析:(1)打开 modi1cpp,调试程序,显示错误提示为“modi1cpp(22):error C2248:j:cannotaccess private member declared in
8、class TestClass”,第 1个标识下的 j变量由于被定义为私有的,其他函数不可获得,这里应该是 public型,所以在第 1个标识下添加“punic:”。(2)程序中对于 j的调用,说明 j并不是常量,所以修改 i的定义,将其定义为变量,而不是常变量,即修改第 2个标识下语句为“int i;”。 (3)题目中要求调用对象,输出 i的值为 1。原程序中 j变量的使用并没有赋初始化值,输出值为乱码,所以第三个标识下添加“i=1;”。二、简单应用题(总题数:1,分数:2.00)2.使用 VC6打开考生文件夹下的源程序文件 modi2cpp。阅读下列函数说明和代码,完成空出部分程序。函数
9、convert(char*stroct)的功能是将八进制转换为十进制。 提示:要每一位转换,然后把转换后得到的数累加起来即可。 注意:不能修改其他部分的代码。 #include #include int convert(char*stroct) int main() coutconvert(“7“)convert(“10“)convert(“1234“)_正确答案:(正确答案:int re=0; while(*strOct) re=re*8+(*strOct)-0; strOct+; return re;)解析:解析:(1)定义整型变量变量 re,re 记录求得的 10进制数。 (2)conv
10、ert(char*strOct)的输入参数为字符指针,而不是整形数据指针,因此需要将 ASCII码转换成相应的数字量,即(*strOct)-0。 (3)按权展开可采用循环迭代的方法,如:(123) 8 =(18)+2)8+3,即迭代过程为上一次运算得到的结果再乘以 8加现在的元素值,即 re=re*8+(*strOct)-0。三、综合应用题(总题数:1,分数:2.00)3.使用 VC6打开考生文件夹下的源程序文件 modi3cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)完成构造函数的定义,请在注释*1*后添加适当的语句。 (2)定义类的友元函数 fun(),请在
11、注释*2*后添加适当的语句。 (3)定义类的友元函数 main(),请在注释*3*后添加适当的语句。 (4)补充语句,释放内存。请在注释*4*后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#includeiostreamhClass TestClasspublic: *1* data=d; next=NULL; void SetNext(TestClass*p) next=p; private: int data; TestClass*next; *2* *3;TestClass*fun(TestClass*h) TestClass *t,*s,*u,*v;
12、s=h-next; h-next=NULL; while(s!=NULL) for(t=s,v=h;v!=NULLv-data t-data;u=v,v=v-next); s=s 一next; if(v=h) h=t; else u-next=v; t-next=v; return h;Void main() TestClass*h=NULL; TestClass*q=NULL; do int data; cout“please input anumber,and end with 0“endl; cindata; if(data=0)break; TestClass*p = newTestC
13、lass(data); if(h=NULL) h=q=p; else q-SetNext(p); q=p; while(1); h=fun(h); for(;h!=NULL;) q=h-next; couth-data“ “endl; delete h; *4* return;(分数:2.00)_正确答案:(正确答案:(1)添加语句:TestClass(int d) (2)添加语句: friend TestClass*fun(TestClass*); (3)添加语句:friend void main(); (4)添加语句:h=q;)解析:解析:(1)题目 1要求“完成构造函数的定义”。构造函数
14、是一种特殊的成员函数,它的名字必须与类同名,所以这里的构造函数名字是 TestClass。根据构造函数内的“data=d;”语句,知道构造函数TestClass中参数 d为 int型,即这里补全“TestClass(int d)”。 (2)题目 2要求“定义类的友元函数fun()”。在 C+中,如果在本类以外的其它地方定义了一个函数,在类体中是用 friend对该函数进行声明的,此函数称为本类的友元函数,并且能够调用本类中的成员变量。程序中 fun()在类 TestClass以外的地方定义的,为了其实现对类 TestClass中程序变量的调用,在类 TestClass中补全“friend TestClass*fun(TestClass*);”。 (3)题目 3要求“定义类的友元函数 main()。”同(2),这里补全“friend void main();”。 (4)题目 4要求“补充语句,释放内存。”为了防止内存泄漏,在指针使用后要将其释放。第四个标识前面的“delete h;”语句已经将指针 h释放了,所以要释放指针 q,只要将其指向 h就可以了,即“h=q;”。