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

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

1、国家二级( C+)机试模拟试卷 56及答案与解析 一、程序改错题( 30分) 1 使用 VC6打开考生文件夹下的工程 test42_1,此工程包含一个源程序文件test42_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为: rect area: 12 rectb area: 30 源程序文件 test42_1.cpp清单如下: #include iostream.h class CRectangle /* found */ int *width, height; public: CRectangle (int,int); CRectangle (); int area

2、 (void) return (*width * *height); ; CRectangle:CRectangle (int a, int b) width = new int; height = new int; /* found */ width = a; *height = b; CRectangle: CRectangle () delete width; delete height; /* found */ void main () CRectangle rect (3,4), rectb (5,6); cout “rect area: “ rect.area() endl; co

3、ut “rectb area: “ rectb.area() endl; return 0; 二、简单应用题( 40分) 2 请编写一个函数 long Fibo(int n), 该函数返回 n的 Fibonacci数。规则如下: n等于 1或者 2时, Fibonacci数为 1,之后每个 Fibonacci数均为止前两个数之和, 即: F(n)=F(n-1)+F(n-2) 注意:清使用递归算法实现该函数。 部分源程序已存在文件 test1_2.cpp中。 请勿修改主函数 main和其他函数中的任何内容,仅在函数 Fibo的花括号中填写若干 语句。如 n=8时,结果是 21。 文件 test1

4、_2.cpp清单如下 : #include iostream.h corlsh int N=8; long Fibo(int n); void main() long f=Fibo(N); couk f endl; long Fibo(int n) 三、综合应用题( 30分) 3 使用 VC6打开考生文件夹下的工程 test41_3。此工程包含一个 test41_3.cpp,其中定义了类 Rectangle,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)定义类 Rectangle的私有数据成员 left, top和 fight, bottom,它们都是 int型的数据。请

