2012全国计算机二级C++上机 完整题库 第一套 请使用VC6打开考生文件夹下的工程proj1:该工程含有一个源程序文件proj1.cpp。其中每个注释'//ERROR ***********found*************'之后的一行有语句存在错误。请修改这些错误:使程序的输出结果为~1 2 3 4 5 6 7 8 9 10 // proj1.cpp #include 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[]; // delete []array; }
void Print() const { for(int i = 0; i < arraySize; i++) // ERROR **********found********** cin << array[i] << ' '; // cout << array[i] << ' ';
cout << endl; } private: int *array; int arraySize; };
int main() { // ERROR **********found********** MyClass obj; //MyClass obj(10); obj.Print(); return 0; 1/271页 } 请使用VC6打开考生文件夹下的工程proj2:该工程含有一个源程序文件proj2.cpp。其中定义了类Bag和用于测试该类的主函数main。类Bag是一个袋子类:用来存放带有数字标号的小球;如台球中的球:在类中用一个整数值表示一个小球,:其中运算符成员函数==用来判断两个袋子对象是否相同;即小球的个数相同:每种小球数目也相同:但与它们的存储顺序无关,?成员函数int InBag(int ball)用来返回小球ball在当前袋子内出现的次数:返回0表示该小球不存在。为类实现这两个函数:其用法可参见主函数main。 运算符函数operator ==中首先判断两个袋子内的小球个数是否相同:再调用InBag函数来判断每种小球在两个袋子内是否具有相同的出现次数 // proj2.cpp #include 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中是否出现次数不同 2/271页 //**********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之和。 3/271页 // proj3.cpp #include 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< return 0; } 第二套 请使用VC6打开考生文件夹下的工程proj1:该工程含有一个源程序文件proj1.cpp。其中位于每个注释'//ERROR ************found***************'之后的一行语句存在错误。请修正这些错误:使程序的输出结果为~ 4/271页 Constructor called of 10 The value is 10 Descructor called of 10
// proj1.cpp #include using namespace std;
class MyClass { public: MyClass(int i) { value = i; cout << 'Constructor called of ' << value << endl; }
// ERROR **********found********** void Print() // void Print() const { cout << 'The value is ' << value << endl; }
// ERROR **********found********** void ~MyClass() //~MyClass() { cout << 'Destructor called of ' << value << endl; } private: // ERROR **********found********** int value = 0; // 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**********'行的下一行添加适当的代码: 5/271页 |
|
来自: 昵称31288815 > 《考试》