【计算机类职业资格】二级C++分类模拟124及答案解析.doc
《【计算机类职业资格】二级C++分类模拟124及答案解析.doc》由会员分享,可在线阅读,更多相关《【计算机类职业资格】二级C++分类模拟124及答案解析.doc(9页珍藏版)》请在麦多课文档分享上搜索。
1、二级 C+分类模拟 124 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1。其中有线段类 Line 的定义。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: End point 1 = (1,8),End point 2 = (5,2),length = 7.2111。 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream #incl
2、ude cmath using namespace std; class Line; double length(Line); class Line /线段类 double x1,y1; /线段端点 1 double x2,y2; /线段端点 2 public: / ERROR *found* Line (double x1, double y1, double x2, double y2) const this - x1 = x1; this - y1 = y1; this - x2 = x2; this - y2 = y2; double getX1() const return x1;
3、double getY1() const return y1; double getX2() const return x2; double getY2() const return y2; void show() const cout “End point 1 = (“ x1 “,“ y1; cout “), End point 2 = (“ x2 “,“ y2; / ERROR *found* cout “), length = “ length (this) “。“ endl; ; double length (Line i) / ERROR *found* return sqrt (1
4、.x1 - 1.x2) * (1.x1 - 1.x2) + (1.y1 - 1.y2) * (1.y1 - 1.y2); int main() Line r1(1.0,8.0,5.0,2.0); r1.show(); return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2。其中有向量基类 VectorBase、向量类Vector 和零向量类 ZeroVector 的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为: (1,2,3,4,5) (0,0,0
5、,0,0,0) 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 #include iostream using namespace std; class VectorBase /向量基类,一个抽象类 int len; public: VectorBase (int fen): len(len) int length() const return len; /向量长度,即向量中元素的个数 virtual double getElement (int i) const = 0; /取第 i 各元素的值 virtual double sum()
6、const = 0; /求所有元素的和 void show() const /显示向量中所有元素 cout “(“; for (int i =0; i length() - 1; i+) cout getElement(i) “,“; / *found* cout _ “)“ endl; /显示最后一个元素 ; class Vector:public VectorBase /向量类 double * val; public: Vector(int fen, double v = NULL):VectorBase(len) val = new doublelen; for(int i = 0;
7、i len; i+) vali = (v = NULL? 0.0:vi); / *found* Vector() _; double getElement(int index) const return valindex; double sum() const double s = 0.0; / *found* for (int i = 0; i length(); i+) return s; ; class ZeroVector: public VectorBase /零向量类 public: ZeroVector(int len):VectorBase(len) / *found* dou
8、ble getElement(int index) const _; double sum() const return 0.0; ; int main() VectorBase * v; double d = 1,2,3,4,5; v = new Vector(5,d); v - show(); delete v; v = new ZeroVector(6); v - show(); delete v; return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中声明了 Sorted
9、List 类,是一个用于表示有序数据表的类。其成员函数 insert 的功能是将一个数据插入到一个有序表中,使得该数据表仍然保持有序。请编写这个 insert 函数。程序的正确输出应为: 插入前: 1,2,4,5,7,8,10 插入 6 和 3 后: 1,2,3,4,5,6,7,8,10 要求: 补充编制的内容写在“/ *333*”与“/ *666*”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。 /SortedList.h #include iostream using na
10、mespace std; class SortedList /有序数据表类 int len; double * d; public: SortedList (int len, double data = NULL); SortedList() delete d; int length() const return len; /有序数据表长度(即元素的个数) double getElement (int i) const return di; void insert (double data); void show() const; /显示有序数据表 ; void writeToFile (ch
11、ar *, const SortedList /main.cpp #include “SortedList.h“ SortedList:SortedList (int len, double data):len(len) d = new doublelen; for(int k = 0; k len; k+) dk = (data = NULL? 0.0:datak); for(int i = 0; i len-1; i+) int m=i; for(int j = i; j len; j +) if(dj dm) m=j; if (m i) double t = dm; dm = di; d
12、i = t; void SortedList:insert (double data) / *333* / *666* void SortedList:show() const /显示有序数据表 for(int i = 0; i len - 1; i+) cout di “,“; cout dlen - 1 endl; int main() double s = 5,8,1,2,10,4,7; SortedList list(7,s); cout “插入前:“ endl; list.show(); list.insert(6.0); list.insert(3.0); cout “插入 6 和
13、 3 后:“ endl; list.show(); writeToFile(“ “, list); return 0; (分数:40.00)_二级 C+分类模拟 124 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1。其中有线段类 Line 的定义。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: End point 1 = (1,8),End point 2 = (5,2),length = 7.2111
14、。 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream #include cmath using namespace std; class Line; double length(Line); class Line /线段类 double x1,y1; /线段端点 1 double x2,y2; /线段端点 2 public: / ERROR *found* Line (double x1, double y1, double x2, double y2) const this - x1 = x1; this - y1
15、= y1; this - x2 = x2; this - y2 = y2; double getX1() const return x1; double getY1() const return y1; double getX2() const return x2; double getY2() const return y2; void show() const cout “End point 1 = (“ x1 “,“ y1; cout “), End point 2 = (“ x2 “,“ y2; / ERROR *found* cout “), length = “ length (t
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
5000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 职业资格 二级 分类 模拟 124 答案 解析 DOC
