1、二级 C+机试-60 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test34_1,此工程包含一个源程序文件 test34_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:David 3123 1000源程序文件 test34_1.cpp 清单如下:#include iostream.h#include string.hclass personpublic:char name20;unsigned long id;float salary;void print()coutn
2、ame id salary/* found */void main( )person p;person *ptr;/* found */ptr=p;/* found */strcpy(“David“,ptr-name);ptr-id=3123;ptr-salary=1000;ptr-print();(分数:30.00)填空项 1:_二、2简单应用题(总题数:1,分数:40.00)2.请编写一个函数 sum(int array,int len),该函数返回数组 array 的所有整数元素的和,其中 len为数组 array 的长度。注意:部分源程序已存在文件 test34_2.cpp 中。请勿修
3、改主函数 main 和其他函数中的任何内容,仅在函数 sum 的花括号中填写若干语句。程序输出结果如下:sum of array 15文件 test34_2.cpp 的内容如下:#include iostream.hint sum(int array,int len)void main()static int a5-1,2,3,4,5;int result=sum(a,5);cout“sum of array “resultend1;(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test34_3。此工程包含一个 test34_3
4、.cpp,其中定义了表示栈的类stack。源程序中 stack 类的定义并不完整,请按要求完成下列操作,将程序补充完整。(1)定义类 stack 的私有数据成员 sp 和 size,它们分别为整型的指针和变量,其中 sP 指向存放栈的数据元素的数组,size 为栈中存放最后一个元素的下标值。请在注释“/*1*”之后添加适当的语句。(2)完成类 stack 的构造函数,该函数首先从动态存储空间分配含有 100 个元素的 int 型数组,并把该数组的首元素地址赋给指针 sp,然后将该数组的所有元素赋值为 0,并将 size 赋值为-1(size 等于-1 表示栈为空)。请在注释“/*2*”之后添加
5、适当的语句。(3)完成类 stack 的成员函数 push 的定义。该函数将传入的整型参数 x 压入栈中,即在 size 小于数组的最大下标情况下, size 自加 1,再给 x 赋值。请在注释“/*3*”之后添加适当的语句。(4)完成类 stack 的成员函数 pop 的定义,该函数返回栈顶元素的值,即在 size 不等于-1 的情况下,返回数组中下标为 size 的元素的值,并将 size 减 1。请在注释“/*4*”之后添加适当的语句。程序输出结果如下:the top elem:1the pop elem:1the stack is empty注意:除在指定位置添加语句之外,请不要改动程
6、序中的其他内容。源程序文件 test34_3.cpp 清单如下:#includeiostream.hclass stack/* 1 *public:stack ( );bool empty()return size=-1;bool full() return size=99;void push(int x);void pop();void top();stack:stack()/* 2 *for(int i=0; i100; i+)*(sp+i)=0;size=-1;void stack:push(int x)/* 3 *cout“the stack is full“end1;elsesize
7、+;*(sp+size) = x;void stack:pop()/* 4 *cout“the stack is empty“end1;elsecout“the pop elem:“*(sp+size)end1;size-;void stack:top()if iempty() )cout“the stack is empty“end1;elsecout“the top elem:“*(sp+size)end1;void main ( )stack s;s.push(1);s.top();s.pop();s.top();(分数:30.00)_二级 C+机试-60 答案解析(总分:100.00,
8、做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test34_1,此工程包含一个源程序文件 test34_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:David 3123 1000源程序文件 test34_1.cpp 清单如下:#include iostream.h#include string.hclass personpublic:char name20;unsigned long id;float salary;void print()coutname id salary/* found */voi
9、d main( )person p;person *ptr;/* found */ptr=p;/* found */strcpy(“David“,ptr-name);ptr-id=3123;ptr-salary=1000;ptr-print();(分数:30.00)填空项 1:_ (正确答案:(1)错误:正确:;(2)错误:ptr=p;正确:ptr=int result=sum(a,5);cout“sum of array “resultend1;(分数:40.00)_正确答案:(int sum(int array,int len)int sum=0;for (int i=0;ilen;i+)
10、sum=sum+arrayi;return sum;)解析:解析 本题主要考查考生对于数组访问和 for 循环语句的掌握。应该注意的是,长度为 len 的数组 array 的下标范围为 0 到 len-1,而不是从 1 到 len,所以循环控制变量的初始值为 0。三、3综合应用题(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test34_3。此工程包含一个 test34_3.cpp,其中定义了表示栈的类stack。源程序中 stack 类的定义并不完整,请按要求完成下列操作,将程序补充完整。(1)定义类 stack 的私有数据成员 sp 和 size,它们分别为整型
11、的指针和变量,其中 sP 指向存放栈的数据元素的数组,size 为栈中存放最后一个元素的下标值。请在注释“/*1*”之后添加适当的语句。(2)完成类 stack 的构造函数,该函数首先从动态存储空间分配含有 100 个元素的 int 型数组,并把该数组的首元素地址赋给指针 sp,然后将该数组的所有元素赋值为 0,并将 size 赋值为-1(size 等于-1 表示栈为空)。请在注释“/*2*”之后添加适当的语句。(3)完成类 stack 的成员函数 push 的定义。该函数将传入的整型参数 x 压入栈中,即在 size 小于数组的最大下标情况下, size 自加 1,再给 x 赋值。请在注释“
12、/*3*”之后添加适当的语句。(4)完成类 stack 的成员函数 pop 的定义,该函数返回栈顶元素的值,即在 size 不等于-1 的情况下,返回数组中下标为 size 的元素的值,并将 size 减 1。请在注释“/*4*”之后添加适当的语句。程序输出结果如下:the top elem:1the pop elem:1the stack is empty注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。源程序文件 test34_3.cpp 清单如下:#includeiostream.hclass stack/* 1 *public:stack ( );bool empty()re
13、turn size=-1;bool full() return size=99;void push(int x);void pop();void top();stack:stack()/* 2 *for(int i=0; i100; i+)*(sp+i)=0;size=-1;void stack:push(int x)/* 3 *cout“the stack is full“end1;elsesize+;*(sp+size) = x;void stack:pop()/* 4 *cout“the stack is empty“end1;elsecout“the pop elem:“*(sp+si
14、ze)end1;size-;void stack:top()if iempty() )cout“the stack is empty“end1;elsecout“the top elem:“*(sp+size)end1;void main ( )stack s;s.push(1);s.top();s.pop();s.top();(分数:30.00)_正确答案:(1)int *sp;int size;(2)spnew int100;(3)if(full()(4)if(empty()解析:解析 本题主要考查的是考生利用类、数组、指针和基本控制结构等知识,建立经典数据结构的能力。栈在数据结构中是一应用范围很广的类,在这里实现的只是最核心的部分。在该题中特别注意使用new 进行动态空间申请及指针在数组访问中的应用。