[计算机类试卷]国家二级C++机试(操作题)模拟试卷321及答案与解析.doc

上传人:ownview251 文档编号:497401 上传时间:2018-11-28 格式:DOC 页数:6 大小:32KB
下载 相关 举报
[计算机类试卷]国家二级C++机试(操作题)模拟试卷321及答案与解析.doc_第1页
第1页 / 共6页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷321及答案与解析.doc_第2页
第2页 / 共6页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷321及答案与解析.doc_第3页
第3页 / 共6页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷321及答案与解析.doc_第4页
第4页 / 共6页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷321及答案与解析.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

1、国家二级 C+机试(操作题)模拟试卷 321及答案与解析 一、基本操作题 1 使用 VC6打开考生文件夹下的源程序文件 modi1 cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为: 1 1 2 1 注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构,错误的语句在 *error*的下面。 #include Class TestClass *error* *error* const int j; public: TestClass() *error* static int i=0; cout #include int convert(char*s

2、troct) int main() cout Class TestClass public: *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!=NULL v-data data; u=v, v= v-next); s=s一 next

3、; 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; coutdata; if(data=0)break; TestClass*p = new TestClass(data); if(h=NULL) h=q=p; else q-SetNext(p); q=p; while(1); h=fun(h); for(; h!=NULL; ) q=h-next; coutdata“ “endl; delete h; *4* return;

4、 国家二级 C+机试(操作题)模拟试卷 321答案与解析 一、基本操作题 1 【正确答案】 (1)添加语句: public: (2)int j; (3)添加语句: j=1; 【试题解析】 (1)打开 modi1 cpp,调试程序,显示 错误提示为“modi1 cpp(22): error C2248: j: cannotaccess private member declared in class TestClass”,第 1个标识下的 j变量由于被定义为私有的,其他函数不可获得,这里应该是 public型,所以在第 1个标识下添加 “punic: ”。 (2)程序中对于 j的调用,说明 j并

5、不是常量,所以修改 i的定义,将其定义为变量,而不是常变量,即修改第 2个标识下语句为 “int i; ”。 (3)题目中要求调用对象,输出 i的值为 1。原程 序中 j变量的使用并没有赋初始化值,输出值为乱码,所以第三个标识下添加 “i=1; ”。 二、简单应用题 2 【正确答案】 int re=0; while(*strOct) re=re*8+(*strOct)-0; strOct+; return re; 【试题解析】 (1)定义整型变量变量 re, re记录求得的 10进制数。 (2)convert(char*strOct)的输入参数为字符指针,而不是整形数据指针,因此需要将 ASC

6、II码转换成相应的数字量,即 (*strOct)-0。 (3)按权展开可采用循环迭代的方法,如: (123)8=(18)+2)8+3,即迭代过程为上一次运算得到的结果再乘以8加现在的元素值,即 re=re*8+(*strOct)-0。 三、综合应用题 3 【正确答案】 (1)添加语句: TestClass(int d) (2)添加语句: friend TestClass*fun(TestClass*); (3)添加语句: friend void main(); (4)添加语句: h=q; 【试题解析】 (1)题目 1要 求 “完成构造函数的定义 ”。构造函数是一种特殊的成员函数,它的名字必须与

7、类同名,所以这里的构造函数名字是 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; ”。

展开阅读全文
相关资源
猜你喜欢
  • ASTM A447 A447M-2011(2016) Standard Specification for Steel Castings Chromium-Nickel-Iron Alloy (25-12 Class) for High-Temperature Service《高温设备用铬镍铁合金 (25-12级) 钢铸件的标准规格》.pdf ASTM A447 A447M-2011(2016) Standard Specification for Steel Castings Chromium-Nickel-Iron Alloy (25-12 Class) for High-Temperature Service《高温设备用铬镍铁合金 (25-12级) 钢铸件的标准规格》.pdf
  • ASTM A449-2007a Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90ksi的热处理钢制六角头螺钉、螺.pdf ASTM A449-2007a Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90ksi的热处理钢制六角头螺钉、螺.pdf
  • ASTM A449-2007b Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90ksi的热处理钢制六角头螺钉、螺.pdf ASTM A449-2007b Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90ksi的热处理钢制六角头螺钉、螺.pdf
  • ASTM A449-2007be1 Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90ksi的热处理钢制六角头螺钉.pdf ASTM A449-2007be1 Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90ksi的热处理钢制六角头螺钉.pdf
  • ASTM A449-2010 Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90 ksi的热处理钢制六角头螺钉 螺.pdf ASTM A449-2010 Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90 ksi的热处理钢制六角头螺钉 螺.pdf
  • ASTM A449-2014 Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90 ksi的热处理钢制六角帽螺钉 螺.pdf ASTM A449-2014 Standard Specification for Hex Cap Screws Bolts and Studs Steel Heat Treated 120 105 90 ksi Minimum Tensile Strength General Use《通用最低拉伸强度为120 105 90 ksi的热处理钢制六角帽螺钉 螺.pdf
  • ASTM A450 A450M-2004a Standard Specification for General Requirements for Carbon Ferritic Alloy and Austenitic Alloy Steel Tubes《碳素 铁素体和奥氏体合金钢管的一般要求的标准规范》.pdf ASTM A450 A450M-2004a Standard Specification for General Requirements for Carbon Ferritic Alloy and Austenitic Alloy Steel Tubes《碳素 铁素体和奥氏体合金钢管的一般要求的标准规范》.pdf
  • ASTM A450 A450M-2007 Standard Specification for General Requirements for Carbon and Low Alloy Steel Tubes.pdf ASTM A450 A450M-2007 Standard Specification for General Requirements for Carbon and Low Alloy Steel Tubes.pdf
  • ASTM A450 A450M-2009 Standard Specification for General Requirements for Carbon and Low Alloy Steel Tubes《通用要求的碳素钢和低合金钢管的标准规范》.pdf ASTM A450 A450M-2009 Standard Specification for General Requirements for Carbon and Low Alloy Steel Tubes《通用要求的碳素钢和低合金钢管的标准规范》.pdf
  • 相关搜索

    当前位置:首页 > 考试资料 > 职业资格

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1