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

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

1、国家二级 C+机试(操作题)模拟试卷 293及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 proj1下的工程 proj1,该工程中包含程序文件 main epp,其中有关 TVSet(“电视机 ”)和主函数 main的定义。程序中位于每个 “ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: 规格: 29英寸,电源:开,频道: 5,音量: 18 规格: 29英寸,电源:关,频道: 1,音量: 1 注意 :只修改每个 “ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostre

2、am using namespace std; class TVSet “电视机 ”类 const int size; int channel; 频道 int volume; 音量 bool on; 电源开关: true表示开, false表示关 public: ERROR *found* TVSet(int size) thiS Size(size); channel 0; volume 15; on false; int getsize()constreturnsize; 返回电视机规格 bool isOn()constreturnon; ) 返回电源开关状态 返回当前音量,关机情况下返回

3、 1 int getVolume()const returnisOn()?volume: 1; 返回当前频道,关机情况下返回 1 int getChannel()constreturn isOn()?channel: 1; ) ERROR *found* void turnOnOff()const 将电源在 “开 ”和 “关 ”之间转换 on !on; ) void setChannelTo(int chan) 设置频道 (关机情况下无效 ) if(isOn() chan 0 chan 99) channel chan; void setVolumeTo (int vol) 设置音量 (关机情

4、况下无效 ) if(isOn() vol 0 vol 30) volume vol, void show state() ERROR *found* cout “规格: “ getsize() “英寸 “ “,电源: “ (isOn()?“开 “: “关 “) “,频道: “ getChannel() “,音量: “ getVolume() end1; ; int main() TVSet tv(29); tv turnOnOff(); tv setChanneiTo(5); tv setVolumeTo(18); tv show state(); tv turnOnOff(); tv sho

5、w State(); return 0; 二、简单应用题 2 请使用 VC6或使用【答题】菜单打开考生文件夹 proj2下的工程 proj2。该工程中包含一个程序文件 main cpp,其中有类 Quadritic、类 Root及主函数 main的定义。一个 Quadritic对象表示一个 a2 b c的一元二次多项式。一个 Root对象用于表示方程 a2 b c 0的一组根,它的数据成员 hum_of_roots有 3种可能的值,即 0、 1和 2,分别表示根的 3种情况:无实根、有两个相同的实根和有两个不同的实根。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输

6、出结果应为 (注:输出中的 X表示 2): 3X 2 4X 50 0 无实根 4 5X 2 6X 2 0 0 有两个相同的实根: 0 666667和 0 666667 1 5X 2 2X 3 0 0 有两个不同的实根: 0 896805和 2 23014 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动 “*found*”。 #1nclude lostream #include iomanip #include cmath using namespace std; class Root 一元二次方程的根 public: const double x1; 第一个根 c

7、onst double x2; 第二个根 const int num_of_roots;不同根的数量: 0、 1或 2 创建一个 “无实根 ”的 Root对象 Root(): x1(0 0),x2(0 0), num_of_roots(0) 创建一个 “有两个相同的实根 ”的 Root对象 Root(double root) *found* : _ 创建一个 “有两个不同的实根 ”的 Root对象 Root(double root1, doubleroot2): x1(root1),x2(root2), num of roots(2) void show()const 显示根的信息 cout

8、“ tt“; switch(num_of_roots) case 0: *found* case 1: cout “有两个相同的实根: “ x1 “和 “ x2; break; default: cout “有两个不同的实根: “ x1 “和 “ x2; break; ; class Quadratic 二次多项式 public: const double a, b, c;分别表示二次项、一次项和常数项等 3个系数 Quadratic(double a, double b, double c) 构造函数 *found* : _ Quadratic(Quadratic&x) 复制构造函数 : a

