1、二级 C+-39及答案解析(总分:100.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)使用 VC+6.0打开考生文件夹下的源程序文件 1.cpp,该程序运行时有错误,请改正错误,使得程序输出:Hellotest注意:不要改动 main函数,不能增加或删除行,也不能更改程序的结构,错误的语句在/*error*的下面。试题程序:#includeiostream/*error*template(T)void fun(T t)std:cout“test“std:end1;/*error*templateboolvoid fun(bool t)std:cout(t?“H
2、ello“:“Hi“)std:end1;int main()/*error*bool flag=TRUE;fun(flag);fun(int)flag);return 0;(分数:30.00)填空项 1:_填空项 1:_填空项 1:_二、B2简单应用题/B(总题数:1,分数:40.00)1.请使用“答题”菜单或使用 VC6打开考生文件夹 proj2下的工程 proj2,函数 void Insert(node*q)使程序能够完成如下功能:从键盘输入一行字符,调用该函数建立反序的无头结点的单链表,然后输出整个链表。 注意:请勿修改主函数 main和其他函数中的任何内容,只需在画线处编写适当代码,也
3、不能删除或移动/*found*。 /源程序 proj2.cpp #includeiostream using namespace std; struct node char data; node*link: *head; /链表首指针 void Insert(node*q) /将节点插入链表首部 /*found* _; head=q; int main() char ch; node *p; head=NULL: cout“Please input the string“endl; while(ch=cin.get()!=/n) /*found* _;/用 new为节点 p动态分配存储空间 p
4、-data=ch; /*found* _; /在链表首部插入该节点 p=head; while(p!=NULL) coutp-data; p=p-link; coutendl; return 0: (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)2. 试题源程序文件清单如下: #includeiostream #includeiornanip using namespace std; class MiniString public: friend ostream return output; friend istream /用于输入的临时数组 temp0=/0; /
5、初始为空字符串 inputsetw(100)temp; int inten= strlen(temp); /输入字符串长度 if(inten! =0) s.length= inten; /赋长度 if(s.sPtr!=0) deletes.sPtr; /避免内存泄露 s.sPtr=new chars.length+1; strcpy(s.sPtr,temp); /如果 s不是空指针,则复制内容 else s.sPtr0=/0; /如果 s是空指针,则为空字符串 return input; /*333* /*666* private: int length; /字符串长度(不超过 100个字符)
6、 char*sPtr; /指向字符串的起始地址 ; /proj3. cpp #includeiostream #includeiomanip using namespace std; #include “proj3.h“ int main() void writeToFile(char*); MiniString strl(“Happy“); coutstrl“/n“; writeToFile(“K:/K01/61010002/“); return 0: (分数:30.00)_二级 C+-39答案解析(总分:100.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)
7、使用 VC+6.0打开考生文件夹下的源程序文件 1.cpp,该程序运行时有错误,请改正错误,使得程序输出:Hellotest注意:不要改动 main函数,不能增加或删除行,也不能更改程序的结构,错误的语句在/*error*的下面。试题程序:#includeiostream/*error*template(T)void fun(T t)std:cout“test“std:end1;/*error*templateboolvoid fun(bool t)std:cout(t?“Hello“:“Hi“)std:end1;int main()/*error*bool flag=TRUE;fun(fla
8、g);fun(int)flag);return 0;(分数:30.00)填空项 1:_ (正确答案:应改为“templateclass T”。)解析:填空项 1:_ (正确答案:删除“templatebool”。)解析:填空项 1:_ (正确答案:应改为“bool flag=true;”。)解析:解析 C+中的模板类格式为 templateclass T,所以第 1处的语句“template(T)”应修改为“templateclass T”。在第 2处的“void fun(bool t)”函数中,t 变量为布尔型变量,是 C+中的基础变量,不是模板变量,没有使用到模板类,所以删除第 2处的模板
9、类型定义语句。第 3处的“bool flag=TRUE;”是声明布尔型变量 flag,并将其值赋为逻辑真。而 TRUE并不是合法值,C+中区分大小写,所以逻辑真值应为 true,即“bool flag=true;”。二、B2简单应用题/B(总题数:1,分数:40.00)1.请使用“答题”菜单或使用 VC6打开考生文件夹 proj2下的工程 proj2,函数 void Insert(node*q)使程序能够完成如下功能:从键盘输入一行字符,调用该函数建立反序的无头结点的单链表,然后输出整个链表。 注意:请勿修改主函数 main和其他函数中的任何内容,只需在画线处编写适当代码,也不能删除或移动/*
10、found*。 /源程序 proj2.cpp #includeiostream using namespace std; struct node char data; node*link: *head; /链表首指针 void Insert(node*q) /将节点插入链表首部 /*found* _; head=q; int main() char ch; node *p; head=NULL: cout“Please input the string“endl; while(ch=cin.get()!=/n) /*found* _;/用 new为节点 p动态分配存储空间 p-data=ch;
11、 /*found* _; /在链表首部插入该节点 p=head; while(p!=NULL) coutp-data; p=p-link; coutendl; return 0: (分数:40.00)_正确答案:(1)node*head; 2)p=new node(); 3)p-link=head; head=p;)解析:1)后面有 head=q;,显然有个变量头指针没定义,所以在该行应该定义一个头指针 head。 2)为指针 p分配内存空间。 3)将新结点插入到链表中,链表的指针顺序不能搞错,应该是先将 p-link 指向head指向的地址,然后将 head指针指向 p。三、B3综合应用题/
12、B(总题数:1,分数:30.00)2. 试题源程序文件清单如下: #includeiostream #includeiornanip using namespace std; class MiniString public: friend ostream return output; friend istream /用于输入的临时数组 temp0=/0; /初始为空字符串 inputsetw(100)temp; int inten= strlen(temp); /输入字符串长度 if(inten! =0) s.length= inten; /赋长度 if(s.sPtr!=0) deletes.
13、sPtr; /避免内存泄露 s.sPtr=new chars.length+1; strcpy(s.sPtr,temp); /如果 s不是空指针,则复制内容 else s.sPtr0=/0; /如果 s是空指针,则为空字符串 return input; /*333* /*666* private: int length; /字符串长度(不超过 100个字符) char*sPtr; /指向字符串的起始地址 ; /proj3. cpp #includeiostream #includeiomanip using namespace std; #include “proj3.h“ int main() void writeToFile(char*); MiniString strl(“Happy“); coutstrl“/n“; writeToFile(“K:/K01/61010002/“); return 0: (分数:30.00)_正确答案:()解析:解析 默认构造函数 MiniString (const char*s=“)中,需要用类的数据成员 char*sPtr动态分配足够的存储空间来接收 s所指向的字符串,可采用 strlen()函数求字符串长度,但分配空间时需要加1。在析构函数中应该将 sPtr指向的存储空间释放。