资源描述
第一套
请使用VC6打开考生文件夹下的工程proj1,该工程含有一个源程序文件proj1.cpp。其中每个注释"//ERROR ***********found*************"之后的一行有语句存在错误。请修改这些错误,使程序的输出结果为:1 2 3 4 5 6 7 8 9 10
注意:只需修改注释"//ERROR ***********found**********"的下一行语句,不要改动程序中的其它内容。
// proj1.cpp
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int len)
{
array = new int[len];
arraySize = len;
for(int i = 0; i < arraySize; i++)
array[i] = i+1;
}
~MyClass()
{
// ERROR **********found**********
delete []array;
}
void Print() const
{
for(int i = 0; i < arraySize; i++)
// ERROR **********found**********
cout << array[i] << ' ';
cout << endl;
}
private:
int *array;
int arraySize;
};
int main()
{
// ERROR **********found**********
MyClass obj(10);
obj.Print();
return 0;
}
请使用VC6打开考生文件夹下的工程proj2,该工程含有一个源程序文件proj2.cpp。其中定义了类Bag和用于测试该类的主函数main。类Bag是一个袋子类,用来存放带有数字标号的小球(如台球中的球,在类中用一个整数值表示一个小球),其中运算符成员函数==用来判断两个袋子对象是否相同(即小球的个数相同,每种小球数目也相同,但与它们的存储顺序无关);成员函数int InBag(int ball)用来返回小球ball在当前袋子内出现的次数,返回0表示该小球不存在。为类实现这两个函数,其用法可参见主函数main。
运算符函数operator ==中首先判断两个袋子内的小球个数是否相同,再调用InBag函数来判断每种小球在两个袋子内是否具有相同的出现次数。
注意:只需在指定位置编写适当代码,不要改动程序中的其他内容,也不能删除或移动"//**********found******************"。
// proj2.cpp
#include <iostream>
using namespace std;
const int MAXNUM = 100;
class Bag {
private:
int num;
int bag[MAXNUM];
public:
Bag(int m[], int n=0); // 构造函数
bool operator == (Bag &b); // 重载运算符==
int InBag(int ball); // 某一小球在袋子内的出现次数,返回0表示不存在
};
Bag::Bag(int m[], int n)
{
if(n > MAXNUM) {
cerr << "Too many members\n";
exit(-1);
}
for(int i = 0; i < n; i++)
bag[i] = m[i];
num = n;
}
bool Bag::operator == (Bag &b) // 实现运算符函数==
{
if (num != b.num) // 元素个数不同
return false;
for (int i = 0; i < num; i++)
//**********found**********
if (InBag(bag[i])!=b.InBag(bag[i])) // TODO: 加入条件, 判断当前袋子中每个元素在当前袋子和袋子b中是否出现次数不同
//**********found**********
return false; // TODO: 加入一条语句
return true;
}
int Bag::InBag(int ball)
{
int count = 0;
for (int i = 0; i < num; i++)
//**********found**********
if (bag[i]==ball) // TODO: 加入条件, 判断小球ball是否与当前袋子中某一元素相同
//**********found**********
count++ ; // TODO: 加入一条语句
return count;
}
int main()
{
int data[MAXNUM], n, i;
cin >> n;
for (i = 0; i < n; i++)
cin >> data[i];
Bag b1(data, n); // 创建袋子对象b1
cin >> n;
for (i = 0; i < n; i++)
cin >> data[i];
Bag b2(data, n); // 创建袋子对象b2
if( b1 == b2) // 测试b1和b2是否相同
cout << "Bag b1 is same with Bag b2\n";
else
cout << "Bag b1 is not same with Bag b2\n";
return 0;
}
请使用VC6打开考生目录下的工程文件proj3。此工程包含一个源程序文件proj3.cpp,其中定义了用于表示二维向量的类MyVector;程序应当显示:(6,8)
但程序中有缺失部分,请按下面的提示,把下划线标出的三处缺失部分补充完整:
(1)在//**1** *************found***********的下方是构造函数的定义,它用参数提供的坐标对x和y进行初始化。
(2)在//**2** **************found***********的下方是减法运算符函数定义中的一条语句。两个二维向量相减生成另一个二维向量:它的X坐标等于两个向量X的坐标之差,它的Y坐标等于两个向量Y坐标之差。
(3)在//**3** ***************found***********的下方的语句的功能是使变量v3获得新值,它等于向量v1与向量v2之和。
注意:只需在指定位置编写适当的代码,不要改动程序中的其它内容,也不能删除或移动"*************found***************"。
// proj3.cpp
#include<iostream>
using std::ostream;
using std::cout;
using std::endl;
class MyVector{ //表示二维向量的类
double x; //X坐标值
double y; //Y坐标值
public:
MyVector(double i=0.0 , double j=0.0); //构造函数
MyVector operator+( MyVector j); //重载运算符+
friend MyVector operator-( MyVector i, MyVector j); //重载运算符-
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, 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 main()
{
MyVector v1(2,3), v2(4,5), v3;
//**3** **********found**********
v3=v1+v2;
cout<<v3<<endl;
return 0;
}
第二套
请使用VC6打开考生文件夹下的工程proj1,该工程含有一个源程序文件proj1.cpp。其中位于每个注释"//ERROR ************found***************"之后的一行语句存在错误。请修正这些错误,使程序的输出结果为:
Constructor called of 10
The value is 10
Descructor called of 10
注意:只需要修改注释"//ERROR *******found***************"的下一行语句,不要改动程序中的其它内容。
// proj1.cpp
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int i)
{
value = i;
cout << "Constructor called of " << value << endl;
}
// ERROR **********found**********
void Print() const
{ cout << "The value is " << value << endl; }
// ERROR **********found**********
~MyClass()
{ cout << "Destructor called of " << value << endl; }
private:
// ERROR **********found**********
int value;
};
int main()
{
const MyClass obj(10);
obj.Print();
return 0;
}
凡用过C语言标准库函数strcpy(char*s1,char*s2)的程序员都知道使用该函数时有一个安全隐患,即当指针s1所指向的空间不能容纳字符串s2的内容时,将发生内存错误。类String的Strcpy成员函数能进行简单的动态内存管理,其内存管理策略为:
(1)若已有空间能容纳下新字符串,则直接进行字符串拷贝;
(2)若已有空间不够时,它将重新申请一块内存空间(能容纳下新字符串),并将新字符串内容拷贝到新申请的空间中,释放原字符空间。
请使用VC6打开考生文件夹下的工程proj2,该工程含有一个源程序文件proj2.cpp。其中定义了类String和用于测试该类的主函数main,并且成员函数Strcpy的部分实现代码已在该文件中给出,请在标有注释"//**********found**********"行的下一行添加适当的代码,将这个函数补充完整,以实现其功能。
注意:只需在指定位置编写适当代码,不要改动程序中的其它内容,也不能删除或移动"//**********found******************"。
// proj2.cpp
#include <iostream>
using namespace std;
class String {
private:
int size; // 缓冲区大小
char *buf; // 缓冲区
public:
String(int bufsize);
void Strcpy(char *s); // 将字符串s复制到buf中
void Print() const;
~String() { if (buf != NULL) delete buf; }
};
String::String(int bufsize)
{
size = bufsize;
buf = new char[size];
*buf = '\0';
}
void String::Strcpy(char *s)
{
char *p,*q;
int len = strlen(s);
if (len+1 > size) {
size = len+1;
p = q = new char[size];
//**********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 << size << '\t' << buf << endl;
}
int main()
{
char s[100];
String str(32);
cin.getline(s, 99);
str.Strcpy(s);
str.Print();
return 0;
}
请使用VC6打开考生目录下的工程文件proj3。此工程包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中的点的类myPoint和表示三角形的类MyTriangle;程序运行后应当显示:
6.82843
2
但程序中的缺失部分,请按下面的提示,把下划线标出的三处缺失部分补充完整:
(1)在//**1** ***********found***********的下方是构造函数的定义,它参数提供的三个顶点对point1、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))(其中sqrt表示开二次方)
注意:只需在指定位置编写适当代码,不要改动程序中的其它内容,也不能删除或移动"//**************found*************"。
// proj3.cpp
#include<iostream>
#include<cmath>
using namespace std;
class MyPoint{ //表示平面坐标系中的点的类
double x;
double y;
public:
MyPoint (double x,double y){this->x=x;this->y=y;}
double getX()const{ return x;}
double getY()const{ return y;}
void show()const{ cout<<'('<<x<<','<<y<<')';}
};
class MyTriangle{ //表示三角形的类
MyPoint point1; //三角形的第一个顶点
MyPoint point2; //三角形的第二个顶点
MyPoint point3; //三角形的第三个顶点
public:
MyTriangle(MyPoint p1,MyPoint p2,MyPoint p3);
double perimeter()const; //返回三角形的周长
double area()const; //返回三角形的面积
};
//**1** **********found**********
MyTriangle::MyTriangle(MyPoint p1,MyPoint p2,MyPoint p3):point1(p1),point2(p2),point3(p3){ }
double distance(MyPoint p1,MyPoint p2) //返回两点之间的距离
{
return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+
(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
}
//**2** **********found**********
double MyTriangle::perimeter()const
{
return distance(point1,point2)+distance(point2,point3)+distance(point3,point1);
}
double MyTriangle::area()const
{//**3** **********found**********
double s=perimeter()/2; // 使用perimeter函数
return sqrt(s*(s-distance(point1,point2))*
(s-distance(point2,point3))*
(s-distance(point3,point1)));
}
int main( )
{
MyTriangle tri(MyPoint(0,2),MyPoint(2,0),MyPoint(0,0));
cout<<tri.perimeter()<<endl<<tri.area()<<endl;
return 0;
}
9
展开阅读全文