9、(x a), b(x b), c(x c) Quadratic add(Quadratic x) const 求两个多项式的和 return Quadratic(a x a, b x b, c x c); Quadratic sub (Quadratic x)const 求两个多项式的差 *found* double value(double X)const 求二次多项式的值 return a*x*x b*x c; Root root()const求一元二次方程的根 double delta b*b 4*a*c;计算判别式 if(delta 0 0)return Root(); if(delt

10、a 0 0) return Root( b (2*a); double sq sqrt(delta); return Root( b sq) (2*a), ( b sq) (2*a); void show()const 显示多项式 cout end1 a “X 2“ showpos b “X“ C noshowpos; void showFunction() 显示一元二次方程 show(); cout “ 0 0“; ; int main() Quadratic q1(3 0, 4 0, 5 0),q2(4 5, 6 0, 2 0), q3(q2 sub(q1); q1 showFunctio

11、n(); q1 root() show(); q2 showFunction(); q2 root() show(); q3 showFunction(); q3 root() show(); cout end1; return 0; 三、综合应用题 3 请使用 VC6或使用【答题】菜单打开考生文件夹 proj3下的工程 proj3,其中包含了类 Integers和主函数 main的定义。一个 Integers对象就是一个整数的集合,其中包含 0个或多个可重复的整数。成员函数 add的作用是将一个元素添加到集合中,成员函数 remove的作用是从集合中删除指定的元素 (如果集合中存在该元素 )

12、,成员函数 sort的作用是将集合中的整数按升序进行排序。请编写这个 sort函数。此程序的正确输出结果应为: 5 28 2 4 5 3 2 75 27 66 31 5 28 2 4 5 3 2 75 27 66 31 6 5 28 2 4 5 3 2 75 27 66 31 6 19 5 28 4 5 32 75 27 66 31 6 19 5 28 4 5 3 2 75 27 66 31 6 19 4 2 3 4 4 5 5 6 19 27 28 31 66 75 要求: 补充编制的内容写在 “*333*”与 “ *666*”之间。不得修改程序的其他部分。 注意:相关文件包括: main

13、cpp、 Integers h。 程序最后调用 writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件 out dat中。输出函数 writeToFile已经编译为 obj文件。 Integers h i fndef INTEGERS #de fine INTEGERS #include iostream using namespace std; const int MAXELEMENTS 100; 集合最多可拥有的元素个数 class Integers int elemMAXELEMENTS; 用于存放集合元素的数组 int counter; 用于记录集合中元素个数

14、的计数器 public: Integers(): counter(0) 创建一个空集合 Integers(int data, intsize); 利用数组提供的数据创建一个整数集合 void add(int element); 添加一个元素到集合中 void remove(int element); 删除集合中指定的元素 int getCount()constreturn counter; ) 返回集合中元素的个数 int getElement(int i)constreturn elemi; ) 返回集合中指定的元素 void sort(); 将集合中的整数按由小到大的次序进行排序 void

15、 show()const; 显示集合中的全部元素 ; void writeToFile(const char * path); #endif main cpp #include“Integers h“ #include iomanip Integers Integers(int data , int size): counter(o) for(int i 0; i size; i ) add(datai); void Integers add (int element) if(counter MAXELEMENTS) elemcounter element; void Integers rem

16、ove(int element) int j; for(j counter 1; j 0; j ) if(elemj element) break; for(int i j; i counter 1; i ) elemi elemi 1; counter; void Integers sort() *333* *666* void Integers show()const for(int i 0; i getCount(); i ) cout; setw(4) getElemerit; (i); cout; end1; int main() int d 5, 28, 2, 4, 5, 3, 2

17、, 75, 27, 66, 31); Integers s(d, 11); s show(); s add(6); s show(); s add(19); s show(); s remove(2); s show(); s add(4); s show(); s sort(); s show(); writeToFile(“); return 0; 国家二级 C+机试(操作题)模拟试卷 293答案与解析 一、基本操作题 1 【正确答案】 (1)TVSet(int size): size(size) (2)void turnOnOff() (3)cout “规格: “ getSize() “

18、英寸 “ 【试题解析】 (1)主要考查考生对构造函数的掌握,因为 size是常变量,所以只能用成员初始化列表来初始 size,即 TVSet(int size): size(size)。 (2)主要考查考生对 const函数的掌握,在 turnOnOff函数中,有语句: on !on;,使得 on的值发生改变,因此该函数不能使用 const。 (3)主要考查考生对输出语句的掌握,下一条语句: “,电源: “(isOn()?“开 “: “关 “),说明输出语句还没结束,因此不能用 “; ”。 二、简单应用题 2 【正确答案】 (1)x1(root), x2(root), Bum_of_roots

19、(1) (2)cout “无实根 “; break; (3)a(a), b(b), c(c) (4)return Quadratic(a X a, b x b, c x c); 【试题解析】 (1)主要考查考生对构造函数的掌握,题目要求创建一个 “有两个相同的实根 ”的 Root对象。说明两个根 x1和 )x2相等,根的数量为 1,因此可以得出语句: xl(root), x2(root), num_of_roots(1)。 (2)主要考查考 生对 switch语句的掌握,在语句 switch(num_of_roots)中,num_of_roots代表根的数量,当为 0时,表示没有根,因此输出无

20、实根,注意要在句尾加 break。 (3)主要考查考生对构造函数的掌握,本题使用成员初始化列表来构造函数。 (4)主要考查考生对成员函数的掌握,题目要求求两个多项式的差。两个多项式的差就是各个次方的系数相减,因此得出语句: return Quadratic(a x a bx b, c x e);。 三、综合应用题 3 【正确答案】 for(int i 0; i counter; i ) 7遍历数组 elem for(int j counter 1; j i; j ) 从最后一位到 i到前一位遍历 elem if(elemi elemj) 如果 elemi大于 elemj,则两值替换 int temp elemi; 定义整形变量 temp并赋值为 elemi; elemi elemj; 给 elemi赋值 elemi elemj temp; 给 elemj赋值 temp

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

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

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