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

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

1、国家二级 C+机试(操作题)模拟试卷 296及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 proj1下的工程 proj1,其中有点类 Point和线段类 Line和主函数 main的定义,程序中位于每个 “ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出应为: p1 (8, 4)p2 (3, 5) 注意:只修改两个 “ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream #include cmath using namespace std; class Point doubl

2、e x, y; public: Point(double x 0 0, double y 0 0) ERROR *found* x x; y y; ) double getX()constreturn x; double getY()constreturn y; ERROR *found* void show()constcout ( x , y ) ; class Line Point p1, p2; public: Line(Point pt1, Point pt2) ERROR *found* pt1 p1; pt2 p2; Point getP1()constreturnp1; Poi

3、nt getP2()constreturnp2; ; int main() Line line(Point(8, 4), Point(3, 5); cout “p1 “; line getPl() show(); cout “p2 “; line getP2() show(); cout end1; return 0; 二、简单应用题 2 请使用 VC6或使用【答题】菜单打开考生文件夹 proj2下的工程 proj2,其中有整数栈类 IntList、顺序栈类 SeqList和链接栈类 LinkList的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为

4、: 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 topElement() const 0; 返回栈顶元素,但不出栈 virtual bool isEmpty()const 0; 判断是否栈空 ; class SeqStack:

5、 public IntStack int data100;存放栈元素的数组 int top; 栈顶元素的下标 public: *found* SeqStack(): _ 把 top初始化为 1表示栈空 void push(int n)data top n; *found* int pop()return_; int topElement()constreturn datatop; bool isEmpty()constreturn top 1; ; struct Node int data; NOde * next; ; class LinkStack: public IntStack Nod

6、e * 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 topElement()const return top data; bool isEmpty()constreturn top NULL; ; void pushData(IntStack&st) st push(8); st push(1);

7、st push(3); st push(6); st push(4); void popData(IntStack&st) while(!st isEmpty() cout st pop() “; int main() SeqStack st1; pushData(st1); popData(st1); cout end1; LinkStack st2; pushData(st2); popData(st2); cout end1; return 0; 三、综合应用题 3 请使用 VC6或使用【答题】菜单打开考生文件夹 proj3下的工程 proj3,其中声明 IntSet是一个用于表示正整数

8、集合的类。 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*”与 “*666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out dat中。输出函数 writeToFile已经编译为 obj文件,并且在本程序中调用。 Intset h #

9、include iostream using namespace std; const int Max 1 00; 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; i end; i ) if(element

10、i a) return true; return false; int GetEnd()return end; ) 返回最后一个元素的下标 int GetElement(int i)return elementi; ) 返回下标为 i的元素 IntSet Intersection(IntSet&set); 求当前集合与集合 set的交 void Print() 输出集合中的所有元 素 for(int i 0; i end; i ) if(i 1) 20 0) cout elementi end1; else cout elementi ; cout end1; private: int ele

11、mentMax; int end; ; void writeTOFile(const char * ); main cpp #include“ntSet h“ IntSet IntSet Intersection (IntSet set) int aMax, size 0; *333* *666* return IntSet(a, size); int main() int a 1, 2, 3, 5, 8, 10); int b 2, 8, 9, 11, 30, 56, 67); IntSet setl (a, 6), Set2 (b, 7), set3; cout “求交 集前: “ end

12、1; set1 Print(); set2 Print(); set3 Print(); set3 set1 Intersection(set2); cout end1 “求交集后: “ end1; set1 Print(); set2 Print(); set3 Print(); writeToFile(“); return 0; 国家二级 C+机试(操作题)模拟试卷 296答案与解析 一、基本操作题 1 【正确答案】 (1): x(x), y(y)或 this x x, this y y; (2)void show()const eout ( x , y ); (3): p1(pt1),

13、p2(pt2)或 p1 pt1; p2 pt2 【试题解析】 (1)主要考查考生对构造函数的掌握,因为形参名和私有成员名称一样,因此不能直接赋值,在这里使用成员列表初始化,也可以使用 this指针赋值。 (2)主要考查考生对语句基本语法的掌握,根据语句: void show()constcout( x , y )。可看出函数体内并没有 “; ”作为 cout语句的结束符,因此程序错误。 (3)主要考查考生对构造函数的掌握,形参是 pt1和 pt2,这里写反了,也可以使用成员列表初始化法,可以避免这种错误。 二、简单应用题 2 【正确答案】 (1)top( 1) (2)datatop (3)to

14、p(NULL) (4)p next top 【试题解析】 (1)主要考查考生对构造函数的掌握情况,先看语句注释:把 top初始化为 1表示栈空,即要把 top赋值为 1即可。 (2)主要考查考生对纯虚函数的掌握情况,先看纯虚函数在基类的注释:出栈并返回出栈元素。要返回栈顶元素可以通过 datatop得到,出栈同时要使得 top往下移动,即 top。 (3)主要考查考生对构造函数的掌握情况,先看语句注释:把 top初始化为NULL表示栈空,因此使用成员列表初始化直接把 top赋值为 NULL即可。 (4)主要考查考生对栈的掌握, push为人栈函数, top指向栈顶元素,因此新添加的指针的 ne

15、xt要指向 top,即 p next top;。 三、综合应用题 3 【正确答案】 for(int i 0; i set GetEnd(); i ) 遍对象 set数组 if(IsMemberOf(set GetElement(i) 判断对象 set数组第 i个值是不是集合中的值,如果是则把它插入到 a中 asize set GetElement(i); 【试题解析】 主要考查考生对数组的掌握,根据 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中。

展开阅读全文
相关资源
猜你喜欢
相关搜索

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

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