1、二级 C+机试-133 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.请使用“答题”菜单或使用 VC6 打开考生文件夹 proj1 下的工程 proj1。此工程定义了 Stop-Watch(秒表)类,用于表示时、分、秒信息,有构造函数 StopWatch()、设置时间函数 reset()、并且重载了前置和后置+运算符,用于实现增加秒的功能。程序中位于每个/ERROR*found*下的语句行有错误,请加以改正。改正后程序的输出应该是:00:00:0000:01:00注意:只能修改每个/ERROR*found*下的那一行,不要改动程序中的
2、其他内容。/源程序#includeiostream#includeiomanipusing namespace std;class StopWatch /“秒表”类int hours,minutes,seconds; /小时、分钟、秒public:StopWatch():hours(0),minutes (0),seconds(0)void reset()hours=minutes=seconds=0;StopWatch operator+(int) /后置+StopWatch old=*this;+(*this);return old;/前进 1 秒StopWatch operator+()
3、 /前置+/ERROR*found*if(seconds+=60)seconds=0;minutes+;if(minutes=60)minutes=0;hours+;/ERROR*found*return this:friend void show(StopWatch);;void show(StopWatch watch)coutsetfill(0);coutsetw(2)watch.hours:setw(2)watch.minutes:setw(2)watch.secondsendl;int main()StopWatch sw;show(sw);for (int i=0; i59; 1+
4、) sw+;/ERROR*found*show(sw+);return 0:(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.请使用“答题”菜单或使用 VC6 打开考生文件夹 proj2 下的工程 proj2。此工程定义了一个人员类Person,然后派生出学生类 Student 和教授类 Professor。请在程序中的画线处填写适当的代码,然后删除横线,以实现上述定义。此程序的正确输出结果应为:My name is Zhang.my name is Wang and my G.P.A.is 3.88My name is Li,I have 8 publications
5、注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动“/*found*”。/源程序#include iostreamusing namespace std;class Personpublic:/*found*_name=NULL;Person(char*s) name=new charstrlen(s)+1;strcpy(name,s);Person() if(name!=NULL) deletename;/*found*_Disp() cout“My name is“name“./n“; /声明虚函数void setName(char*s) name=new cha
6、rstrlen(s)+1;strcpy(name,s); protected:char*name:;class Student: public Personpublic:/*found*Student(char*s,double g)_void Disp() cout“my name is“name“and my G.P.A.is ilgpa“./n“; private:float gpa;class Professor: public Personpublic:void setPubls(int n)publs=n;void Disp() cout“My name is“name“,I ha
7、ve“publs“publications./n“; private:int publs;int main()/*found*_;Person x(“Zhang“);p=x;p-Disp();Student y(“Wang“,3.88);p=y;p-Disp();Professor z:z.setName(“Li“);z.setPubls(8);p=z;p-Disp();return 0:(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.请使用“答题”菜单或使用 VC6 打开考生文件夹 proj3 下的工程 proj3,其中声明了 MyString 类。MyString
8、 是一个用于表示字符串的类。成员函数 startsWith 的功能是判断此字符串是否以指定的前缀开始,其参数 s 用于指定前缀字符串。如果参数 s 表示的字符串是 MyString 对象表示的字符串的前缀,则返回 true;否则返回 false。注意,如果参数 s 是空字符串或等于 MyString 对象表示的字符串,则结果为 true。例如:字符串“abc“是字符串“abcde“的前缀,而字符串“abd“不是字符串“abcde“的前缀。请编写成员函数startsWith。在 main 函数中给出了一组测试数据,此情况下程序的输出应该是:s1=abcdes2=abcs3=abds4=s5=ab
9、cdes6=abcdefs1 startsWith s2:trues1 startsWith s3 falses1 startsWith s4 trues1 startsWith s5 f trues1 startsWith s6 false要求:补充编制的内容写在/*333*与/*666*两行之间,不得修改程序的其他部分。注意:程序最后已经将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。/源程序#include“MyString.h“bool MyString:startsWith(const char*s)const/*
10、333*/*666*int main()char s1=“abcde“;char s2=“abc“;char s3=“abd“;char s4=“ “;char s5=“abcde“;char s6=“abcdef“;MyString str(s1);cout“s1=“s1endl“s2=“s2endl“s3=“s3endl“s4=“s4endl“s5=“s5endl“s6=“s6endl:coutboolalpha“s1 startsWith s2:“str.startsWith(s2)endl“s1 startsWith s3:“str.startsWith(s3)endl“s1 star
11、tsWith s4:“str.startsWith(s4)endl“s1 startsWith s5:“str.startsWith(s5)endl“s1 startsWith s6:“str.startsWith(s6)endl;/writeToFile(“K:/bl0/61000101/“);return 0:(分数:30.00)_二级 C+机试-133 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.请使用“答题”菜单或使用 VC6 打开考生文件夹 proj1 下的工程 proj1。此工程定义了 Stop-Watch(秒表)类,用于
12、表示时、分、秒信息,有构造函数 StopWatch()、设置时间函数 reset()、并且重载了前置和后置+运算符,用于实现增加秒的功能。程序中位于每个/ERROR*found*下的语句行有错误,请加以改正。改正后程序的输出应该是:00:00:0000:01:00注意:只能修改每个/ERROR*found*下的那一行,不要改动程序中的其他内容。/源程序#includeiostream#includeiomanipusing namespace std;class StopWatch /“秒表”类int hours,minutes,seconds; /小时、分钟、秒public:StopWatc
13、h():hours(0),minutes (0),seconds(0)void reset()hours=minutes=seconds=0;StopWatch operator+(int) /后置+StopWatch old=*this;+(*this);return old;/前进 1 秒StopWatch operator+() /前置+/ERROR*found*if(seconds+=60)seconds=0;minutes+;if(minutes=60)minutes=0;hours+;/ERROR*found*return this:friend void show(StopWat
14、ch);;void show(StopWatch watch)coutsetfill(0);coutsetw(2)watch.hours:setw(2)watch.minutes:setw(2)watch.secondsendl;int main()StopWatch sw;show(sw);for (int i=0; i59; 1+) sw+;/ERROR*found*show(sw+);return 0:(分数:30.00)_正确答案:(1)if(+seconds=60)2)return*this;3)show(+sw);)解析:1)是在前置+运算符重载函数中,所以应该是+secnods
15、而不是 seconds+。2)函数类型是 StopWatch 对象,所以应该将本对象返回,而不是对象指针。为*this,而非 this。3)题目要求显示的是 00:01:00,调用 show(sw+)时显示的是 00:01:59,故知道该处应该改为调用前置重载运算符 show(+sw)。二、2简单应用题(总题数:1,分数:40.00)2.请使用“答题”菜单或使用 VC6 打开考生文件夹 proj2 下的工程 proj2。此工程定义了一个人员类Person,然后派生出学生类 Student 和教授类 Professor。请在程序中的画线处填写适当的代码,然后删除横线,以实现上述定义。此程序的正确
16、输出结果应为:My name is Zhang.my name is Wang and my G.P.A.is 3.88My name is Li,I have 8 publications注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动“/*found*”。/源程序#include iostreamusing namespace std;class Personpublic:/*found*_name=NULL;Person(char*s) name=new charstrlen(s)+1;strcpy(name,s);Person() if(name!=NULL
17、) deletename;/*found*_Disp() cout“My name is“name“./n“; /声明虚函数void setName(char*s) name=new charstrlen(s)+1;strcpy(name,s); protected:char*name:;class Student: public Personpublic:/*found*Student(char*s,double g)_void Disp() cout“my name is“name“and my G.P.A.is ilgpa“./n“; private:float gpa;class Pr
18、ofessor: public Personpublic:void setPubls(int n)publs=n;void Disp() cout“My name is“name“,I have“publs“publications./n“; private:int publs;int main()/*found*_;Person x(“Zhang“);p=x;p-Disp();Student y(“Wang“,3.88);p=y;p-Disp();Professor z:z.setName(“Li“);z.setPubls(8);p=z;p-Disp();return 0:(分数:40.00
19、)_正确答案:(1)Person()name=NULL;2)virtual void Disp()cout“My name is“name“./n“;3)Student(char 术 s,double g):Person(s),gpa(g)4)Person *p;)解析:1)声明 Person 的无参构造函数。2)声明 Disp 为虚函数,而且 Disp 没有返回值,所以它的函数类型为 void。3)声明 Student 的构造函数,Student 继承了 Person 类,所以在派生类构造函数中一定要为基类的带参构造函数赋值,同时还要为派生类自己的数据成员初始化赋值。一般格式如下:派生类名(
20、参数总表):基类名(参数表),数据成员 1(初始值 1),4)由下面的程序,可知 p 在使用前要声明,故此此处是声明 p。三、3综合应用题(总题数:1,分数:30.00)3.请使用“答题”菜单或使用 VC6 打开考生文件夹 proj3 下的工程 proj3,其中声明了 MyString 类。MyString 是一个用于表示字符串的类。成员函数 startsWith 的功能是判断此字符串是否以指定的前缀开始,其参数 s 用于指定前缀字符串。如果参数 s 表示的字符串是 MyString 对象表示的字符串的前缀,则返回 true;否则返回 false。注意,如果参数 s 是空字符串或等于 MySt
21、ring 对象表示的字符串,则结果为 true。例如:字符串“abc“是字符串“abcde“的前缀,而字符串“abd“不是字符串“abcde“的前缀。请编写成员函数startsWith。在 main 函数中给出了一组测试数据,此情况下程序的输出应该是:s1=abcdes2=abcs3=abds4=s5=abcdes6=abcdefs1 startsWith s2:trues1 startsWith s3 falses1 startsWith s4 trues1 startsWith s5 f trues1 startsWith s6 false要求:补充编制的内容写在/*333*与/*666*
22、两行之间,不得修改程序的其他部分。注意:程序最后已经将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。/源程序#include“MyString.h“bool MyString:startsWith(const char*s)const/*333*/*666*int main()char s1=“abcde“;char s2=“abc“;char s3=“abd“;char s4=“ “;char s5=“abcde“;char s6=“abcdef“;MyString str(s1);cout“s1=“s1endl“s2=“
23、s2endl“s3=“s3endl“s4=“s4endl“s5=“s5endl“s6=“s6endl:coutboolalpha“s1 startsWith s2:“str.startsWith(s2)endl“s1 startsWith s3:“str.startsWith(s3)endl“s1 startsWith s4:“str.startsWith(s4)endl“s1 startsWith s5:“str.startsWith(s5)endl“s1 startsWith s6:“str.startsWith(s6)endl;/writeToFile(“K:/bl0/61000101/
24、“);return 0:(分数:30.00)_正确答案:(/*333*if(s=NULL) return true,elsefor(int i=0;istrlen(s);i+)if(stri!=si)return false;return true:/*666*)解析:解析 首先判断传人的形参 s 是否为空字符串,如果是,则直接返回为 true,否则进行比较。按照 s 的字符长度进行比较。如果 strlen(s)size,显然为 false;如果 strlen(s)=size,则逐个位置元素比较。如果出现不相等,则直接返回 fasle;如果循环完毕仍然相等,则说明 s 被包含在 str 中,返回 true。