C++-全国计算机二级真题(含上机题、答案)_第1页
C++-全国计算机二级真题(含上机题、答案)_第2页
C++-全国计算机二级真题(含上机题、答案)_第3页
C++-全国计算机二级真题(含上机题、答案)_第4页
C++-全国计算机二级真题(含上机题、答案)_第5页
已阅读5页,还剩204页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

VC++全国计算机二级题库

第一套

请使用VC6打开考生文件夹下的工程projl,该工程含有一个源程序文件projl.cpp。其中每个注释""ERROR

***********found*************"之后的•行有语句存在错误。请修改这些错误,使程序的输出结果为:123456789

10

注意:只需修改注释"/ERROR***********found**********〃的下•行语句,不要改动程序中的其它内容。

//projl.cpp

include<iostream>

usingnamespacestd;

classMyClass{

public:

MyClass(intlen)

{

array=newint[len];

arraySize=len;

for(inti=0;i<arraySize;i++)

array[i]=i+1;

)

~MyClass()

//ERROR**********found**********

delete[]array;

}

voidPrint()const

for(inti=0;i<arraySize;i++)

11ERROR**********^Qjjp£|**********

cout«array[i]«'

cout«endl;

)

private:

int*array;

intarraySize;

};

intmain()

11ERROR**********^Qgp^|**********

MyClassobj(10);

obj.Print();

return0;

)

请使用VC6打开考生文件夹下的工程proj2,该工程含有一个源程序文件pro.j2.cpp。其中定义了类Bag和用于测试该类的主函

数main。类Bag是一个袋子类,用来存放带有数字标号的小球(如台球中的球,在类中用一个整数值表示一个小球),其中运

算符成员函数==用来判断两个袋子对象是否相同(即小球的个数相同,每种小球数目也相同,但与它们的存储顺序无关):

成员函数intInBag(intball)用来返回小球ball在当前袋子内出现的次数,返回0表示该小球不存在。为类实现这两个函数,

其用法可参见主函数main。

运算符函数。perator==中首先判断两个袋子内的小球个数是否相同,再调用InBag函数来判断每种小球在两个袋子内是

否具有相同的出现次数。

注意:只需在指定位置编写适当代码,不要改动程序中的其他内容,也不能删除或移动

”//**********found******************"0

//proj2.cpp

#include<iostream>

usingnamespacestd;

constintMAXNUM=100;

classBag{

private:

intnum;

intbag[MAXNUM];

public:

Bag(intm[\,intn=0);//构造函数

booloperator==(Bag&b);//重载运算符==

intlnBag(intball);//某一小球在袋子内的出现次数,返回0表示不存在

);

Bag::Bag(intm[],intn)

if(n>MAXNUM){

cerr«"Toomanymembers\n";

exit(-l);

for(inti=0;i<n;i++)

bag[i]=m[i];

num=n;

boolBag::operator==(Bag&b)//实现运算符函数二二

if(num!=b.num)//元素个数不同

returnfalse;

for(inti=0;i<num;i++)

〃**********found**********

if(lnBag(bag[i])!=b.lnBag(bag[i]))//TODO:加入条件,判断当前袋子中每个元素在当前袋子和袋子

b中是否出现次数不同

〃**********found**********

returnfalse;//TODO:加入一条语句

returntrue;

)

intBag::lnBag(intball)

intcount=0;

for(inti=0;i<num;i++)

〃**********found**********

if(bag[i]==ball)//TODO:加入条件,判断小球ball是否与当前袋子中某一元素相同

〃**********found**********

count++;//TODO:加入一条语句

returncount;

)

intmain()

intdata[MAXNUM],n,i;

cin»n;

for(i=0;i<n;i++)

cin»data[i];

Bagbl(data,n);//创建袋子对象bl

cin»n;

for(i=0;i<n;i++)

cin»data[i];

Bagb2(data,n);//创建袋子对象b2

if(bl==b2)//测试bl和b2是否相同