5、在注释 “/*1*”之后添加适当的语句。 (2)添加类 Rectangle的带四个 int型参数 1、 t、 r、 b的构造函数的声明,并使这四个参数的默认值均为 0,请在注释 “/*2*”之后 添加适当的语句。 (3)添加类 Rectangle的成员函数 SetTop()参数为 int型的 t,作用为把 t的值赋给类的数据成员 top,添加类 Rectangle的成员函数 SetBottom()参数为 int型的 t,作用为把 t的值赋给类的数据成员 bottom,请在注释 “/*3*”之后添加适当的语句。 (4)完成派生类 Rectangle的成员函数 Show()的定义,使其以格式 “r

6、ight-bottom point is(right, bottom)”输出,请在注释 “/*4*”之后添加适当的语句。 源程序文件 test41_3.cpp清单如下: #include iostream.h class Rectangle / * 1 * int right, bottom; public: / * 2 * Rectangle(); void Assign(int 1, int t, int r, int b); void SetLeft(int t)left = t; void SetRight(int t)right = t; / * 3 * void SetBottom

7、(int t)bottom = t; void Show(); ; Rectangle:Rectangle(int 1, int t, int r, int b) left = 1; top = t; right = r; bottom = b; void Rectangle:Assign(int 1, int t, int r, int b) left = 1; top = t; right = r; bottom = b; void Rectangle:Show() cout “left-top point is (“ left “,“ top “)“ n; / * 4 * void ma

8、in() Rectangle rect; rect.Show(); rect.Assign(100,200,300,400); rect.Show(); 国家二级( C+)机试模拟试卷 56答案与解析 一、程序改错题( 30分) 1 【 正确答案】 (1) 错误: int*width, height; 正确: int*width, *height; (2) 错误: width=a; 正确: *width=a; (3) 错误: void main() 正确; int main() 【试题解析】 (1)根据后面类中构造函数的定义可以看出只有定义成指针的变量才能动态申请空间,所以本题的错误在于没有把

9、变量 height定义成指针类型; (2)变量 width是指针类型的变量,直接给它赋值相当于让它指向一个新的地址,而要改变它指向的变量的值,应 该使用取内容符号 “*”: (3)根据主函数最后的返回值情况,可知该主函数是需要定义成带返回值的函数,本题的错误在于,把主函数定义成了 void,改成 int即可,考生一定要注意函数定义的返回值和最后实际的返回情况是否一一对应。 二、简单应用题( 40分) 2 【正确答案】 long Fibo(int n) if(n=1)return 1L; else if(n=2) return 1L; else return Fibo(n-1)+Fibo(n-2

10、); 【试题解析】 本题考查的是考生对于递归函数的熟练掌握。递归是指在调用函数的过程中出现调用该函数自身,这里递归的结束条件是 n等于 1或 2,即已知数列前两项为 1。其调用过程如下:如果函数的参数为 l或者 2就把返回值 1返回调用函数;否则,就使用递推公式 Fibo(n)=Fibo(n-1)+Fibo(n-2),把 n-1和 n-2作为参数调用原函数,即这是一个递归求值的过程 (递推的过程 )。 三、综合应用题( 30分) 3 【正确答案】 (1) int left, top; (2) Rectangle(int 1=0, int t=0, int r=0, int b=0); (3) void SetTop(int t)top=t; (4) cout “right-bottom point is (“ right “,“ bottom “)“ n; 【试题解析】 主要考查考生对于类的定义和定义一般成员函数的掌握,其中 (2)中为了使构造函数可以不带参数,使用了对于参数给定默认值的方法,这点需要考生注意, (4)中连续 的字符流的输出可以连续使用 “ ”符号实现。

展开阅读全文
相关资源
猜你喜欢
  • ASTM A672 A672M-2009e1 Standard Specification for Electric-Fusion-Welded Steel Pipe for High-Pressure Service at Moderate Temperatures《中等温度高压条件下电融焊钢管的标准规格》.pdf ASTM A672 A672M-2009e1 Standard Specification for Electric-Fusion-Welded Steel Pipe for High-Pressure Service at Moderate Temperatures《中等温度高压条件下电融焊钢管的标准规格》.pdf
  • ASTM A672 A672M-2014 Standard Specification for Electric-Fusion-Welded Steel Pipe for High-Pressure Service at Moderate Temperatures《中等温度高压条件下电融焊钢管的标准规格》.pdf ASTM A672 A672M-2014 Standard Specification for Electric-Fusion-Welded Steel Pipe for High-Pressure Service at Moderate Temperatures《中等温度高压条件下电融焊钢管的标准规格》.pdf
  • ASTM A672-2006 Standard Specification for Electric-Fusion-Welded Steel Pipe for High-Pressure Service at Moderate Temperatures《中温高压用电熔焊钢管的标准规范》.pdf ASTM A672-2006 Standard Specification for Electric-Fusion-Welded Steel Pipe for High-Pressure Service at Moderate Temperatures《中温高压用电熔焊钢管的标准规范》.pdf
  • ASTM A672-2008 Standard Specification for Electric-Fusion-Welded Steel Pipe for High-Pressure Service at Moderate Temperatures.pdf ASTM A672-2008 Standard Specification for Electric-Fusion-Welded Steel Pipe for High-Pressure Service at Moderate Temperatures.pdf
  • ASTM A673 A673M-2007 Standard Specification for Sampling Procedure for Impact Testing of Structural Steel《结构钢冲击试验的取样程序的标准规范》.pdf ASTM A673 A673M-2007 Standard Specification for Sampling Procedure for Impact Testing of Structural Steel《结构钢冲击试验的取样程序的标准规范》.pdf
  • ASTM A673 A673M-2007(2012) Standard Specification for Sampling Procedure for Impact Testing of Structural Steel 《结构钢冲击试验的取样程序标准规格》.pdf ASTM A673 A673M-2007(2012) Standard Specification for Sampling Procedure for Impact Testing of Structural Steel 《结构钢冲击试验的取样程序标准规格》.pdf
  • ASTM A673 A673M-2017 Standard Specification for Sampling Procedure for Impact Testing of Structural Steel《结构钢冲击试验取样程序的标准规格》.pdf ASTM A673 A673M-2017 Standard Specification for Sampling Procedure for Impact Testing of Structural Steel《结构钢冲击试验取样程序的标准规格》.pdf
  • ASTM A674-2005 Standard Practice for Polyethylene Encasement for Ductile Iron Pipe for Water or Other Liquids《水或其他液体用球墨铸铁管的聚乙烯包装物的标准实施规程》.pdf ASTM A674-2005 Standard Practice for Polyethylene Encasement for Ductile Iron Pipe for Water or Other Liquids《水或其他液体用球墨铸铁管的聚乙烯包装物的标准实施规程》.pdf
  • ASTM A674-2010 Standard Practice for Polyethylene Encasement for Ductile Iron Pipe for Water or Other Liquids《水或其他液体用球墨铸铁管的聚乙烯包装物的标准实施规程》.pdf ASTM A674-2010 Standard Practice for Polyethylene Encasement for Ductile Iron Pipe for Water or Other Liquids《水或其他液体用球墨铸铁管的聚乙烯包装物的标准实施规程》.pdf
  • 相关搜索

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

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