【计算机类职业资格】二级C++机试-179及答案解析.doc

上传人:ownview251 文档编号:1324359 上传时间:2019-10-17 格式:DOC 页数:8 大小:35.50KB
下载 相关 举报
【计算机类职业资格】二级C++机试-179及答案解析.doc_第1页
第1页 / 共8页
【计算机类职业资格】二级C++机试-179及答案解析.doc_第2页
第2页 / 共8页
【计算机类职业资格】二级C++机试-179及答案解析.doc_第3页
第3页 / 共8页
【计算机类职业资格】二级C++机试-179及答案解析.doc_第4页
第4页 / 共8页
【计算机类职业资格】二级C++机试-179及答案解析.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、二级 C+机试-179 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:33.00)1.给定一个函数,其函数功能为:使 10 个整数按由小到大的顺序排列。在主函数中输入 10 个数,并输出结果。使用 VC6 打开考生文件夹下的工程 RevProj5。此工程包含一个源程序文件 RevMain5cpp,该程序实现上述功能。但此程序运行有问题。请找出错误的地方,改正后将此程序调试正确。注意:不得删行或增行,也不得更改程序结构。文件 RevMain5.cpp 中的程序清单如下:/RevMain5.cpp#includeiostream#includeiomanip

2、using namespace std;int main()void sort(int array,int n);int data10,*p,i;cout“Input 10 numbers/n“;for (i=0; i10; i+)cindata i;cout“the origined array is:“;for (p=data;pdata+10;p+)if(p-coutsetw (5) *p;sort (data, 10);cout“/n the present array is:“;for(p=data;pdata+10;p+)if(p-coutsetw (5)*p;void sort(

3、int array,int n)/* * * * * * * * *found * * * * * * * * * */for(p1=array;p1array+(n-1) ;p1+)for (p2=p1+1 ;p2array+n;p2+)if (*p1*p2)/* * * * * * * * *found * * * * * * * * * */temp=*p1; *p1=*p2; *p2=*p1;(分数:33.00)_二、2简单应用题(总题数:1,分数:33.00)2.请编写一个函数 fun(),该函数的功能是:返回给定字符串中大写字母字符的个数。如字符串“Hello World“中,大写

4、字母的个数为 2 个。注意:部分源程序已存在文件 PROC5.CPP 中。请勿修改主函数和其他函数中的任何内容,仅在函数 fun()的花括号中填写若干语句。文件 PROC5.cpp 的内容如下:/PROC5.cpp#includeiostream#includestringusing namespace std;int fun(char *str);int main()char str =“Chinese Computer World“;coutfun(str)end;return 0;int fun(char *str)/*(分数:33.00)_三、3综合应用题(总题数:1,分数:34.00

5、)3.使用 VC6 打开考生文件夹下的工程 MyProj5。此工程包含一个源程序文件 MyMain5.cpp,其中定义了用于表示日期的类 Date,但类 Date 的定义并不完整。请按要求完成下列操作,将类 Date 的定义补充完成:定义私有数据成员 length、width 和 labe1,其中 length、width 是 int 型,labe1 是类 point 的对象。它们分别表示长方形的长、宽以及坐标点。请在注释“/*1*”之后添加适当的语句。完成构造函数 square 的定义,使 square 对象的默认值为:length=0,width=0, label(0,0),请在注释“/*

6、2*”之后添加适当的语句。完成重载构造函数 square(int l,int w,int x,int y)的定义,把数据成员 length、width 和对象labe1 分别初始化为参数 l、w、x 和 y 的值,请在注释“/*3*”之后添加适当的语句。完成成员函数 get_area()的类外定义,此函数的功能是返回此长方形的面积,及返回 length*width 的值。请在注释“/*4*”之后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。源程序文件 MyMain5.cpp 清单如下:/MyMain5.cpp#includeiostreamusing namesp

7、ace std;class pointprivate:int x,y;public:void set (int i,int j)x=i;y=j;int get_y ()return y;class square/* * 1 * *public:/* * 2 * *square(int l,int w, int x, int y)/* * 3 * *void set(int l,int w)length=1;width=w;int get_area() constreturn length*width;int square:get_area() const/* * 4 * *int main()

8、square small(2,4,1,35);coutsmall.get_area()end1;return 0;(分数:34.00)_二级 C+机试-179 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:33.00)1.给定一个函数,其函数功能为:使 10 个整数按由小到大的顺序排列。在主函数中输入 10 个数,并输出结果。使用 VC6 打开考生文件夹下的工程 RevProj5。此工程包含一个源程序文件 RevMain5cpp,该程序实现上述功能。但此程序运行有问题。请找出错误的地方,改正后将此程序调试正确。注意:不得删行或增行,也不得更改程序结构。文件

9、 RevMain5.cpp 中的程序清单如下:/RevMain5.cpp#includeiostream#includeiomanipusing namespace std;int main()void sort(int array,int n);int data10,*p,i;cout“Input 10 numbers/n“;for (i=0; i10; i+)cindata i;cout“the origined array is:“;for (p=data;pdata+10;p+)if(p-coutsetw (5) *p;sort (data, 10);cout“/n the prese

10、nt array is:“;for(p=data;pdata+10;p+)if(p-coutsetw (5)*p;void sort(int array,int n)/* * * * * * * * *found * * * * * * * * * */for(p1=array;p1array+(n-1) ;p1+)for (p2=p1+1 ;p2array+n;p2+)if (*p1*p2)/* * * * * * * * *found * * * * * * * * * */temp=*p1; *p1=*p2; *p2=*p1;(分数:33.00)_正确答案:(修改后的函数 sort 为:

11、void sort(int array,int n)int *p1,*p2,temp;for(p1=array;p1array+(n-1) ;p1+)for(p2=p1+1;p2array+n;p2+)if (*p1*p2)temp=*p1;*p1=*p2;*p2=temp;)解析:解析 程序中有两处标识行,表明程序中有两处错误,而且都是在函数 sort 的函数体中的错误。不难发现在函数 sort 中出现了*p1、*p2、temp 这样的变量,而在用到这些变量前,程序没有对它们进行定义,所以在第 1 处错误处写上“int *p1,*p2,temp;”。第 2 处错误:易知 if 语句中的语句序

12、列是实现两个指针变量中值的交换,显然上述语句不能实现此功能,所以应改为:“temp=*p1;*p1:*p2;*p2=temp;”二、2简单应用题(总题数:1,分数:33.00)2.请编写一个函数 fun(),该函数的功能是:返回给定字符串中大写字母字符的个数。如字符串“Hello World“中,大写字母的个数为 2 个。注意:部分源程序已存在文件 PROC5.CPP 中。请勿修改主函数和其他函数中的任何内容,仅在函数 fun()的花括号中填写若干语句。文件 PROC5.cpp 的内容如下:/PROC5.cpp#includeiostream#includestringusing namesp

13、ace std;int fun(char *str);int main()char str =“Chinese Computer World“;coutfun(str)end;return 0;int fun(char *str)/*(分数:33.00)_正确答案:(下面是函数 fun()函数体的一种实现:int fun(char *str)int num=0; /用于记录大写字母符的个数,初始值为 0。while(*str!=/0) /若是大写字母,则总数加 1。 if(*str=Astr+;return num;)解析:解析 本题要求编写函数 fun()的函数体,完成的功能是返回给定字符串

14、中大写字母字符的个数。从给出的函数首部可知,函数体可用字符指针和循环体的方式来实现。三、3综合应用题(总题数:1,分数:34.00)3.使用 VC6 打开考生文件夹下的工程 MyProj5。此工程包含一个源程序文件 MyMain5.cpp,其中定义了用于表示日期的类 Date,但类 Date 的定义并不完整。请按要求完成下列操作,将类 Date 的定义补充完成:定义私有数据成员 length、width 和 labe1,其中 length、width 是 int 型,labe1 是类 point 的对象。它们分别表示长方形的长、宽以及坐标点。请在注释“/*1*”之后添加适当的语句。完成构造函数

15、 square 的定义,使 square 对象的默认值为:length=0,width=0, label(0,0),请在注释“/*2*”之后添加适当的语句。完成重载构造函数 square(int l,int w,int x,int y)的定义,把数据成员 length、width 和对象labe1 分别初始化为参数 l、w、x 和 y 的值,请在注释“/*3*”之后添加适当的语句。完成成员函数 get_area()的类外定义,此函数的功能是返回此长方形的面积,及返回 length*width 的值。请在注释“/*4*”之后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内

16、容。源程序文件 MyMain5.cpp 清单如下:/MyMain5.cpp#includeiostreamusing namespace std;class pointprivate:int x,y;public:void set (int i,int j)x=i;y=j;int get_y ()return y;class square/* * 1 * *public:/* * 2 * *square(int l,int w, int x, int y)/* * 3 * *void set(int l,int w)length=1;width=w;int get_area() constre

17、turn length*width;int square:get_area() const/* * 4 * *int main()square small(2,4,1,35);coutsmall.get_area()end1;return 0;(分数:34.00)_正确答案:(补充完整后,类 square 的完整定义为:class squareprivate:int length,width;point label;public:square()length=0;width=0;label.set(0,0);square(int l,int w, int x,int y)length=1;width=w;labe1, set (x, y);void set(int l,int w)length=1;width=w;int get_area() const;int square:get_area() constreturn length*width;)解析:解析 第 1 空白处是定义类 square 的各种私有成员,第 2 空白处完成构造函数 square()的定义,并具有默认参数,第 3 空白处是完成重载构造函数,第 4 空白处完成成员函数 get_area()的类外定义。

展开阅读全文
相关资源
猜你喜欢
  • STAS SR ISO 2268-1995 Surface active agents (non-ionic) - Determination of poly-ethylene glycols and non-ionic active matter (adducts) -Weibull method《表面活性剂(非离子型).聚乙烯乙二醇和非离子活性物质(加合.pdf STAS SR ISO 2268-1995 Surface active agents (non-ionic) - Determination of poly-ethylene glycols and non-ionic active matter (adducts) -Weibull method《表面活性剂(非离子型).聚乙烯乙二醇和非离子活性物质(加合.pdf
  • STAS SR ISO 2270-1995 Nonionic surface active agents - Polyetoxilated dertvates lodometrical determination of the oxyethylenic groups《非离子表面.活性剂.乙氧乙烯基化的聚乙氧基衍生物碘量法测定 》.pdf STAS SR ISO 2270-1995 Nonionic surface active agents - Polyetoxilated dertvates lodometrical determination of the oxyethylenic groups《非离子表面.活性剂.乙氧乙烯基化的聚乙氧基衍生物碘量法测定 》.pdf
  • STAS SR ISO 2281-1994 Horology Water-resistant watches《钟表学.防水表》.pdf STAS SR ISO 2281-1994 Horology Water-resistant watches《钟表学.防水表》.pdf
  • STAS SR ISO 2285-1995 Vulcanized or thermoplastic rubber — Determination of tension set at normal and high temperatures《硫化货热塑性橡胶.设定在常温和高温下的张力测试 》.pdf STAS SR ISO 2285-1995 Vulcanized or thermoplastic rubber — Determination of tension set at normal and high temperatures《硫化货热塑性橡胶.设定在常温和高温下的张力测试 》.pdf
  • STAS SR ISO 2303+A1-1996 Ssoprene rebber (IR)  Non-oil extended  solution-poiymenzed types - Evaluation procedure《异戊橡胶的非石油延伸,聚合溶解类型.评价程序 》.pdf STAS SR ISO 2303+A1-1996 Ssoprene rebber (IR) Non-oil extended solution-poiymenzed types - Evaluation procedure《异戊橡胶的非石油延伸,聚合溶解类型.评价程序 》.pdf
  • STAS SR ISO 2315-1995 Aircraft - Two and four pole sealed electromagnetic relay 2 A and 3 A - Clearance and fixing dimensions《飞行器.2和4极密封电磁继电器,2A和3A.清除和修复部分 》.pdf STAS SR ISO 2315-1995 Aircraft - Two and four pole sealed electromagnetic relay 2 A and 3 A - Clearance and fixing dimensions《飞行器.2和4极密封电磁继电器,2A和3A.清除和修复部分 》.pdf
  • STAS SR ISO 2354-1995 Magnesium alloys Determination of insoluble zirconium Alizarin sulphonate photometric method《镁合金.不溶性锆的测定.茜素磺酸光度法》.pdf STAS SR ISO 2354-1995 Magnesium alloys Determination of insoluble zirconium Alizarin sulphonate photometric method《镁合金.不溶性锆的测定.茜素磺酸光度法》.pdf
  • STAS SR ISO 2374-1983 Lifting aDpiiances - Range of maximum capacities for basic models《起重装置.基本模式的最大能力范围 》.pdf STAS SR ISO 2374-1983 Lifting aDpiiances - Range of maximum capacities for basic models《起重装置.基本模式的最大能力范围 》.pdf
  • STAS SR ISO 2380-2-1994 SCREWDRIVERS FOR SLOTTED-HEAD SCREWS Part 2  General requirements lengts of blades and marking of hand operated screwdrivers《狭槽头螺钉的螺丝刀.第二部:一般要求,刀片的长度和手工操作螺丝.pdf STAS SR ISO 2380-2-1994 SCREWDRIVERS FOR SLOTTED-HEAD SCREWS Part 2 General requirements lengts of blades and marking of hand operated screwdrivers《狭槽头螺钉的螺丝刀.第二部:一般要求,刀片的长度和手工操作螺丝.pdf
  • 相关搜索

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

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