cout«"BagblissamewithBagb2\n";

else

cout«"BagblisnotsamewithBagb2\n";

return0;

请使用VC6打开考生目录下的工程文件proj3。此工程包含一个源程序文件proj3.cpp,其中定义了用于表示二维向量的类

MyVector;程序应当显示:(6,8)

但程序中有缺失部分,请按下面的提示,把下划线标出的三处缺失部分补充完整:

(1)在〃**1***************found***********的下方是构造函数的定义,它用参数提供的坐标对x和y进行初始化。

(2)在〃**2****************found***********的卜方是减法运算符函数定义中的一条语句。两个二维向量相减生成另

•个二维向量:它的X坐标等于两个向量X的坐标之差,它的Y坐标等于两个向量Y坐标之差。

(3)在〃**3*****************found***********的下方的语句的功能是使变量v3获得新值,它等于向量vl与向量v2之

和。

注意:只需在指定位置编写适当的代码,不要改动程序中的其它内容,也不能删除或移动

"*************found***************"。

//proj3.cpp

#include<iostream>

usingstd::ostream;

usingstd::cout;

usingstd::endl;

classMyVector{〃表示二维向量的类

doublex;〃X坐标值

doubley;//Y坐标值

public:

MyVector(doublei=0.0,doublej=0.0);〃构造函数

MyVectoroperator+(MyVectorj);〃重投运算符+

friendMyVectoroperator-(MyVectori,MyVectorj);〃重载运算符-

friendostream&operator«(ostream&os,MyVectorv);//重载运算符<<

);

//**I************found**********

MyVector::MyVector(doublei,doublej):x(i),y(j){}

MyVectorMyVector::operator+(MyVectorj){

returnMyVector(x+j.x,y+j.y);

)

MyVectoroperator-(MyVectori,MyVectorj)

{//**2************found**********

returnMyVector(i.x-j.x,i.y-j.y);

)

ostream&operator«(ostream&os,MyVectorv){

os<<'('«v.x«*,1«v.y«*)*;〃输出向量v的坐标

returnos;

)

intmainO

{

MyVectorvl(2,3),v2(4,5),v3;

//**3************found**********

v3=vl+v2;

cout«v3«endl;

return0;

)

第二套

请使用VC6打开考生文件夹下的工程projl,该工程含有一个源程序文件projl.cppo其中位于每个注释“"ERROR

************found***************〃之后的•行语句存在错误。请修正这些错误,使程序的输出结果为:

Constructorcalledof10

Thevalueis10

Descructorcalledof10

注意:只需要修改注释'7/ERROR*******found***************〃的卜.一行语句,不要改动程序中的其它内容。

//projl.cpp

#include<iostream>

usingnamespacestd;

classMyClass{

public:

MyClass(inti)

(

value=i;

cout<<*Constructorcalledof"«value<<endl;

)

//ERROR**********found**********

voidPrint()const

{cout«”Thevalueis〃«value«endl;}

//ERROR**********found**********

^MyClass()

{cout<</zDestruetorcalledof〃«value«endl;}

private:

//ERROR**********found**********

intvalue;

);

intmain()

(

constMyClassobj(10);

obj.Print();

return0;

)

凡用过C语言标准库函数strcpy(char*sl,char*s2)的程序员都知道使用该函数时有一个安全隐患,即当指针si所指向的空间

不能容纳字符串s2的内容时,将发生内存错误。类String的Strcpy成员函数能进行简单的动态内存管理,其内存管理策略为:

(1)若已有空间能容纳下新字符串,则直接进行字符串拷贝;

(2)若已有空间不够时,它将重新申请一块内存空间(能容纳下新字符串),并将新字符串内容拷贝到新申请的空间中,

释放原字符空间。

请使用VC6打开考生文件夹下的工程proj2,该工程含有一个源程序文件proj2.cpp。其中定义了类String和用于测试该类

