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
19、cked=true; bool isClosed () const return closed; /门关着时返回 true,否则返回 false bool isOpened () const return ! closed; /门开着时返回 true,否则返回 false bool isLocked () const return locked; /门锁着时返回true,否则返回 false bool isUnlocked () const return ! locked; ) /门未锁时返回 true,否则返回 false void open() /开门 coutendl“打开“num“号门
20、.“; /ERROR*found* if(closed) cout“门是开着的,无须再开门。“; else if(locked) cout“门是锁着的,打不开。“; else closed=false; cout“门打开了。“; void close() /关门 coutendl“关上“num“号门.“; if(closed) cout“门是关着的,无须再关门。“; else closed=true; cout“门关上了。“; /ERROR*found* void lock()const /锁门 coutendl“锁上“num“号门.“; if(locked) cout“门是锁着的,无须再锁门
21、。“; else if(! closed) cout“先关门.“; closed=true; locked=true; cout“门锁上了。“; void unlock()/开锁 cout_正确答案:(1)this-num=num; (2)if(!closed) (3)void lock()解析:考点 本题考查 Door类,其中涉及 bool型私有成员及成员函数、构造函数和其他成员函数。在构造函数中 this指针指向的是当前类,因此当参数名与要赋值的成员名称一样时,使用 this指针来区别。解析 (1)主要考查考生对 this指针的掌握,在构造函数中 this指针指向的是当前类,因此要给 nu
22、m赋值使用语句 this-num=num;完成。 (2)主要考查考生对 if语句的掌握,先看类的私有成员中关于closed的定义:bool closed;/true 表示门关着。再看下一条语句:cout”门是开着的,无须再开门。”;。即满足条件时就会输出:门是开着的,无须再开门。因此 if括号内应该是!closed。 (3)主要考查考生对 const函数的掌握,lock 函数体中存在语句 locked=true,即有参数发生改变,因此不能用const。二、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 proj2,该工程中包含
23、一个程序文件main.cpp,其中有日期类 Date、人员类 Person及排序函数 sortByName和主函数 main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为: 按姓名排序 排序前 张三 男 出生日期:1978 年 4月 20日 王五 女 出生日期:1965 年 8月 3日 杨六 女 出生日期:1965 年 9月 5日 李四 男 出生日期:1973 年 5月 30日 排序后: 李四 男 出生日期:1973年 5月 30日 王五 女 出生日期:1965 年 8月 3日 杨六 女 出生日期:1965 年 9月 5日 张三 男 出
24、生日期: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 getYear()constreturn year;) int getMonth () const return month; in
25、t 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 () const return name; bool isMale () const return is male; Date get
26、Birthdate () const return birth date; /利用 strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于 int compareName (const Person coutname“(is_male? “男“: “女“)“出生日期: “birth_date. getYear() “年“/显示出生年 /*found* _/显示出生月 birth_date. getDay()“日“; /显示出生日 ; void sortByName (Person ps, int size) /将人员数组按姓名排列为升序 for(int i=0; is
27、ize-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(“杨六“, false, Date(1965, 9, 5), Person(“李四“, true, Date(1973, 5, 30)
28、; 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)_正确答案:(1)is_male(is_male),birth_date(birth_date) (2)return strcmp
29、(name,p.getName(); (3)birth_date.getMonth()“月“)解析:考点 本题考查 Date类和 Person类,其中涉及构造函数、const 函数、bool 型私有成员及成员函数,以及 strcmp()函数。 解析 (1)主要考查考生对构造函数的掌握,由函数体内 strcpy(this-name,name);可知,要使用成员列表初始化的成员为 is_male和 birth_date。 (2)主要考查考生对strcmp()函数的掌握,先看程序对该函数的功能要求:利用 strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于。因为 strc
30、mp()函数的功能是比较字符串大小,因此可以直接被return语句调用:return strcmp(name,p.getName();。 (3)主要考查考生对成员函数的掌握,程序的注释为:显示出生月,由此可以知道这里要输出出生月份,直接调用函数 getMonth()即可。 strcmp()函数、strcpy()函数、strlen()函数等是经常会用到的系统函数,要了解各个函数的功能:stralt(连接)、strcly(复制)、strump(比较)、strlen(求长度)。三、B综合应用题/B(总题数:1,分数:40.00)3.请使用 VC6或使用答题菜单打开考生文件夹 proj3下的工程 pr
31、oj3,其中包含了类 IntegerSet和主函数 main的定义。一个 IntegerSet对象就是一个整数的集合,其中包含 0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组 elem的前若干单元中。成员函数 add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数 remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数 remove。在 main函数中给出了一组测试数据,此时程序的正确输出结果应为: 2 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 2
32、8 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 #includeiostream using namespace std; const int MAXELEMENTS=100; /集合最多可拥有的元素
33、个数 class IntegerSet int elemMAXELEMENTS; /用于存放集合元素的数组 int counter; /用于记录集合中元素个数的计数器 public; IntegerSet(): counter(0) /创建一个空集合 IntegerSet(int data, int size); /利用数组提供的数据创建一个整数集合 void add(int element); /添加一个元素到集合中 void remove(int element); /删除集合中指定的元素 int getCount()constreturn counter; /返回集合中元素的个数 int
34、 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(datai); void IntegerSet:add(int element) int j; /从后往前寻找第一个小于等于e
35、lement的元素 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=element; /将 element插入到该位置 counter+; /计数器加 1 void IntegerSet:remove
36、 ( 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(); s.add(19); s.show(); s.remove(2); s.show(); s.add(4); s.show();
37、WriteToFile (“); return 0; (分数:40.00)_正确答案:(for(int i=0; icounter; i+) /遍历整个集合(数组 elem) if(element=elemi) /如果 element等于 elemi for(int j=i; jcounter-1; j+) /从 i开始遍历集合 elem elemj=elemj+1; /把 elemj+1赋值给 elemj counter-; /elem长度自减 1 return; /返回 )解析:考点 本题考查 IntegerSet类,其中涉及数组、构造函数、成员函数、const 函数和插入排序。类中的数组 elem是一个按升序存放的数组,要填写的程序段是完成 remove函数的功能,即删除指定元素。解析 主要考查考生对有序数组的掌握,题目要求成员函数 remove从集合中删除指定的元素(如果集合中存在该元素)。遍历数组 elem中的元素,找出与形参 element相等的元素,并将其删除,每删除一个元素,即将该元素之后的每个元素前移一位,如果不存在与形参 element相等的元素则没有操作。使用下标i遍历数组,if 语句判断是否与 element相等。