1、二级 C+-71及答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 prog1。其中位于每个“/ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: v1=23;v2=42 注意:只修改每个“/ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream using namespace std; class MyClass int v1; static int v2; public: MyClas
2、s(int v) : vl (v) int getValue() const return v1; static int getValue(int dummy) / ERROR * found* return v1; ; / ERROR * found* int MyClass.v2 = 42; int main() MyClass obj (23); / ERROR * found* int v1 = obj.v1; int v2 = MyClass:getValue(0); cout “v1 = “ v1 “; v2 = “ v2 endl; return 0; (分数:30.00)_二、
3、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 prog2,其中定义了 Stack类和 Entry类。Stack是一个基于链式存储结构的栈,Entry 表示存储在栈中的数据顶。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。 #include iostream using namespace std; class Ent
4、ry public: Entry* next; int data; /* found* Entry (Entry * n, int d): _, data(d) ; class Stack Entry* top; public: Stack() : top(0) Stack() while (top!= 0) Entry* tmp = top; /* found* top =_; delete tmp; void push (int data) /* found* top = new Entry(_, data); int pop ( ) if (top = 0) return0; /* fo
5、und* int result = _; top = top-next; return result; ; int main ( ) int a = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; Stack s; int i = 0; for (i = 0; i 10; i+) cout ai “; s.push(ai); cout endl; for (i = 0; i 10; i+) cout s.pop() ; cout endl; return 0; (分数:30.00)_三、B综合应用题/B(总题数:1,分数:40.00)3.请使用答题菜单命令或直接用 VC6打开考生文
6、件夹下的工程 proj3,其中声明的 IntSet是一个用于表示正整数集合的类。IntSet 的成员函数 IsSubSet的功能是判断集合 B是否是集合 A的子集。在IsSubSet中可以使用成员函数 IsMembeOf睐判断一个正整数是否在此集合中,请编写成员函数IsSubSet。在 main函数中给出了一组测试数据,此时程序的输出应该为: 集合 A:1 2 3 5 8 10 集合B:2 8 集合 B是集合 A的子集 注意:只能在函数 IsSubSet的“/*333*”和“/*666*”之间填入若干语句,不要改动程序中的其他内容。 程序最后将结果输出到文件 out.dat中。输出函数 wri
7、teToFile已经编译为 obj文件,并且在本程序中调用。 /Intset.h #include iostream using namespace std; const int Max=100; class IntSet public: IntSet () /构造一个空集合 end=-1; IntSet (int a, int size) /构造一个包含数组 a中 size个元素的集合 if (size=Max) end=Max-1; else end = size-1; for(int i=0; i=end; i+) elementi=ai; bool IsMemberOf (int a)
8、 /判断 a是否为集合的元素 for(int i=0; i=end; i+) if (elementi = a) return true; return false; int GetEnd() return end; int getElement (int i) return elementi; /返回下标 i处的元素 bool IsSubSet (IntSet /判断集合set是否为当前集合的子集 void Print () /输出集合中的所有元素 for(int i=0; i=end; i+) if(i+1)% 20=0) cout elementi endl; else cout ele
9、menti ; cout endl; private: int elementMax; int end; ; void writeToFile (const char * ); /main.cpp #include “IntSet. h“ bool IntSet:IsSubSet(IntSet int b =2, 8; IntSet set1 (a, 6), set2 (b, 2); cout “集合 A:“; set1.Print (); cout “集合 B:“; set2.Print (); if (set1.IsSubSet (set2) cout “集合 B是集合 A的子集“ end
10、l; else cout “集合 B不是集合 A的子集“ endl; writeToFile (“); return 0; (分数:40.00)_二级 C+-71答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 prog1。其中位于每个“/ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: v1=23;v2=42 注意:只修改每个“/ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream
11、using namespace std; class MyClass int v1; static int v2; public: MyClass(int v) : vl (v) int getValue() const return v1; static int getValue(int dummy) / ERROR * found* return v1; ; / ERROR * found* int MyClass.v2 = 42; int main() MyClass obj (23); / ERROR * found* int v1 = obj.v1; int v2 = MyClass
12、:getValue(0); cout “v1 = “ v1 “; v2 = “ v2 endl; return 0; (分数:30.00)_正确答案:(1)return MyClass:v2; (2)int MyClass:v2=42; (3)int v1=obj.getValue();)解析:考点 本题考查的是 MyClass类,其中涉及静态数据成员、构造函数和成员函数。 解析 (1)主要考查考生对静态成员函数的掌握情况,根据函数定义:static int getValue(int dummy)可知,函数要求返回一个静态整型值。 (2)主要考查考生对静态成员的掌握情况,给静态成员赋值要使用作
13、用域符“:”。 (3)主要考查考生对成员函数的掌握情况,由于 v1是类的私有成员,故不能被 main函数直接调用。二、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 prog2,其中定义了 Stack类和 Entry类。Stack是一个基于链式存储结构的栈,Entry 表示存储在栈中的数据顶。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“
14、/*found*”。 #include iostream using namespace std; class Entry public: Entry* next; int data; /* found* Entry (Entry * n, int d): _, data(d) ; class Stack Entry* top; public: Stack() : top(0) Stack() while (top!= 0) Entry* tmp = top; /* found* top =_; delete tmp; void push (int data) /* found* top =
15、new Entry(_, data); int pop ( ) if (top = 0) return0; /* found* int result = _; top = top-next; return result; ; int main ( ) int a = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; Stack s; int i = 0; for (i = 0; i 10; i+) cout ai “; s.push(ai); cout endl; for (i = 0; i 10; i+) cout s.pop() ; cout endl; return 0; (分
16、数:30.00)_正确答案:(1)next(n) (2)top-next (3)top+ (4)top-data)解析:考点 本题考查的是 Entry类和 Stack类,其中涉及指针、构造函数、析构函数和成员函数。 解析 (1)主要考查考生对构造函数的掌握情况,使用成员列表初始化。 (2)主要考查考生对栈的掌握情况,新元素需要添加到栈顶。 (3)主要考查考生对动态分配的掌握情况,给栈顶添加元素。 (4)主要考查考生对栈的掌握情况,推出栈顶元素。三、B综合应用题/B(总题数:1,分数:40.00)3.请使用答题菜单命令或直接用 VC6打开考生文件夹下的工程 proj3,其中声明的 IntSet是
17、一个用于表示正整数集合的类。IntSet 的成员函数 IsSubSet的功能是判断集合 B是否是集合 A的子集。在IsSubSet中可以使用成员函数 IsMembeOf睐判断一个正整数是否在此集合中,请编写成员函数IsSubSet。在 main函数中给出了一组测试数据,此时程序的输出应该为: 集合 A:1 2 3 5 8 10 集合B:2 8 集合 B是集合 A的子集 注意:只能在函数 IsSubSet的“/*333*”和“/*666*”之间填入若干语句,不要改动程序中的其他内容。 程序最后将结果输出到文件 out.dat中。输出函数 writeToFile已经编译为 obj文件,并且在本程序
18、中调用。 /Intset.h #include iostream using namespace std; const int Max=100; class IntSet public: IntSet () /构造一个空集合 end=-1; IntSet (int a, int size) /构造一个包含数组 a中 size个元素的集合 if (size=Max) end=Max-1; else end = size-1; for(int i=0; i=end; i+) elementi=ai; bool IsMemberOf (int a) /判断 a是否为集合的元素 for(int i=0
19、; i=end; i+) if (elementi = a) return true; return false; int GetEnd() return end; int getElement (int i) return elementi; /返回下标 i处的元素 bool IsSubSet (IntSet /判断集合set是否为当前集合的子集 void Print () /输出集合中的所有元素 for(int i=0; i=end; i+) if(i+1)% 20=0) cout elementi endl; else cout elementi ; cout endl; private
20、: int elementMax; int end; ; void writeToFile (const char * ); /main.cpp #include “IntSet. h“ bool IntSet:IsSubSet(IntSet int b =2, 8; IntSet set1 (a, 6), set2 (b, 2); cout “集合 A:“; set1.Print (); cout “集合 B:“; set2.Print (); if (set1.IsSubSet (set2) cout “集合 B是集合 A的子集“ endl; else cout “集合 B不是集合 A的子
21、集“ endl; writeToFile (“); return 0; (分数:40.00)_正确答案:(if(endset.GetEnd() /如 end小于 set.GetEnd() return false; /返回 false for(int i=0; i=set.GetEnd(); i+) /i 从 0到 set.GetEnd遍历 if (! IsMemberOf(set.GetElement (i) /如果 set.GetElement(i)不在对象集合中 return false; /返回 false return true; /否则返回true)解析:考点 本题考查的是 IntSet类,其中涉及数组、构造函数、bool 函数和成员函数。 解析 主要考查考生对数组的掌握情况,题目要求完成函数 bool IntSet:IsSubSet(IntSet&set)的函数体,该函数用来判断集合 B是否是集合 A的子集。使用 for循环遍历集合 B的每个元素,调用函数 IsMemberOf判断每个元素是否是集合 A中的元素,如果全是则集合 B是集合 A的子集,否则不是集合 A的子集。