的主函数main,并且成员函数Strcpy的部分实现代码已在该文件中给出,请在标有注释〃〃**********found**********〃行的

卜一行添加适肖的代码,将这个函数补充完整,以实现其功能。

注意:只需在指定位置编写适当代码,不要改动程序中的其它内容,也不能删除或移动

〃//**********found******************“o

//proj2.cpp

#include<iostream>

usingnamespacestd;

classString{

private:

intsize;//缓冲区大小

char*buf;//缓冲区

public:

String(intbufsize);

voidStrcpy(char*s);//将字符串s复制到buf中

voidPrint()const;

^String(){if(buf!=NULL)deletebuf;}

};

String::String(intbufsize)

(

size=bufsize;

buf=newchartsize];

*buf='\0';

)

voidString::Strcpy(char*s)

(

char*p,*q;

intlen=strlen(s);

if(len+1>size){

size=len+1;

p=q=newchar[size];

//**********[ourid**********

while(*(q++)=*(s++));//TODO:添加代码将字符串s拷贝到字符指针q中

delete[]buf;

buf=p;

)

else{

〃**********found**********

for(p=buf;*p=*s;p++,s++);//TODO:添加代码将字符串s拷贝到buf中

)

}

voidString::Print()const

(

cout«size«'\t'«buf«endl;

)

intmain()

(

chars[100];

Stringstr(32);

cin.getline(s,99);

str.Strcpy(s);

str.Print();

return0;

)

请使用VC6打开考生目录下的工程文件proj3。此工程包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中

的点的类myPoinl和表示三角形的类MyTriangle;程序运行后应当显示:

6.82843

2

但程序中的缺失部分,请按卜一面的提示,把卜.划线标出的三处缺失部分补充完整:

