C++全国计算机二级真题(含上机题、答案).docx
《C++全国计算机二级真题(含上机题、答案).docx》由会员分享,可在线阅读,更多相关《C++全国计算机二级真题(含上机题、答案).docx(208页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、VC+全国计算机二级题库 第一套 请使用VC6打开考生文件夹下的工程projl,该工程含有个源程序文件projl.cpp。其中每个注释ERROR *found*之后的,行有语句存在错误。请修改这些错误,使程序的输出结果为:123456789 10注意:只需修改注释 ERROR *found*的下1行语句,、要改动程序中的其内容。/projl.cpp#include using namespace std;class MyClass public:MyClass(int len)array = new intflen;arraySize = len;for(int i = 0; i arrayS
2、ize; i+) arrayi = i+1;MyClass()11 ERROR* *Q|jp|* *delete array;void Print() constfor(int i = 0; i arraySize; i+)11 ERRORj* *cout arrayi cout endl;private:int * array;int arraySize;; int main()IJ error *found* MyClass obj(10);obj.Print(); return 0;|请使用VC6打开考生文件夹下的工程proj2,该工程含有一个源程序文件proj2. cpp0其中定义了类
3、Bag和用于测试该类的主函 数main。类Bag是一个袋子类,用来存放带有数字标号的小球(如台球中的球,在类中用个整数值表示个小球),其中运 算符成员函数=用来判断两个袋子对象是否相同(即小球的个数相同,每种小球数目也相同,但与它们的存储顺序无关); 成员函数int InBag(int ball)用来返回小球ball在当前袋子内出现的次数,返回0表示该小球不存在。为类实现这两个函数, 其用法可参见主函数main。运算符函数operator =中首先判断两个袋子内的小球个数是否相同,再调用InBag函数来判断每种小球在两个袋子内是 否具有相同的出现次数。注意:只需在指定位置编写适当代码,不要改动
4、程序中的其他内容,也不能删除或移动 /*1ound*”0/ proj2.cpp#include using namespace std;const int MAXNUM = 100;class Bag private:int num;int bagMAXNUM;public:构造函数Bag(int m,int n=0);bool operator = (Bag &b); /Z 垂载运算符int lnBag(int ball); /Z某小球在袋子内的出现次数,返回表示不存在 ;Bag:Bag(int m, int n)if(n MAXNUM) cerr Too many membersn; ex
5、it(-l);for(int i 二 ; i n; i+)bagi = mi;num 二 n;)bool Bag:operator = (Bag &b) /Z 实现运算符函数if (num != b.num) /Z元素个数不同return false;for (int i = 0; i num; i+)*found*if (lnBag(bagi)!二b.lnBag(bagi) TOD。:加入条件,判断当前袋子中每个元素在当前袋子和袋子 b中是否出现次数不同/* f und*return false; /TODO:加入一条语句return true;int Bag:lnBag(int ball)
6、int count 二 ;for (int i = 0; i num; i+)*found*if (bagi=ball)/TODO:加入条件,判断小球ball是否与当前袋子中某一元素相同 *found*count+; /TODO:加入一条语句return count;int main()int dataMAXNUM, n, i;cin n;for (i = 0; i n; i+) cin datai;Bag bl(data, n); cin n;for (i = 0; i n; i+) cin datai;Bag b2(data, n);if( bl = b2)/创建袋子对象bl创建袋子对象b
7、2测试b!和b2是否相同cout Bag bl is same with Bag b2n”; elsecout Bag bl is not same with Bag b2n;return 0;请使用VC6打开考生目录下的工程文件proj3。此工程包含一个源程序文件proj3. cpp,其中定义了用于表示二维向量的类 MyVector:程序应当显示:(6,8)但程序中有缺失部分,请按下面的提示,把下划线标出的三处缺失部分补充完整:(1)在*1* *found*的下方是构造函数的定义,它用参数提供的坐标对x和y进行初始化。(2)在*2* *found*的下方是减法运算符函数定义中的一条语句。两个
8、二维向量相减生成另 一个二维向量:它的X坐标等于两个向量X的坐标之差,它的Y坐标等于两个向量Y坐标之差。(3)在*3* *found*的下方的语句的功能是使变量v3获得新值,它等于向量vl与向量v2之 和。注意:只需在指定位置编写适当的代码,不要改动程序中的其它内容,也不能删除或移动 *f ound*/ proj3. cpp#includeusing std:ostream;using std:cout;using std:endl;class MyVector(表示二维向量的类double x;X坐标值double yiY坐标值public:MyVector (double i=0. 0 ,
9、 double j=0. 0)J构造函数MyVector operator+( MyVector j)i重载运算符+friend MyVector operator-( MyVector i, MyVector j)1重载运算符friend ostream& operator ( ostream& os, MyVector v): 重载运算符 ;/*1* *found*MyVector:MyVector(double i , double j): x(i),y(j) MyVector MyVector:operator+( MyVector j) return MyVector(x+j. x,
10、 y+j.y);MyVector operator-( MyVector i, MyVector j)/*2* *found* return MyVector (i. x-j. x, i. y-j. y);ostream& operator( ostream& os, MyVector v) os ( v. x , v. y ):输出向量 v 的坐标return os;int mainO(MyVector vl (2,3), v2(4,5), v3;/*3* *found*v3=vl+v2;coutv3endl; return 0;第二套请使用VC6打开考生文件夹的工程projl,该工程含有一
11、个源程序文件projl.cpp.其中位于每个注释ERROR *found*之后的行语句存在错误。请修正这些错误,使程序的输出结果为: Constructor called of 10 The value is 10 Descructor cal led of 10注意:只需要修改注释 ERROR * found*的一行语句,不要改动程序中的其它内容。/ projl. cpp ttinclude using namespace std;class MyClass public:MyClass(int i)(value = i;cout *Constructor cal led of value
12、endl;)/ ERROR *found* void Print 0 const cout The value is value endl; / ERROR *found*MyClass () cout Destructor called of ” value endl; private:/ ERROR *found* int value;1;int main()(const MyClass obj(10);obj. Print ();return 0;凡用过C语言标准库函数strcpy(char*sl,char*s2)的程序员都知道使用该函数时有一个安全隐患,即当指针si所指向的空间 不能容
13、纳字符串s2的内容时,将发生内存错误。类String的Strcpy成员函数能进行简单的动态内存管理,其内存管理策略为:(1)若已有空间能容纳下新字符串,则直接进行字符串拷贝:(2)若已有空间不够时,它将重新申请块内存空间(能容纳新字符串),并将新字符串内容拷贝到新申请的空间中, 释放原字符空间。请使用VC6打开考生文件夹下的工程proj2,该工程含有一个源程序文件proj2. cpp。其中定义了类String和用于测试该类 的主函数main,并且成员函数Strcpy的部分实现代码已在该文件中给出,请在标有注释*found*行的 下行添加适当的代码,将这个函数补充完整,以实现其功能。注意:只需在
14、指定位置编写适当代码,不要改动程序中的其它内容,也不能删除或移动/*found*/ proj2. cppinclude using namespace std;class String private:int size;/Z缓冲区大小char *buf;/Z 缓冲区public:String(int bufsize);void Strcpy (char *s);/ 将字符串s复制到buf中void Print() const;String0 if (buf != NULL) delete buf; ;String:String(int bufsize)(size = bufsi ze;buf
15、= new charsize;*buf =,、;)void String:Strcpy(char *s)(char *p, *q ;int len = strlen(s);if (len+1 size) size = len+1;p = q = new charsize;/*found*while(*(q+)=*(s+); / TODO:添加代码将字符串s拷贝到字符指针q中delete buf;buf = p;else /*found*for(p=buf ;*p二s;p+, s+); / TODO:添加代码将字符串s拷贝至buf中void String:Print() const(cout s
16、ize t buf endl;int main()(char s100;String str(32);cin. getline(s, 99);str. Strcpy (s);str. Print ();return 0;)请使用VC6打开考生目录下的工程文件proj3。此工程包含一个源程序文件proj3. cpp,其中定义了用于表示平面坐标系中 的点的类myPoinl和表示三角形的类MyTriangle;程序运行后应当显示:6. 828432但程序中的缺失部分,请按面的提示,把卜划线标出的三处缺失部分补充完整:(1)在*1* *found*的下方是构造函数的定义,它参数提供的三个顶点对poin
17、t 1、point2和 point3进行初始化。(2)在*2* * found*的方是成员函数perimeter的定义,该函数返回三角形的周氏。(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*”/ proj3. cpp#includeiostream#includecmathus
18、ing namespace std;class MyPoint表示平面坐标系中的点的类double x;double y;public:MyPoint (double x, double y)this-x=x;this-y=y; double getXOconst( return x; double getY()const return y;void show () const cout ( x ,y );1;class MyTriangleMyPointMyPointMyPoint public:pointl;point2;point3;表示三角形的类三角形的第一个顶点三角形的第二个顶点三角
19、形的第三个顶点MyTriangle(MyPoint pl, MyPoint p2, MyPoint p3);返回三角形的周长double perimeter 0 const;double area()const:返回三角形的面积;/*1* *found*MyTriangle:MyTriangle(MyPoint pl, MyPoint p2, MyPoint p3):point 1(pl), point2(p2), point3(p3)( )double distance(MyPoint pl, MyPoint p2)返回两点之间的距离(return sqrt (pl. getXO-p2. g
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 全国计算机 二级 上机 答案
限制150内