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

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

1、国家二级 C+机试(操作题)模拟试卷 136及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 proj1下的工程 projl,该工程中包含程序文件 main cpp,其中有类 Door(“门 ”)和主函数 main的定义。程序中位于每个 “ ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 打开 503号门 门是锁着的,打不开。 打开 503号门的锁 锁开了。 打开 503号门 门打开了。 打开 503号门 门是开着的,无须再开门。 锁上 503号门 先关门 门锁上了。 注意:只修改每个 “ ERROR*found*”下的那

2、一行,不要改动程序中的其他内容。 #include USing namespace std; clasS Door int num; 门号 bool closed; true表示门关着 bool locked; true表示门锁着 public: Door(int num) ERROR*found* num=thiS一 num; closed=locked=true; bool isClosed()constreturn closed; 门关着时返回 true,否则返回 false bool isOpened()constreturn!closed; 门开着时返回 true,否则返回 fals

3、e bool isLocked()constreturnlocked; 门锁着时返回 true,否则返回 false bool isUnlocked()const returnlocked; ) 门未锁时返回 true,否则返回 false void open() 开门 cout using namespace std; class Date 日期类 int year, month, day;年、月、日 public: Date(int year, int month, int day): year(year), month(month), day(day) int getYear()cons

4、treturn year; int getMonth()constreturn month; ) int getDay()const t return day; ; class Person人员类 char name14;姓名 bool is male;性别,为 true时表示男性 Date birth date;出生日期 public: Person(char*name, bool ismale, Date birthdate) *found* strcpy(this一 name, name); const char*getName()constreturn name; bool isMal

5、e()constreturn ismale; Date getBirthdate()const return birth date; 利用 strcmp()函数比较姓名,返回一个正数、 0或负数,分别表示大于、等于、小于 int compareName(const Person p) const *found* _ void show() couti) Person P=psm; psm=psi; psi=p, int main() Person staff= Person(“张三 ”, true, Date(1 978, 4, 20), Person(“王五 ”, false, Date(1

6、965, 8, 3), Person(“杨六 ”, false, Date(1965, 9, 5), Person(“李四 ”, true, Date(1 973, 5, 30) ; const int size=sizeof(staff) sizeof(staff0); int i; cout USing nameSPace Std; const int MAxELE陋 NTS=100; 集合最多可拥有的元素个数 class IntegerSet int elemMAXELEMENTS; 用于存放集合元素的数组 int counter; 用于记录集合中元素个数的计数器 public: Int

7、egerSet(): counter(0) 创建一个空集合 IntegerSet(int data, int size); 利用数组提供的数据创建一个整数集合 void add(int element); 添加一个元素到集合中 void remove(int element); 删除集合中指定的元素 int getCount()constreturn counter; 返回集合中元素的个数 int getElement(int i)constreturn elemi; 返回集合中指定的元素 void show()const; ; void WriteTOFile(char。 ); #endif

8、 main cpp #include”IntegerSet h” #include IntegerSet: IntegerSet(int data, int size): counter(O) for(int i=0; i0; j一一 ) if(element=elemj一 1)break; 如果找到的是等于 element的元素,说明要添加的元素已经存在,直接返回 if(j0) if(element=elemj一 1)return; 如果找到的是小于 element的元素, J就是要添加的位置 该元素及其后面的元素依次后移,腾出插入位置 for(int k=counter; kj; k一一

9、) elemk=elemk一 1; elemj=element; 将 element插入到该位置 counter+;计数器加 1 void IntegerSet: remove(int ele- ment) *333* *666* void IntegerSet: show()const for(int i=0; inum=hum; (2)if(!closed) (3)void lock() 【试题解析】 本题考查 Door类,其中涉及 bool型私有成员及成员函数、构造函数和其他成员函数。在构造函数中 this指针指向的是当前类,因此当参数名与要赋值的成员名称一样时,使用 this指针来区别

10、。 【解题思路】 (1)主要考查考生对 this指针的掌握,在构造函数中 this指针指向的是 当前类,因此要给 Bum赋值使用语句 this一 num: num;完成。 (2)主要考查考生对 if语句的掌握,先看类的私有成员中关于 closed的定义: bool closed; true表示门关着。再看下一条语句: coutnalne, nanle);可知,要使用成员列表 初始化的成员为 is male和 birthdate。 (2)主要考查考生对 strcmp()函数的掌握,先看程序对该函数的功能要求:利用strcmp()函数比较姓名,返回一个正数、 0或负数,分别表示大于、等于、小于。因

11、为 strcmp()函数的功能是比较字符串大小,因此可以直接被 return语句调用:return strcmp(nallle, p getName();。 (3)主要考查考生对成员函数的掌握,程序的注释为:显示出生月,由此可以知道这里要输出出生月份,直接调用函数 getMonth()即可。 【解题宝 典】 strcmp()函数、 strcpy()函数、 strlen()函数等是经常会用到的系统函数,要了解各个函数的功能: strait(连接 )、 strcly(复制 )、 strump(比较 )、 strlen(求长度 )。 三、综合应用题 3 【正确答案】 for(int i=0; iCO

12、Unter; 1+) 遍历整个集合 (数组 elem) if(element=elemi) 如果 element等于 elemi for(int j =i; j counter1, j+) 从 i开始遍历集合 elem elemj=elemj+1; 把 elemj+1赋值给 elemj counter一一; elem长度自减 1 return; 返回 【试题解析】 本题考查 IntegerSet类,其中涉及数组、构造函数、成员函数、cotmt函数和插入排序。类中的数组 elem是一个按升序存放的数组,要填写的程序段是完成 remove函数的功能,即删除指定元素。 【解题思路】主要考查考生对有序数组的掌握,题目要求成员函数 remove从集合中删除指定的元素 (如果集合中存在该元素 )。遍历数 组 elem中的元素,找出与形参 element相等的元素,并将其删除,每删除一个元素,即将该元素之后的每个元素前移一位,如果不存在与形参 element相等的元素则没有操作。使用下标 i遍历数组, if语句判断是否与 element相等。

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

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

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