1、二级 C+分类模拟 131 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,其中有点类 Point 和线段类 Line 和主函数 main 的定义,程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出应为: p1=(8,4)p2=(3,5) 注意:只修改两个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream #include cmath using namespa
2、ce std; class Point double x, y; public: Point (double x = 0.0, double y = 0.0) / ERROR *found* x=x; y=y; double getX() const return x; double getY() const return y; / ERROR *found* void show() const cout “x“,“y“)“ ; class Line Point p1,p2; public: Line (Point pt1, Point pt2) / ERROR *found* pt1 = p
3、1; pt2 = p2; Point getP1() const return p1; Point getP2() const return p2; ; int main() Line line (Point(8, 4), Point(3,5); cout “p1 =“; line.getP1(). show(); cout “p2 =“; line.getP2(). show(); cout endl; return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中有整数栈类 IntL
4、ist、顺序栈类SeqList 和链接栈类 LinkList 的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 4 6 3 1 8 4 6 3 1 8 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动/“*found*”。 #include iostream using namespace std; class IntStack /整数栈类 public: virtual void push(int) = 0; /入栈 virtual int pop() = 0; /出栈并返回出栈元素 virtual int topE
5、lement() const = 0; /返回栈顶元素,但不出栈 virtual bool isEmpty() const=0; /判断是否栈空 ; class SeqStack: public IntStackint data100; /存放栈元素的数组 int top; /栈顶元素的小标 public: / *found* SeqStack():_ /把 top 初始化为-1 表示栈空 void push (int n) data+top = n; / *found* int pop() return_; int topElement() const return datatop; boo
6、l isEmpty() const return top = -1; ; struct Node int data; Node * next; ; class LinkStack: public IntStack Node * top; public: / *found* LinkStack():_ /把 top 初始化为 NULL 表示栈空 void push (int n) Node * p = new Node; p - data = n; / *found* _ top = p; int pop() int d=top-data; top=top-next; return d; int
7、 topElement() const return top - data; bool isEmpty() const return top = NULL; ; void pushData(IntStack st.push(1); st.push(3); st.push(6); st.push(4); void popData(IntStack int main() SeqStack st1; pushData(st1); popData(st1); cout endl; LinkStack st2; pushData(st2); popData(st2); cout endl; return
8、 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中声明 IntSet 是一个用于表示正整数集合的类。IntSet 的成员函数 Intersection 的功能是求当前集合与另一个集合的交集。请完成成员函数Intersection。在 main 函数中给出了一组测试数据,此时程序的输出应该是: 求交集前: 1 2 3 5 8 10 2 8 9 11 30 56 67 求交集后: 1 2 3 5 8 10 2 8 9 11 30 56 67 2 8 要求: 补充编制的内容写在“/ *333*
9、”与“/ *666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 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 en
10、d = size - 1; for (int i = 0; i = end; i +) elementi = ai; bool IsMemberOf (int a) /判断 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 的元素 IntSet Intersection (IntSet /求当前集
11、合与集合 set 的交 void Print () /输出集合中的所有元素 for(int i=0;i=end;i+) if(i+1)% 20=0) cout elementi endl; else cout elementi “; cout endl; private: int elementMax; int end; ; void writeToFile (const char *); /main.cpp #include “IntSet.h“ IntSet IntSet:Intersection(IntSet / *333* / *666* return IntSet(a,size);
12、int main() int a = 1,2,3,5,8,10; int b = 2,8,9,11,30,56,67; IntSet set1 (a, 6), set2 (b, 7), set3; cout “求交集前:“ endl; set1.Print(); set2.Print(); set3.Print(); set3 = set1.Intersection (set2); cout endl “求交集后:“ endl; set1.Print(); set2.Print(); set3.Print(); writeToFile (“ “); return 0; (分数:40.00)_二
13、级 C+分类模拟 131 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,其中有点类 Point 和线段类 Line 和主函数 main 的定义,程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出应为: p1=(8,4)p2=(3,5) 注意:只修改两个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream #include cmath using namespace
14、 std; class Point double x, y; public: Point (double x = 0.0, double y = 0.0) / ERROR *found* x=x; y=y; double getX() const return x; double getY() const return y; / ERROR *found* void show() const cout “x“,“y“)“ ; class Line Point p1,p2; public: Line (Point pt1, Point pt2) / ERROR *found* pt1 = p1;
15、 pt2 = p2; Point getP1() const return p1; Point getP2() const return p2; ; int main() Line line (Point(8, 4), Point(3,5); cout “p1 =“; line.getP1(). show(); cout “p2 =“; line.getP2(). show(); cout endl; return 0; (分数:30.00)_正确答案:()解析:(1):x(x),y(y) 或this-x=x,this-y=y; (2)void show() const cout“(“x“,“
16、y“)“; (3):p1(pt1),p2(pt2) 或p1=pt1;p2=pt2 答案考生文件夹 考点 本题考查的是 Point 类和 Line 类,其中涉及构造函数、const 函数和成员函数。构造函数一般使用成员列表初始化,语句最后有个“;”作为结束符。 解析 (1)主要考查考生对构造函数的掌握,因为形参名和私有成员名称一样,因此不能直接赋值,在这里使用成员列表初始化,也可以使用 this 指针赋值。 (2)主要考查考生对语句基本语法的掌握,根据语句:void show() const cout“(“x“,“y“)“。可看出函数体内并没有“;”作为 cout 语句的结束符,因此程序错误。
17、(3)主要考查考生对构造函数的掌握,形参是 pt1 和 pt2,这里写反了,也可以使用成员列表初始化法,可以避免这种错误。二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中有整数栈类 IntList、顺序栈类SeqList 和链接栈类 LinkList 的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 4 6 3 1 8 4 6 3 1 8 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动/“*found*”。 #include iostre
18、am using namespace std; class IntStack /整数栈类 public: virtual void push(int) = 0; /入栈 virtual int pop() = 0; /出栈并返回出栈元素 virtual int topElement() const = 0; /返回栈顶元素,但不出栈 virtual bool isEmpty() const=0; /判断是否栈空 ; class SeqStack: public IntStackint data100; /存放栈元素的数组 int top; /栈顶元素的小标 public: / *found*
19、SeqStack():_ /把 top 初始化为-1 表示栈空 void push (int n) data+top = n; / *found* int pop() return_; int topElement() const return datatop; bool isEmpty() const return top = -1; ; struct Node int data; Node * next; ; class LinkStack: public IntStack Node * top; public: / *found* LinkStack():_ /把 top 初始化为 NU
20、LL 表示栈空 void push (int n) Node * p = new Node; p - data = n; / *found* _ top = p; int pop() int d=top-data; top=top-next; return d; int topElement() const return top - data; bool isEmpty() const return top = NULL; ; void pushData(IntStack st.push(1); st.push(3); st.push(6); st.push(4); void popData(
21、IntStack int main() SeqStack st1; pushData(st1); popData(st1); cout endl; LinkStack st2; pushData(st2); popData(st2); cout endl; return 0; (分数:30.00)_正确答案:()解析:(1)top(-1) (2)datatop- (3)top(NULL) (4)p-next=top 答案考生文件夹 考点 本题考查的是 IntStack 类及其派生类 SeqStack 类和 LinkStack 类,其中涉及纯虚函数、数组、构造函数和动态数组。本题对栈的知识要求很
22、高,栈的特点是先进后出,后进先出。 解析 (1)主要考查考生对构造函数的掌握情况,先看语句注释:把 top 初始化为-1 表示栈空,即要把top 赋值为-1 即可。 (2)主要考查考生对纯虚函数的掌握情况,先看纯虚函数在基类的注释:出栈并返回出栈元素。要返回栈顶元素可以通过 datatop得到,出栈同时要使得 top 往下移动,即 top-。 (3)主要考查考生对构造函数的掌握情况,先看语句注释:把 top 初始化为 NULL 表示栈空,因此使用成员列表初始化直接把 top 赋值为 NULL 即可。 (4)主要考查考生对栈的掌握,push 为入栈函数,top 指向栈顶元素,因此新添加的指针的
23、next 要指向top,即 p-next=top;。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中声明 IntSet 是一个用于表示正整数集合的类。IntSet 的成员函数 Intersection 的功能是求当前集合与另一个集合的交集。请完成成员函数Intersection。在 main 函数中给出了一组测试数据,此时程序的输出应该是: 求交集前: 1 2 3 5 8 10 2 8 9 11 30 56 67 求交集后: 1 2 3 5 8 10 2 8 9 11 30 56 67 2 8 要求: 补充编制的内
24、容写在“/ *333*”与“/ *666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 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 -
25、 1; else end = size - 1; for (int i = 0; i = end; i +) elementi = ai; bool IsMemberOf (int a) /判断 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 的元素 IntSet Intersection (I
26、ntSet /求当前集合与集合 set 的交 void Print () /输出集合中的所有元素 for(int i=0;i=end;i+) if(i+1)% 20=0) cout elementi endl; else cout elementi “; cout endl; private: int elementMax; int end; ; void writeToFile (const char *); /main.cpp #include “IntSet.h“ IntSet IntSet:Intersection(IntSet / *333* / *666* return IntSe
27、t(a,size); int main() int a = 1,2,3,5,8,10; int b = 2,8,9,11,30,56,67; IntSet set1 (a, 6), set2 (b, 7), set3; cout “求交集前:“ endl; set1.Print(); set2.Print(); set3.Print(); set3 = set1.Intersection (set2); cout endl “求交集后:“ endl; set1.Print(); set2.Print(); set3.Print(); writeToFile (“ “); return 0; (
28、分数:40.00)_正确答案:()解析:for (int i = 0; i = set.GetEnd(); i+) /遍对象 set 数组 if (IsMemberOf (set.GetElement (i) /判断对象 Set 数组第 i 个值是不是集合中的值,如果是则把它插入到 a 中 a size + = set.GetElement(i); 答案考生文件夹 考点 本题考查的是 IntSet 类,其中涉及构造函数、bool 函数和成员函数。本类是一个用于表示正整数集合的类,题目要求填写的函数能实现交集的功能,也就是将两个数组内的元素进行比较,将一样的元素提取出来。 解析 主要考查考生对数组的掌握,根据 IntSet 类的构造函数: 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; 可知数组 element 用来装载集合,end 表示数组长度,因此调用函数 IsMemberOf 来判断 set 中的元素是否存在于集合中,如果存在则放入数组 a 中。 主要考查考生对数组的掌握,集合可以用数组来实现,交集就是将两个数组中相等的元素提取出来放入一个新建立的数组。