问答题使用VC6打开考生文件夹下的工程test37_3。此工程包含一个test37_3.cpp,其中定义了类Letter和Number以及Grid,其中Grid类由Letter类和Number类public派生,但三个类的定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)添加类Letter的带一个参数c的构造函数,c是char型的数据,默认值为'A',该函数把参数c的值赋给类的保护成员ch,请在注释“//**1**”之后添加适当的语句。 (2)添加类Number的带一个参数n的构造函数,n是int型的数据,默认值为0,该函数把参数n的值赋给类的保护成员num,请在注释“//**2**”之后添加适当的语句。 (3)添加派生类Grid构造函数的定义,传入的参数为char型的c和血型的n,并将其分别赋值给基类的ch和num,请在注释“//**3**”之后添加适当的语句。 (4)完成派生类Grid的友元函数——“<<”运算符重载的定义,使其以格式“[g.ch,g.num]”输出,请在注释“//**4 **”之后添加适当的语句。 源程序文件test37_3.cpp清单如下: #include<iost ream. h> class Letter protected: char ch; public: // ** 1 ** ; class Number protected: int num; public: // ** 2 ** ; class Grid : public Letter, public Number public: // ** 3 ** friend ostream ; // ** 4 ** o << "[" << g.ch << ", "<< g.num << "]"; return o; int main ( ) Grid g('C', 3); cout << "Grid reference: "<< g << end1; return 0;
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成函数fun(char*data),此函数的功能是,找出字符串数组中最小的ASCII值,如果有相同变量,则输出最后一个所在的位置;如果字符串为空,则返回-1;或者不存在时也返回-1。
注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。计算数字如果第一个字母最小,则返回0。依次增加。
#include
int fun(char*data)
{
}
void main()
{
char str[1024];
cout<<"请输入一行英文字符串:\n";
cin.getline(str,1024);
cout<<”最小的字母出现在距离头部”<
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,该工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句有错误。请改正这些错误,使程序的输出结果为:1 2 3 4 5 6 7 8 9 10注意:只能修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。//projl.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[i];}void Print()const{for(int i=0;i<arraySize;i++)//ERROR**********found********** cin<<array[i]<<”; cout<<endl;}private: int*array; int arraySize;},int main(){//ERROR********** found**********MyClass obj;obj.Print();return 0;}
问答题用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了多个类,但类不能达到输出要求,按照定义的输出修改函数,使得输出如下:
TestClass3
TestClass2
TestClass1
其中定义的类并不完整,按照要求完成下列操作,将类的定义补充完整。
(1)类TestClass2公共继承于TestClassI,请在注释∥********1********后添加适当的语句。
(2)print函数为虚函数,请在注释∥********2********后添加适当的语句。
(3)修改语句,使得P指定对象obj3,请在注释∥********3********后添加适当的语句。
(4)修改语句,利用P调用print()函数,完成输出信息,注释∥********4********后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
#include
using namespace std;
class TestClassl
{
public:
void print(){
cout<<“TestClasSl”<
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为:(1.2.3.4,5)(0,0,0,0,0,0)注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>usingnamespacestd;classVectorBase{//向量基类,一个抽象类intlen;public:VectorBase(intlen):fen(len){}intlength()const{returnlen;}//向量长度,即向量中元素的个数virtualdoublegetElement(inti)const=0;//取第i个元素的值virtualdoublesum()const=0;//求所有元素的和voidshow()const{//显示向量中所有元素cout<<"(";for(inti=0,i<length()-1;i++)cout<<getElement(i)<<",";//**********found**********cout<<________<<")"<<end1;//显示最后一个元素}};classVector:publicVectorBase{//向量类double*val;public:Vector(intlen,doublev[]=NULL):VectorBase(len){val=newdouble[len];for(inti=0;i<len;i++)val[i]=(v==NULL?0.0:v[i]),}//**********found**********~Vector(){________;}doublegetElement(intindex)const{returnval[index];}doublesum()const{doubles=0.0;//**********found**********for(inti=0;i<length();i++)________;returns;}};classZeroVector:publicVectorBase{//零向量类public:ZeroVector(intlen):VectorBase(len){}//**********found**********doublegetElement(intindex)const{________;}doublesum()const{return0.0;}};intmain(){VeCtorBaSe*v;doubled[]={1,2,3,4,5};V=newVector(5,d);v->show();deletev;v=newZeroVector(6);v->show();deleteV;return0,}
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中声明IntSet是一个用于表示正整数集合的类。IntSet的成员函数Intersection的功能是求当前集合与另一个集合的交集。请完成成员函数Intersection。在main函数中给出了一组测试数据,此时程序的输出应该是:
求交集前:
1 2 3 5 8 10
2 8 9 11 30 56 67
求交集后:
1 2 3 5 8 10
2 8 9 11 30 56 67
2 8
要求:
补充编制的内容写在“// *******333*******”与“// *******666*******”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//Intset.h
#include <iostream>
using namespace std;
const int Max=100;
class IntSet
{
public:
IntSet()
//构造一个空集合
{
end = -1;
}
IntSet (int a[], int size) //构造一个包含数组a中size个元素的集合
{
if (size >= Max)
end = Max - 1;
else
end = size - 1;
for (int i = 0; i <= end; i ++)
element[i] = a[i];
}
bool IsMemberOf (int a)
//判断a是否为集合中的一个元素
{
for (int i = 0; i <= end; i ++)
if (element[i] == a)
return true;
return false;
}
int GetEnd() {return end;}
//返回最后一个元素的下标
int GetElement (int i) {return element[i];}
//返回下标为i的元素
IntSet Intersection (IntSet
//求当前集合与集合set的交
void Print ()
//输出集合中的所有元素
{
for(int i=0;i<=end;i++)
if((i+1)% 20==0)
cout << element[i] << endl;
else
cout << element[i] << "";
cout << endl;
}
private:
int element[Max];
int end;
};
void writeToFile (const char *);
//main.cpp
#include "IntSet.h"
IntSet IntSet::Intersection(IntSet
// *******333*******
// *******666*******
return IntSet(a,size);
}
int main()
{
int a[] = {1,2,3,5,8,10};
int b[] = {2,8,9,11,30,56,67};
IntSet set1 (a, 6), set2 (b, 7), set3;
cout << "求交集前:" << endl;
set1.Print();
set2.Print();
set3.Print();
set3 = set1.Intersection (set2);
cout << endl << "求交集后:" << endl;
set1.Print();
set2.Print();
set3.Print();
writeToFile (" ");
return 0;
}
问答题请编写一个函数void fun(int a [],int n),其中a为数组,n为数组a的长度。函数fun()的功能是冒泡排序法将数组a元素按从小到大的顺序排列,实现数组a的升序排列。 注意:部分源程序已存在文件PROC12.cpp中。 请勿修改主函数和其他函数中的任何内容,仅在函数fun()的花括号中填写若干语句。 文件PROC12.cpp的内容如下: //PROC12. cpp #include <iostream> using namespace std; #define MAX 100 void fun(int a[],int n); int main () int a[MAX],n,i; cout<<"Please enter the array size n:/n"; do cin>>n; if (n>100) cout<<"array size flowover! ReEnter a number(0-100)/n"; while (n>100); cout<<"Enter the array data:/n"; for (i=0; i<n; i++) cin>>a [ii; fun(a[],n); for (i=0; i<n; i++) cout<<a [i] <<" "; cout<<end1; return 0; void fun(int a[ ],int n) // * * * * * * * *
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正main()函数中的错误,使程序的输出结果如下:Constructor.Default constructorArea is12Area is0Area is12注意:错误的语句在//******error******的下面,修改该语句即可。#include<iostream.h>class CRectangle{private:double length,width;public:CRectangle(){cout<<"Defaultconstructor.\n";}CRectangle(double1,double w){length=1;width=w;cout<<"Constructor.\n";}void Set(double1,double w){this->length=1;this->width=w;}void GetArea(){cout<<"Area is"<<length*width<<endl;}};void main(){CRectangle Rectl(3.0,4.0);//******error******CRectangle Rect2(1);//******error******CRectangle Rect3;Rect1.GetArea();//******error******Rect2.Set(0);Rect2.GetArea();Rect3.GetArea();}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为:
sizeoffstrl)=5
sizeof(str2)=10
sizeof(str3)=1
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在∥********error********的下面。
(1)不能删除assert()语句;
(2)只能修改后面的数字。
提示:assert()函数如果为假,则会产生一个中断异常。
#include
#include
void main()
{
char*strl=”abc”;
∥********error********
assert(Sizeof(str1)==3);
cout<<“Sizeof(strl) = 5”
<
问答题使用VC++6.0打开
下的源程序文件3.cpp。请完成以下部分,实现在屏幕上输出为
C3
C2
这个程序需要修改的部分,请按照以下部分实现。
(1)类C0不能被实例化,请定义一个纯虚函数print,在注释1后添加适当的语句。
(2)类C1私有虚继承类C0,请在注释2后添加适当的语句。
(3)类C2公有继承于C0,请在注释3后添加适当的语句。
(4)类C3公有继承于C2与C1,请在注释4后添加适当的语句。
注意:增加代码或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
class C0
{
//********1********
};
//********2********
class C1:
{
public:
void print()
{
cout<<"C1"<<endl;
}
};
//********3********
class C2:
{
public:
void print()
{
cout<<"C2"<<endl;
}
};
//********4********
class C3:
{
public"
void print()
{
cout<<"C3"<<endl;
}
};
void main()
{
C3 obj3;
C2 obj2;
obj3.print();
obj2.print();
return;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成以下函数:
int factorial(int n):求出n的阶乘,必须使用递归调用。
如果n小于1则返回0。
注意:不能修改函数的其他部分。
#include
#include
int factorial(int n)
{
}
void main()
{
cout<
问答题请使用VC6或使用【答题】菜单打开考生文件夹prog3下的工程prog3,其中包含了类TaxCalculator(“个税计算器”)和主函数main的定义。创建“个税计算器”需要接收税率表信息和起征额信息。在main函数中,通过两个数组创建了如下的税率表:利用这个税率表创建“个税计算器”时,假定起征额为2000元(即不超过2000元的所得不征收个人所得税)。请补充完成计算应纳个人所得税额的成员函数getTaxPayable,其中的参数income为月收入。此程序的正确输出结果应为:月收入为800元时应缴纳个人所得税0元月收入为1800元时应缴纳个人所得税0元月收入为2800元时应缴纳个人所得税55元月收入为3800元时应缴纳个人所得税155元月收入为4800元时应缴纳个人所得税295元月收入为5800元时应缴纳个人所得税455元注意:只能在函数getTaxPayable中的“//**********333**********”和“//**********666**********”之间填入若干语句,不要改动程序中的其他内容。//TaxCaleulator.h#include#includeusingnamespacestd;classTaxCalculator{public:TaxCalculatOr(doublethelimits[],doublethe—rates[],intthe—length,doublethe—threshold):lowerlimits(newdouble[thelength]),rates(newdouble[the—lengthj),listlen(thelength),thresh.old(thethreshold){for(inti=0;i=0){//**********333**********//**********666**********一一i;}returntax_payable;}voidTaxCalculator::showTaxPayable(doubleincome)const{cout
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: Leaf Node 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>using namespace std;class Component{public://声明纯虚函数print()//********** found********** }; class Composite:public Component{ public: //**********found**********void setChiid(_______){ m chiId=child; } virtual void print()const { m child->print(); }private: Component*m—child; }; class Leaf:public Component{ public: virtual void print()const { //**********found********** } }; int main() { Leaf node; Composite comp; comp.setChild(&node); Component*P=&comp; P->print(); return 0; }
问答题综合应用
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类Polynomial("多项式")的定义。形如5x4+3.4x2-7x+2代数式称为多项式,其中的5为4次项系数,3.4为2次项系数,-7为1次项系数,2为0次项(常数项)系数。此例缺3次项,意味着3次项系数为0,即省略了0x3。在Polynomial中,多项式的各个系数存储在一个名为coef的数组中。例如对于上面的多项式,保存在coef[0]、coef[1]、…、coef[4]中的系数依次为:2.0、-7.0、3.4、0.0、5.0,也即对于i次项,其系数就保存在coef[i]中。作为成员函数重载的运算符"+"用于计算两个多项式的和,并返回作为计算结果的那个多项式。请补充完成文件Polynomial.cpp中重载运算符函数operator + 的定义。此程序的正确输出结果应为:
+7.3X^4+20.6X^3-41.2X^2-2.4X^1+5X^0
-2.3X^5+14.6X^4+12.8X^3+2.8X^2+0.2X^1+1X^0
注意:只需在"//********333********"和"//********666********"之间填入若干语句,不得改动程序中的其他部分。
//Polynomial.h
#include
using namespace std;
const int MaxNum=10;
class Polynomial{ //"多项式"类
double coef[MaxNum]; //系数数组,coef[0]为0次项(常数项)系数,
//coef[1]为1次项系数,coef[2]为二次项系数,依此类推
public:
Polynomial(double c[],int num) {
for(int i=0;i
for(i=0;i
}
//计算两个多项式的和
Polynomial operator +(const Polynomial
void Print() {
for(int i=MaxNum-1;i>=0;i--)
if(coef[i]>0)
cout<<'+'<
else if(coef[i]!=0)
cout<
cout<
}
};
void writeToFile(const char *path);
//Polynomial.cpp
#include"Polynomial.h"
Polynomial Polynomial::operator +(const Polynomial //存放结果系数的数组
//********333********
//********666********
return Polynomial(c,10);
}
// main.cpp
#include"Polynomial.h"
int main(){
double p1[]={5.0, 3.5, -41.2, 7.8},
p2[]={0.0, -5.9, 0.0, 12.8, 7.3},
p3[]={1.0, 6.1, 2.8, 0.0, 7.3, -2.3};
Polynomial poly1(p1,sizeof(p1)/sizeof(double)),
poly2(p2,sizeof(p2)/sizeof(double)),
poly3(p3,sizeof(p3)/sizeof(double));
(poly1+poly2).Print();
(poly2+poly3).Print();
writeToFile("");
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为: 1 1 2 1 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream.h>Class TestClass{ //********error******** //********error******** const int j;public: TestClass() { //********error******** static int i=0; cout<<++i<<endl; cout<<j<<endl; }};Void main(){ TestClass obj1; TestClass obj2; obj2.j +=obj1.j; return;}
问答题使用VC6打开下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为:initTestClassdelTestClass注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream.h>classTestClass{//********error********//********error********intTestClass(){cout"initTestClass"endl;};//********error********void~TestClass(){cout"delTestClass"endl;};};voidmain(){TestClassobj;return;}
问答题请使用VC6或使用【答题】菜单打开
proj1下的工程proj1,此工程包含有一个源程序文件proj1.cpp。其中位于每个注释“// ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
(4,4)
注意:只修改注释“// ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.cpp
#include <iostream>
using namespace std;
class Point{
public:
// ERROR *******found*******
Point(double x, double y) _x(x), _y(y) {}
double GetX() const {return _x;}
double GetY() const {return _y;}
// ERROR *******found*******
void Move (double xOff, double yOff) const
{_x + = xOff; _y + = yOff;}
protected:
double _x, _y;
};
int main()
{
Point pt(1.5, 2.5);
pt.Move(2.5, 1.5);
// ERROR *******found******** 以下语句输出pt成员_x和_y的值
cout << "(" << pt._x << "," << pt._y << ")"<< endl;
return 0;
}
问答题使用VC6打开下的源程序文件modi1.cpp,该程序运行时有错误,请改正错误,使得程序正常运行。并使程序输出结果为:ABCDEFABCDEFABCDEF注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream.h>voidmain(){//********error********chars1[]="ABC"DEF";//********error********chars2[]="ABCDEF";//********error********chars3[]="ABC"+"DEF";coutcouts2endl;couts3endl;return;}
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2,此工程包含一个源程序文件proj2.cpp。其中定义了Score类。
Score是一个用于管理考试成绩的类。其中,数据成员_s指向存储成绩的数组,_n表示成绩的个数;成员函数Sort使用冒泡排序法将全部成绩按升序进行排列。
请在程序中的横线处填写适当的代码,然后删除横线,以实现Score类的成员函数Sort。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
// proj2.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Score{
public:
Score(double * s, int n) : _s(s), _n(n) {}
double GetScore(int i) const{return_s[i];}
void Sort();
private:
double *_s;
int _n;
};
void Score::Sort()
{
// ********found********
for (int i = 0; i < _n-1; ______)
// ********found********
for (int j =______; j > i; j--)
if (_s[j] < _s[j -i])
{// 交换_s[j]和_s[j-1]
double t = _s[j];
// ********found********
______;
// ********found********
______;
}
}
int main()
{
const int NUM = 10;
double s[NUM];
srand(time(0));
for (int i=0; i<NUM; i++)
s[i] = double (rand())/RAND_MAX * 100;
Score ss(s, NUM);
ss.Sort();
for (int j = 0; j<NUM; j ++)
cout << ss.GetScore(j) << endl;
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
The extension is:CPP
注意:只修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.epp
#include <iostream>
using namespace std;
class MyClass {
char * p;
public:
MyClass(char c)
{p = new char; * p=c;}
// ERROR ******** found********
MyClass (const MyClass copy) { p =new char; * p = * (copy.p); }
// ERROR ******** found********
~MyClass() { free p; }
MyClass
*p = * (rhs.p);
// ERROR ******** found********
return this;
}
char GetChar() const { return * p; }
};
int main()
{
MyClass obj1('C'), obj2 ('P');
MyClass obj3(obj1);
obj3 = obj2;
cout << "The extension is:"
<<obj1.GetChar () <<obj2.GetChar()
<<obj3.GetChar () << endl;
return 0;
}