(1)在/***********found***********的下方是构造函数的定义,它参数提供的三个顶点对point1、point2和

point3进行初始化。

(2)在〃**2**************1ound***********的下方是成员函数perimeIer的定义,该函数返叵「三角形的周长。

(3)在〃**3**************found***********的卜方是成员函数area的定义中的一条语句。函数area返回三角形的面

积。

方法是:若a、b、c为三角形的三个边长,并令s=(a+b+c)/2,则三角形的面积A为A二sqrt(s*(s-a)*(s-b)*(s-c))(其中

sqrl表示开二次方)

注意:只需在指定位置编写适当代码,不要改动程序中的其它内容,也不能删除或移动

”//**************found*************“o

//proj3.cpp

ttinclude<iostream>

#include<cmath>

usingnamespacestd;

classMyPoint(〃表示平面坐标系中的点的类

doublex;

doubley;

public:

MyPoint(doublex,doubley){this->x=x;this->y=y;}

doublegetX()const{returnx;}

doublegetY()const{returny;}

voidshow()const{cout«,(*«x«*,>;}

);

classMyTrianglef〃表示三角形的类

MyPointpointl;〃三角形的第一个顶点

MyPointpoint2;〃三角形的第二个顶点

MyPointpoint3;〃三角形的第三个顶点

public:

MyTriangle(MyPointpl,MyPointp2,MyPointp3);

doubleperimeter()const;〃返回三角形的周长

doublearea()const;〃返回三角形的面积

);

//**1************fQund**********

MyTriangle::MyTriangle(MyPointpl,MyPointp2,MyPointp3):point1(pl),point2(p2),point3(p3){)

doubledistance(MyPointpl,MyPointp2)〃返回两点之间的距离

(

returnsqrt((pl.getX()-p2.getX())*(pl.getX()-p2.getX())+

(pl.getY()-p2.getY())*(pl.getY()-p2.getY()));

)

//**2************found**********

doubleMyTriangle::perimeter()const

(

returndistance(pointl,point2)+distance(point2,point3)+distance(point3,pointl);

)

doubleMyTriangle::area()const

{/**********Found**********

doubles=perimeter0/2;//使用perimeter函数

returnsqrt(s*(s-distance(pointl,poin⑵)*

(s-distance(point2,point3))*

(s-distance(point3,pointl)));

)

intmain()

(

MyTriangletri(MyPoint(0,2),MyPoint(2,0),MyPoint(0,0));

cout<<tri.perimeter()<<endl<<tri.area()<<endl;

return0;

)

第三套

请使用VC6打开考生文件夹下的工程projl,”工程含有一个源程序文件projl.cpp.其中位于每个注释"〃ERROR

************found************〃之后的一行语句存在错误。请修正这些错误,使程序的输出结果为:

Thereare2object(s).

注意:只需要修改注释〃〃ERROR*******found*************”的下一行语句,不要改动程序U」的其它内容。

//projl.cpp

include<iostream>

usingnamespacestd;

classMyClass{

public:

〃ERROR**********found**********

MyClass(inti=0):value(i)

{count++;}

voidPrint()

{cout«"Thereare"«count«"object(s)."«endl;}

private:

constintvalue;

staticintcount;

};

11ERROR**********found**********

intMyClass::count=0;

intmain()

MyClassobjl,obj2;

〃ERROR**********found**********

objl.Print();

return0;

}

请使用VC6打开考生文件夹下的工程proj2,该工程含有一个源程序文件proj2.cpp。其中定义了模板函数insert(T

dataset[],int&size,Titem)和主函数main。模板函数insert用来将一个数据item插入到一个已排好序(升序)的数据集

dataset中,其中类型T可以为int,double,char等数据类型,size为当前数据集中元素的个数,当插入操作完成后,size

值将更新。模板函数insert的部分实现代码已在文件proj2.cpp中给出,请在标有注释"〃TODO:〃的行中添加适当的代码,将

运算符函数的operator-补充完整,以实现其功能。

注意:只需在指定位置编写适当代码,不要改动程序中的其它内容,也不能删除或移动

”//**********found******************“o

//proj2.cpp

include<iostream>

usingnamespacestd;

〃请在该部分插入insert函数模板的实现

template<typenameT>

voidinsert(Tsetdata[],int&size,Titem)

for(inti=0;i<size;i++)

〃**********found**********

if(setdata[i]>item){//TODO:添加代码,判断查找元素的插入位置

for(intj=i;j<size;j++)

//**********found**********

setdata[size-j+i]=setdata[size-j+i-l];//TODO:添加一条语句,将插入位置后的所有元素往

后移动一个位置

//提示:移动元素应从最后一个元素开始移动

setdata[i]=item;//插入该元素

size++;

return;

}

//**********found**********

setdata[i]=item;//TODO:添加一条语句,将元素加到最后一个位置上

size++;

return;

intmain()

(

intidata[10]={22,35,56,128},iitem,isize=4,dsize=4,i;

doubleddata[10]={25.1,33.5,48.9,75.3},ditem;

cout«"Pleaseinputoneintegernumberforinserting:";

cin»iitem;

insert(idata,isize,iitem);

for(i=0;i<isize;i++)

cout«idata[i]«'

cout«endl;

cout«"Pleaseinputonedoublenumberforinserting:";

cin»ditem;

insertfddata,dsize,ditem);

for(i=0;i<dsize;i++)

cout«ddata[i]«'

cout«endl;

return0;

请使用VC6打开考生目录下的工程文件proj3。此工程包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标中的点的

类MyPoint和表示线段的类MyLine;程序应当显示:

(0,0)(1,1)

1.41421,1

但程序中的缺失部分,清按下面的提示,把下划线标出的三处缺失部分补充完整:

(1)在〃**1*************found***********的下方是构造函数的定义,它用参数提供两个端点对poin11和poin12进行

初始化。

(2)在〃**2**************found***********的下方是成员函数1ength的定义,它返回线段的长度。

(3)在〃**3**************found***********的卜方是成员函数slope的定义中的一条语句。

函数slope返回线段的斜率,方法是:若线段的两个端点分别是(xl,yl)和(x2,y2),则斜率k为:k=(y2-yl)/(x2-xl)

注意:只需在指定位置编写适当代码,不要改动程序中的其它内容,也不能删除或移动

"〃**************found*************“o

//proj3.cpp

#include<iostream>

#include<cmath>

usingnamespacestd;

classMyPoint{〃表示平面坐标系中的点的类

doublex;

doubley;

public:

MyPoint(doublex,doubley){this->x=x;this->y=y;}

doublegetX()const{returnx;}

doublegetY()const{returny;}

,,,

voidshow()const{cout«(«x«'/«y«')';}

);

classMyLine{〃表示线段的类

MyPointpointl;

MyPointpoint2;

public:

MyLine(MyPointpl,MyPointp2);

MyPointendPointl()const{returnpointl;}〃返回端点1

MyPointendPoint2()const{returnpoint!;}〃返回端点2

doublelength()const;〃返回线段的长度

doubleslope()const;〃返回直线的斜率

);

〃**]************found**********

MyLine::MyLine(MyPointpl,MyPointp2):pointl(pl),point2(p2){}

Iy**2************^QU|^J**********

doubleMyLine::length()const

returnsqrt((pointl.getX()-point2.getX())*(pointl.getX()-point2.getX())+

(pointl.getY()-point2.getY())*(pointl.getY()-point2.getY()));

doubleMyLine::slope()const

{〃**3************found**********

return(point2.getY()-pointl.getY())/(point2.getX()-pointl.getX());

intmain()

MyLineline(MyPoint(0,0),MyPoint(l,l));

line.endPointl().show();

line.endPoint2().show();

cout«endl«line.length()«7«line.slope()«endl;

return0;

)

第四套

请使用〃答题〃菜单或使用VC6打开考生文件夹下的工程projl,该工程含有一个源程序文件projl.cppo其中每个注释

"〃ERROR********found********"之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:

Thevalueis10

注意:只需修改注释"/ERROR********found********”的下一行语句,不要改动程序中的其他内容。

//projl.cpp

#include<iostream>

usingnamespacestd;

classMyClass{

intvalue;

public:

11ERROR********found********

MyClass(intval):value(val){}

intGetValue()const{returnvalue;}

voidSetValue(intval);

);

//ERROR********found********

inlinevoidMyClass::SetValue(intval){value=val;}

intmain()

MyClassobj(O);

obj.SetValue(lO);

下列语句功能是输出的成员的值

//ERROR********fo11nd********objvalue

cout«"Thevalueis"«obj.GetValue()«endl;

return0;

}

请使用〃答题〃菜单或使用VC6打开考生文件夹下的工程文件proj2,该工程含有一个源程序文件proj2.cpp,其中定义了

CharShape类、Triangle类和Rectangle类。

CharShape是个抽象基类,它表示由字符组成的图形(简称字符图形),纯虚函数Show用作显示不同字符图形的相同操

作接口。Triangle和Rectangle是CharShape的派生类,它们分别用于表示字符三角形和字符矩形,并且都定义了成员函数Show,

用于实现各自的显示操作。本程序的正确输出结果为:

*

***

########

########

########

请先阅读程序,分析输出结果,然后根据下列要求在横线处填写适当的代码并删除横线。

(1)将Triangle类的成员函数Show补充完整,使字符三角形的显示符合输出结果。

(2)将Rectangle类的成员函数Show补充完整,使字符矩形的显示符合输出结果。

(3)为类外函数fun添加合适的形参。

注意:只需在指定位置编写适当的代码,不要改动程序中其它内容,也不能删除或移动"〃***********found**********"。

//proj2.cpp

include<iostream>

usingnamespacestd;

classCharShape{

public:

CharShape(charch):_ch(ch){};

virtualvoidShow()=0;

protected:

char_ch;//组成图形的字符

};

classTriangle:publicCharShape{

public:

Triangle(charch,intr):CharShape(ch),_rows(r){}

voidShow();

private:

int_rows;//行数

);

classRectangle:publicCharShape{

public:

Rectangle(charch,intr,intc):CharShape(ch),_rows(r),_cols(c){}

voidShow();

private:

int_rows,_cols;//行数和列数

);

voidTriangle::Show()//输出字符组成的三角形

(

for(inti=1;i<=_rows;i++){

〃********found********

for(intj=1;j<=i+i-1;j++)

cout«_ch;

cout«endl;

voidRectangle::Show()//输出字符组成的矩形

〃********found********

for(inti=1;i<=_rows;i++){

〃********found********

for(intj=1;j<=_cols;j++)

cout«_ch;

cout«endl;

//♦*******found********为fun函数添加形参

voidfun(CharShape&cs){cs.Show();}

intmain()

TriangletriC**,4);

Rectanglerectf#1,3,8);

fun(tri);

fun(rect);

return0;

)

请使用〃答题〃菜单或使用VC6打开考生文件夹下的工程文件proj3,此工程包含一个源程序文件proj3.cpp,补充编制C++

程序proj3.cpp,其功能是从文本文件in.dat中读取全部内容,将文本存放到doc类的对象myDoc中。然后分别统计26个英文字

母在文本中出现的次数,统计时不区分大小写。最后将统计结果输出到文件out.dat中。文件in.dat长度不超过1000字节。

要求:

补充编制的内容写在//***************************333***********与//*********************666**************两

行之间。实现分别统计26个英文字母在文本中出现的次数,并将统计结果在屏幕输出,格式不限。不得修改程序的其它部分。

注意:程序最后已经将结果输出到文件oul.dat中。输出函数writeToFile已经给出并且调用。

//proj3.cpp

#include<iostream>

#include<fstream>

#include<cstring>

usingnamespacestd;

classdoc

(

private:

char*str;〃文本字符串首地址

intcounter[26];〃用于存放26个字母的出现次数

intlength;〃文本字符个数

public:

〃构造函数,读取文件内容,用于初始化新对象。filename是文件名字符串首地址。

doc(char*filename);

voidcount();〃统计26个英文字母在文本中出现的次数,统计时不区分大小写。

~doc();

voidwriteToFile(char.filename);

);

doc::doc(char*filename)

(

ifstreammyFile(filename);

intlen=1001,tmp;

str=newchar[len];

length=0;

while((tmp=myFile.get())!=EOF)

(

str[length++]=tmp;

}

str[length]='\O,;

myFile.close();

for(inti=0;i<26;i++)

counter[i]=0;

voiddoc::count()

(

char*s=str;intindex;

while(*s)

(

if(*s>=,a'&&*s<='z'||*s>=,A'&&*s<='Z')

(

index=*s>='a'?*s-'a':*s-'A';

counter[index]++;

)

s++;

}

for(inti=0;i<26;i++)

cout«(char)('a'+i)«":"«counter[i]«"

y/**********ggg*********

doc::~doc()

(

delete[]str;

}

voiddoc::writeToFile(char*filename)

(

ofstreamoutFile(filename);

for(inti=0;i<26;i++)

outFile«counter[i]«endl;

outFile.close();

)

voidmain()

(

docmyDoc(nin.dat");

myDoc.count();

myDoc.writeToFile("out.dat");

)

第五套

请使用"答题”菜单或使用VC6打开考生文件夹下的工程projl,该工程含有个源程序文件projl.cpp。其中位于每个注释

"〃ERROR**********found**********"之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:

(4,4)

注意:只需修改注释〃〃ERROR**********found**********”的下,行语句,不要改动程序中的其他内容。

//projl.cpp

include<iostream>

usingnamespacestd;

classPoint{

public:

//ERROR********found********

Point(doublex,doubley):_x(x),_y(y){}

doubleGetX()const{return_x;}

doubleGetY()const{return_y;}

//ERROR********found********

voidMove(doublexOff,doubleyOff)

{_x+=xOff;_y+=yOff;}

protected:

double_x,_y;

);

intmain()

(

Pointpt(l.5,2.5);

pt.Move(2.5,1.5);

//ERROR********found********以下语句输出pt成员_x和_y的值

cout«'('«pt.GetX()«'/«pt.GetY()««endl;

return0;

}

请使用”答题〃菜单或使用VC6打开考生文件夹下的工程文件proj2,该工程含有•个源程序文件proj2.cpp,其中定义了

Array类。

在C++程序中访问数组时,如果索引值(下标)小于0或者大于元素个数减1,就会产生越界访问错误。Array是一个带有

检查越界访问功能的数组类,它的成员列表如下所示:

公有成员函数功能

GetValue获取指定元素的值

SetValue将指定元素设置为指定值

GetLength获取元素个数

私有成员函数功能

IsOutOfRange检查索引是否越界

私有数据成员功能

_P指向动态分配的整型数组的指针

_size存放元素个数

Array类的构造函数会动态分配,个int类型数组,以存储给定数量的元素。在公有成员函数GetValue和SetValue中,首

先调用私有成员函数IsOutOfRange检查用于访问数组元素的索引是否越界,只有当索引值在有效范围时,才能进行元素访问

操作。

请在横线处填写适当的代码并删除横线以实现Array类功能。此程序的正确输出结果为:

1,2,3,4,5,6,7,8,9,10

注意:只需在指定位置编写适当的代码,不要改动程序中其它内容,也不能删除或移动

”//***********found**********"0

//proj2.cpp

include<iostream>

usingnamespacestd;

classArray{

public:

Array(intsize)//构造函数

(

〃********found********下列语句动态分配一个int类型数组

_p=newint[size];

_size=size;

}一

^ArrayO{delete[]_p;}//析构函数

voidSetValue(intindex,intvalue)//设置指定元素的值

if(IsOutOfRange(index)){

cerr«"Indexoutofrange!"«endl;

return;

〃********found********

_p[index]=value;

}一

intGetValue(intindex)const//获取指定元素的值

(

if(IsOutOfRange(index)){

cerr«"Indexoutofrange!"«endl;

return-1;

I/********found********

return_p[index];

intGetLength()const{return_size;}//获取元素个数

private:

int*_p;

int_size;

boolIsOutOfRangefintindex)const//检查索引是否越界

〃********found********

if(index<011index>=_size)

returntrue;

elsereturnfalse;

)

);

intmain()

(

Arraya(10);

for(inti=0;i<a.GetLength();i++)

a.SetValue(i,i+1);

for(intj=0;j<a.GetLength()-l;j++)

cout«a.GetValue(j)«“,";

cout«a.GetValue(a.GetLength()-l)«endl;

return0;

)

请使用”答题〃菜单或使用VC6打开考生文件夹下的工程文件proj3,此工程包含一个源程序文件proj3.cpp,其中定义了用

于表示平面坐标系中点的类MyPoint和表示矩形的类MyRectangle;程序应当显示:

(0,2)(2,2)(2,0)(0,0)4

但程序有缺失部分,请按卜.面的提示,把卜.划线标出的三处缺失部分补充完整:

(1)在〃**1***************found***********的下方是构造函数的定义,它用参数提供的左上角和右下角坐标对

upleft和downright进行初始化。

(2)在〃**2****************found***********的卜方是成员函数gelDownLefl的定义中的一条语句。函数gelDownLeft

返回用MyPoint对象表示的矩形的左下角。

(3)在〃**3*****************found***********的下方是成员函数area的定义,它返回矩形的面积。

注意:只需在指定位置编写适当的代码,不要改动程序中的其它内容,也不能删除或移动

"*************-Found***************"。

//proj3.cpp

#include<iostream>

usingnamespacestd;

classMyPoint{〃表示平面坐标系中的点的类

doublex;

doubley;

public:

MyPoint(doublex,doubley){this->x=x;this->y=y;}

doublegetX()const{returnx;}

doublegetY()const{returny;}

,,

voidshow()const{cout«(«x«'/'«y«')';}

);

classMyRectangle{〃表示矩形的类

MyPointup_left;〃矩形的左上角顶点

MyPointdown_right;〃矩形的右下角顶点

public:

MyRectangle(MyPointupleft,MyPointdownright);

MyPointgetUpLeft()const{returnup_left;}〃返回左上角坐标

MyPointgetDownRight()const{returndown_right;}〃返回右下角坐标

MyPointgetUpRight()const;〃返回右上角坐标

MyPointgetDownLeft()const;〃返回左下角坐标

doublearea()const;〃返回矩形的面积

);

〃**]************found**********

MyRectangle::MyRectangle(MyPointpl,MyPointp2):

up_left(pl),down」ight(p2){}

MyPointMyRectangle::getl)pRight()const

returnMyPoint(down_right.getX(),up_left.getY());

}一一

MyPointMyRectangle::getDownLeft()const

{〃**2************found**********

returnMyPointfupJeft.getXt^down.right.getYO);

)

〃**3************f()und**********

doubleIVIyRectangle::area()const

return(getUpLeft().getX()-getDownRight(),getX())*

(getDownRight().getY()-getUpLeft().getY());

intmain()

(

MyRectangler(MyPoint(0,2),MyPoint(2,0));

r.getUpLeft().show();

r.getUpRight().show();

r.getDownRight().show();

r.getDownLeft().show();

cout«r.area()«endl;

return0;

第六套

请使用"答题”菜单或使用VC6打开考生文件夹下的工程projl,该工程含有一个源程序文件projl.cpp。其中位于每个注释

"〃ERROR**********found**********〃之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:

False

注意:请勿更改参数名。只需修改注释〃〃ERROR********found********〃的下一行语句,不要改动程序中的其他

内容。

#include<iostream>

usingnamespacestd;

classMyClass

(

public:

//ERROR********found********请勿更改参数名

MyClass(intx):flag(x){}

voidJudge();

private:

intflag;

);

11ERROR********found********

voidMyClass::Judge()

(

switch(flag)

(

case0:

cout«nFalse"«endl;

//ERROR********found********

break;

default:

cout«"True"«endl;

break;

intmain()

(

MyClassobj(O);

obj.Judge();

return0;

)

请使用”答题〃菜单或使用VC6打开考生文件夹下的工程文件proj2,该工程含有一个源程序文件proj2.cpp,其中定义了

Stack类和ArrayStack类。

Stack类是•个用于表示数据结构〃栈〃的类,栈中的元素是字符型数据。Stack为抽象类,它只定义了栈的用户接口,如

下所示:

公有成员函数功能

push入栈:在栈顶位置添加一个元素

pop退栈:取出并返回栈顶元素

ArrayStack是Stack类的派生类,它实现了Stack定义的接口□ArrayStack内部使用动态分配的字符数组作为栈元素的存

储空间。数据成员maxSize表示栈的最大容量,top记录栈顶的位置。成员函数push和pop分别实现具体的入栈和退栈操作。

请在横线处填写适当的代码并删除横线以实现上述功能。此程序的正确输出结果为:

a,b,c

c,b,a

注意:只需在指定位置编写适当的代码,不要改动程序中其它内容,也不能删除或移动〃〃***********found**********〃。

//proj2.cpp

include<iostream>

usingnamespacestd;

classStack{

public:

virtualvoidpush(charc)=0;

virtualcharpop()=0;

classArrayStack:publicStack{

char*p;

intmaxSize;

inttop;

public:

ArrayStack(ints)

top=0;

maxSize=s;

I/********found********

p=newchar[maxSize];

}

~ArrayStack()

(

〃********found********

delete[]p;

)

voidpush(charc)

(

if(top==maxSize){

cerr«"Overflowin'';

return;

〃********found********

p[top]=c;

top++;

)

charpop()

(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论