【计算机类职业资格】二级C++-41及答案解析.doc
《【计算机类职业资格】二级C++-41及答案解析.doc》由会员分享,可在线阅读,更多相关《【计算机类职业资格】二级C++-41及答案解析.doc(5页珍藏版)》请在麦多课文档分享上搜索。
1、二级 C+-41及答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,该工程中包含程序文件main.cpp,其中有类 Door(“门”)和主函数 main的定义。程序中位于每个“/ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 打开 503号门门是锁着的,打不开。打开 503号门的锁锁开了。 打开 503号门门打开了。 打开 503号门门是开着的,无须再开门。 锁上 503号门先关门门锁上了。 注意:只修改每个“/ERROR
2、*found*”下的那一行,不要改动程序中的其他内容。 #includeiostream 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 () const return closed; /门关着时返回 true,否则返回 false bool isOpened () const retur
3、n ! closed; /门开着时返回 true,否则返回 false bool isLocked () const return locked; /门锁着时返回true,否则返回 false bool isUnlocked () const return ! locked; ) /门未锁时返回 true,否则返回 false void open() /开门 coutendl“打开“num“号门.“; /ERROR*found* if(closed) cout“门是开着的,无须再开门。“; else if(locked) cout“门是锁着的,打不开。“; else closed=false;
4、 cout“门打开了。“; void close() /关门 coutendl“关上“num“号门.“; if(closed) cout“门是关着的,无须再关门。“; else closed=true; cout“门关上了。“; /ERROR*found* void lock()const /锁门 coutendl“锁上“num“号门.“; if(locked) cout“门是锁着的,无须再锁门。“; else if(! closed) cout“先关门.“; closed=true; locked=true; cout“门锁上了。“; void unlock()/开锁 cout_二、B简单应
5、用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 proj2,该工程中包含一个程序文件main.cpp,其中有日期类 Date、人员类 Person及排序函数 sortByName和主函数 main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为: 按姓名排序 排序前 张三 男 出生日期:1978 年 4月 20日 王五 女 出生日期:1965 年 8月 3日 杨六 女 出生日期:1965 年 9月 5日 李四 男 出生日期:1973 年 5月 30日 排序后: 李四 男 出生日期
6、:1973年 5月 30日 王五 女 出生日期:1965 年 8月 3日 杨六 女 出生日期:1965 年 9月 5日 张三 男 出生日期:1978 年 4月 20日 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。 #includeiostream using namespace std; class Date/日期类 int year, month, day; /年、月、日 public: Date(int year, int month, int day): year(year), month (month), day(day) int
7、getYear()constreturn year;) int getMonth () const return month; int getDay () const return day; ; class Person/人员类 char name14; /姓名 bool is_male; /性别, 为 true时表示男性 Date birth date; /出生日期 public: Person(char*name, bool is male, Date birth_date) /*found* :_ strcpy(this-name, name); const char*getName (
8、) const return name; bool isMale () const return is male; Date getBirthdate () const return birth date; /利用 strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于 int compareName (const Person coutname“(is_male? “男“: “女“)“出生日期: “birth_date. getYear() “年“/显示出生年 /*found* _/显示出生月 birth_date. getDay()“日“; /显示出生日 ; vo
9、id sortByName (Person ps, int size) /将人员数组按姓名排列为升序 for(int i=0; isize-1; i+ /采用选择排序算法 int m=i; for(int j=i+1; jsize; j+) if(psj. compareName(psm)0) m=j; if(mi) Person p=psm; psm=psi; psi=p; int main() Person staff= Person(“张三“, true, Date(1978, 4, 20), Person(“王五“, false, Date(1965, 8, 3), Person(“杨
10、六“, false, Date(1965, 9, 5), Person(“李四“, true, Date(1973, 5, 30) ; const int size=sizeof (staff)/sizeof(staff0); int i; coutendl“按姓名排序“; coutendl“排序前:“; for(i=0; isize; i+) staffi. show(); sortByName (staff,size); coutendlendl“排序后:“; for(i=0; isize; i+) staffi. show(); coutendl; return 0; (分数:30.00
11、)_三、B综合应用题/B(总题数:1,分数:40.00)3.请使用 VC6或使用答题菜单打开考生文件夹 proj3下的工程 proj3,其中包含了类 IntegerSet和主函数 main的定义。一个 IntegerSet对象就是一个整数的集合,其中包含 0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组 elem的前若干单元中。成员函数 add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数 remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数 remove。在 main函数中给出了一组测试数据,此时程序的正确输出结果应为: 2
12、 3 4 5 27 28 31 66 75 2 3 4 5 6 27 28 31 66 75 2 3 4 5 6 19 27 28 31 66 75 3 4 5 6 19 27 28 31 66 75 3 4 5 6 19 27 28 31 66 75 要求: 补充编制的内容写在“/*333*”与“/*666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat中。输出函数 WriteToFile已经编译为 obj文件,并且在本程序中调用。/IntegorSet.h #ifndef INTEGERSET #define INTEGERSET #includeiost
13、ream using namespace std; const int MAXELEMENTS=100; /集合最多可拥有的元素个数 class IntegerSet int elemMAXELEMENTS; /用于存放集合元素的数组 int counter; /用于记录集合中元素个数的计数器 public; IntegerSet(): counter(0) /创建一个空集合 IntegerSet(int data, int size); /利用数组提供的数据创建一个整数集合 void add(int element); /添加一个元素到集合中 void remove(int element)
14、; /删除集合中指定的元素 int getCount()constreturn counter; /返回集合中元素的个数 int getElement (int i) const return elemi;)/返回集合中指定的元素 void show () const; ; void WriteToFile(char*); #endif /main.cpp #include“IntegerSet.h“ #includeiomanip IntegerSet:IntegerSet(int data, int size): counter(0) for(int i=0; isize; i+) add
15、(datai); void IntegerSet:add(int element) int j; /从后往前寻找第一个小于等于element的元素 for(j=counter; j0; j-) if(element=elemj-1)break; /如果找到的是等于element的元素,说明要添加的元素已经存在,直接返回 if(j0) if(element=elemj-1)return; /如果找到的是小于 element的元素,j 就是要添加的位置 /该元素及其后面的元素依次后移,腾出插入位置 for(int k=counter; kj; k-) elemk=elemk-1; elemj=el
16、ement; /将 element插入到该位置 counter+; /计数器加 1 void IntegerSet:remove ( int element ) /*333* /*666* void IntegerSet:show () const for(int i=0; igetCount(); i+) coutsetw(4)getElement (i); coutendl; int main() int d=5, 28, 2, 4, 5, 3, 2, 75, 27, 66, 31; IntegerSet s (d, 11); s.show (); s.add(6); s.show();
17、s.add(19); s.show(); s.remove(2); s.show(); s.add(4); s.show(); WriteToFile (“); return 0; (分数:40.00)_二级 C+-41答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,该工程中包含程序文件main.cpp,其中有类 Door(“门”)和主函数 main的定义。程序中位于每个“/ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
18、 打开 503号门门是锁着的,打不开。打开 503号门的锁锁开了。 打开 503号门门打开了。 打开 503号门门是开着的,无须再开门。 锁上 503号门先关门门锁上了。 注意:只修改每个“/ERROR*found*”下的那一行,不要改动程序中的其他内容。 #includeiostream using namespace std; class Door int num; /门号 bool closed; /true 表示门关着 bool locked; /true 表示门锁着 public: Door(int num) /ERROR*found* num=this-num; closed=lo
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
5000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 职业资格 二级 C41 答案 解析 DOC
