问答题使用VC6打开下的源程序文件modi3.cpp。程序通过继承关系,实现对姓名的控制。类TestClass1实现对名字访问的接口,TestClass2实现对名字的设置和输出。程序输出为:TestClass2NameMay其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。(1)在类TestClass1中定义接口函数GetName,为纯虚函数。请在注释//********1********后添加适当的语句。(2)函数GetName2()实现获得名字的缓存,但是只获得读允许操作这个缓存,请在注释//********2********后添加适当的语句。(3)实现TestClass2的构造函数,请在注释//********3********后添加适当的语句。(4)完成TestClass2的构造函数,实现对名字的处理。请在注释//********4********后添加适当的语句。注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>classTestClass1{public://********1********};classTestClass2:publicTestClass1{public:voidGetName(){cout"TestClas2Name"endl;}//********2********{returnm_str;}//********3********{inti;f.r(i=0;str[i]!=0;i++)m_str[i]=str[i];//********4********}private:charm_str[32];};voidmain(){TestClass1*p;TestClass2obj1("May");p=p->GetName();coutobj1.GetName2()endl;return;}
问答题请编写一个函数long Fibo(int n), 该函数返回n的Fibonacci数。规则如下:n等于1或者2时,Fibonacci数为1,之后每个Fibonacci数均为止前两个数之和, 即:F(n)=F(n-1)+F(n-2) 注意:请使用递归算法实现该函数。 部分源程序已存在文件test1_2.cpp中。 请勿修改主函数main和其他函数中的任何内容,仅在函数Fibo的花括号中填写若干语句。如n=8时,结果是21。 文件test1_2.cpp清单如下: #include<iostream.h> consh int N=8; long Fibo(int n); void main() long f=Fibo(N); couk<<f<<endl; long Fibo(int n)
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成函数fun(char*s1,char*s2),此函数的功能是计算s1中出现s2的个数,当不出现时,则返回0。如:s1为"1112223333aaaaeeffd"s2为"11"则返回1s2为"aa"则返回2注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。#include<iostream.h>//注意只能使用int类型,不能类型转换int fun(char*s1,char*s2){}void main(){char s1[1024];char s2[256];cout<<"please input a string:"<<endl;cin.getline(s1,1024);cout<<"please input otherstring:"<<endl;cin.getline(s2,256);cout<<fun(s1,s2);cout<<endl;return;}
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分的程序。实现函数sort(int A[],int n),用冒泡法将数组排序。
提示:所谓冒泡法,就是每次把相邻的两个数交换,较大的数交换到后面。这样下标从0到n-1的数与其后面相邻的数交换,可以把最大的数交换到数组的末端。进行n次下标从0到n-1的交换,则数组则会变成有序的,而且是由大到小的顺序。
注意:不能修改程序的其他部分,并且不能删除其他的部分,也不能修改程序的结构。
#include <iostream.h>
#define N 10
void sort(int A[N],int n)
{
}
int main ()
{
int A[N] ={5, 7, 4, 6, 10, 13, 78,-4,9,20};
sort (A, 10);
for(int i=0;i<sizeof(A)/sizeof(int); i++)
{
cout
A[i]
" ";
}
cout
endl;
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp。其中定义了类Set和用于测试该类的主函数main。类Set是一个用于描述字符集合的类,在该字符集合中,元素不能重复(将“a”和“A”视为不同元素),元素最大个数为100。为该类实现一个构造函数Set(char*s),它用一个字符串来构造一个集合对象,当字符串中出现重复字符时,只放入一个字符。此外,还要为该类实现另一个成员函数InSet(charc),用于测试一个字符C是否在—个集合中,若在,则返回true;否则返回false。构造函数Set和成员函数InSet的部分实现代码已在文件proj2.cpp中给出,请在标有注释“//TODO:”的行中添加适当的代码,将这两个函数补充完整,以实现其功能。提示:在实现构造函数时,可以调用InSet函数来判断一个字符是否已经在集合中。注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//proj2.cpp#include<iostream>usingnamespacestd;constintMAXNUM=100;clasSSet{private:intnum;//元素个数charsetdata[MAXNUM];//字符数组,用于存储集合元素public:Set(char*s);//构造函数,用字符串S构造一个集合对象boolInSet(charc);//判断一个字符C是否在集合中,若在,返回true,否则返回falsevoidPrint()const;//输出集合中所有元素};Set::Set(char*s){num=0;while(*s){//**********found**********if(________)//TODO:添加代码,测试元素在集合中不存在//**********found**********________;//TODO:添加一条语句,加入元素至集合中s++;}}boolSet::InSet(charc){for(inti=0;i<num;i++)//**********found**********if(________)//TODO::添加代码,测试元素C是否与集合中某元素相同//**********found**********________;//TODO:添加一条语句,进行相应处理returnfalse;}voidSet::Print()const{cout<<"Setelements:"<<end1;for(inti=0;i<num;i++)cout<<setdata[i]<<";cout<<end1;}intmain(){chars[MAXNUM];cin.getline(s,MAXNUM-1);//从标准输入中读入一行Setsetobj(s);//构造对象setobjsetobj.Print();//显示对象setobj中内容return0;}
问答题使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错。请改正程序中的错误,使程序输出的结果为
100
37
32
注意:错误的语句在//******error******的下面,修改该语句即可。
试题程序:
#include
//******error******
voidmain
{
//******error******
intm=0142:
//******error******
intn=0X27:
intq=32;
cout< cout< cout< return;
}
问答题使用VC++6.0打开
下的源程序文件1.cpp,该程序运行时有误,请改正其中的错误,使程序正常运行,并使程序的输出结果为
平均值为29
最大值为112
最小值为-11
注意:错误的语句在//******error******的下面,修改该语句即可。
试题程序:
#include<iostream>
using namespace std;
int main()
{
int i,Ave,Min,Max;
int data[8]={100,21,-73,86,14,0,-21,1};
Ave=0;
for(i=0;i<8;i++)
//******error******
Ave=data[i];
Ave/=8;
cout<<"平均值为:"<<Ave<<endl;
Max=Min=data[0];
for(i=0;i<8;i++)
{
//******error******
if(data[i]<Max) Max=data[i];
//******error******
if(data[i]>Min) Min=data[i];
}
cout<<"最大值为:"<<Max<<endl;
cout<<"最小值为:"<<Min<<endl;
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的Matrix是一个用于表示矩阵的类。operator+的功能是实现两个矩阵的加法运算。例如,若有两个3行3列的矩阵则A与B相加的和为请编写operator+函数。要求:补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//Matvix.h#include<iostream>#include<iomanip>usingnamespacestd;constintM=18;constintN=18;classMatrix{intarray[M][N];public:Matrix(){}intgetElement(inti,intj)const{returnarray[i][j];}voidsetElement(inti,intj,intvalue){array[i][j]=value;}voidshow(constchar*s)const{cout<<endl<<s;for(inti=0;i<M;i++){cout<<endl;for(intj=0;j<N;j++)cout<<setw(4)<<array[i][j];}}};voidreadFromFile(constchar*,MatrixvoidwriteToFile(char*,constMatrix//main.cpp#include<fstream>#include"Matrix.h"voidreadFromFile(constchar*filename,Matrixif(!infile){cerr<<"无法读取输入数据文件!/n";return;}intd;for(inti=0;i<M;i++)for(intj=0;j<N;j++){infile>>d;m.setElement(i,j,d);}}Matrixoperator+(constMatrixreadFromFile("",m2);sum=m1+m2;m1.show("Matrixm1:");m2.show("Matrixm2:");sum.show("Matrixsum=m1+m2:");writeTOFile("",sum);return0;}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,输出的结果为:
Constructor,i=0,
Destructor
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
class cobj
{
int i;
public:
cobj();
void display();
~cobj();
};
∥********error********
cobj:cobj()
{
cout<<“Constructor”<<“,”;
i=0;
}
∥********error********
cobj:di splay()
{
cout<<“i=”<
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: 150 200 注意:错误的语句在//*****error******的下面,修改该语句即可。提示:定义Number1为一个整型数据位长的变量。#include<iostream.h>//*****error******struct{ //*****error****** int Number1:1; int Number2;}MyStruct;void main(){ MyStruct mstr; mstr.Number1=150; mstr.Number2=15; //*****error****** int *ptr=&mstr.Number1; cout<<*ptr<<end1; *ptr=200; cout<<*ptr<<endl;}
问答题使用VC6打开考生文件件下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补允完整。
(1) 重载运算符 int, 请在注释∥********1********后添加适当的语句。
(2)重载运算符“
using namespace std;
class TestClass
{
public:
∥********1********
{
cout<<“int”<
问答题使用VC6打开考生文件夹下的工程tsst6_3,此工程包含一个源程序文件test6_3.cpp,其中定义了用于表示考生的类 Student,请按要求完成下列操作,将程序补充完整。
(1)定义私有数据成员code、english分别用于表示考生的编号、英语成绩、它们都是int型的数据。请在注释“//**1**”之后添加适当的语句。
(2)完成成员函数void Student::inputinformation()的定义,该函数用于用户输入一个考生对象的信息,输入格式如下:
输入编号:
英语成绩;
计算机成绩:
请在注释“//**2**”之后添加适当的语句。
(3)利用己实现的类Student的成员函数,完成函数void firstname(Student *A[],int num)的定义,该函数根据考生信息 A[],输出num个考生中总分最高者的编号及其相应的总分,在此不考虑总分相同的情况。请在注释“//**3**”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件test6_3.cpp清单如下:
#include<iostream.h>
class Student
{
//**1**
int computer;
int total;
public:
void getinformation();
void computesum();
int getcode();
int gettotalscore();
~Student();
};
void Student::getinformation()
{
//**2**
cout<<"英语成绩: " ;
cin>>english;
cout<<"计算机成绩: ";
cin>>computer;
}
void Student::computesum()
{
total=english+computer;
cout<<"编号"<<code<<"总分:"<<total<<endl;
}
int student::getcode()
{
return code;
}
int Student::gettotalscore()
{
return total;
}
void firstname(Student *A[],int num)
{
//**3**
tempsum=(*A[0]).gettotalscore();
for(int i=1;i<num;i++)
{
if (((*A[i]).gettotalscore())>tempsum)
{
tempcode=(*A[i]).getcode();
telnpsum=(*A[i]).gettotalscore();
}
}
cont<<"总分最高者--"<<tempcode<<": "<<tempsum<<endl;
}
void main()
{
Student *A[3];
int i,n=3;
for(i=0;i<n;i++)
{
A[i]=new Student;
A[i]->getinformation();
}
for(i=0;i<n;i++)
{
A[i]->computesum();
}
firstname(A,3);
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码。补充函数convert(long s,long*str),使之从低位开始取出长整型变量S中奇数位上的数,依次存放在数str中。例如,当S中的数为:7654321时,str中的数为:7531。注意:请勿改动主函数。#include<iostream.h>void convert(long s,long *str){}void main(){long s,res;cout<<"please enter s:"<<endl;cin>>s;convert(s,&res);cout<<"The result is:"<<res<<endl:return;}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)完成类MyArrayClass的构造函数,申请数组的大小,请在注释//********1********后添加适当的语句。(2)完成类MyArrayClass的析构函数,释放数组,请在注释//********2********后添加适当的语句。(3)完成重载运算符“[]”,用来获得指定下标的数据,请在注释//********3********后添加适当的语句。(4)完成函数SetElement(),用来设置指定下标的数据。如果数据没有超出范围,则设置数据,并返回1,否则返匣0,请在注释//********4********后添加适当的语句。注意:除在指定的位置添加语句外,并不要更改程序中的其他语句。#include<iostream.h>template<Class T>class MyArrayClass{private:T* data;int length;public:MyArrayClass(int len){length=len;//********1********}~MyArrayClass(){//********2********}T&operator[](int i){//********3********}bool SetElement(int i,T t){//********4********if(){data[i]=t;return true;}return false;}};int main(){MyArrayClass<int>obj(5);obj[3]=1;cout<<obj.SetElement(5,2)<<endl;cout<<obj.SetElement(4,2)<<endl;return0;}
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程文件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 //重载运算符<<
};
// **1** *******found*******
______ (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 (______);
}
ostream //输出向量v的坐标
return os;
}
int main()
{
MyVector v1(2,3), v2(4,5), v3;
// **3** *******found*******
v3 =______;
cout << v3 << endl;
return 0;
}
问答题请使用VC6或使用【答题】菜单打开
prog2下的工程prog2。此工程中包含一个程序文件main.cpp,其中有“部门”类Department和“职工”类Staff的定义,还有主函数main的定义。在主函数中定义了两个“职工”对象,他们属于同一部门。程序展示,当该部门改换办公室后,这两个人的办公室也同时得到改变。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换办公室前:
职工号:0789 姓名:张三 部门:人事处 办公室:521
职工号:0513 姓名:李四 部门:人事处 办公室:521
改换办公室后:
职工号:0789 姓名:张三 部门:人事处 办公室:311
职工号:0513 姓名:李四 部门:人事处 办公室:311
注意:只在横线处填写适当的代码,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Department{ //“部门”类
public:
Department (const char * name, const char * office) {
strcpy(this->name, name);
// ********found********
______
}
const char * getName() const {return name;} //返回部门名称
// ********found********
const char * getOffice() const {______} //返回办公室房号
void changeOfficeTo (const char * office) { //改换为指定房号的另一个办公室
strcpy (this -> office, office);
}
private:
char name[20]; //部门名称
char office[20]; //部门所在办公室房号
};
class Staff{ //“职工”类
public:
// **********found**********
Staff (const char * my_id, const char * my_name, Department
strcpy (this -> name, my_name);
}
const char * getID() const {return staff id;}
const char * getName() const {return name;}
Department getDepartment() const {return dept;}
private:
char staff_id[10]; //职工号
char name[20]; //姓名
Department //所在部门
};
void showStaff(Staff
cout << "姓名:" << staff.getName() << " ";
cout << "部门:" << staff.getDepartment().getName() << " ";
cout << "办公室:" << staff.getDepartment().getOffice() << endl;
}
int main() {
Department dept ("人事处", "521");
Staff Zhang ("0789","张三",dept), Li ("0513","李四",dept);
cout << "改换办公室前:" << endl;
showStaff (Zhang);
showStaff (Li);
//人事处办公室由521搬到311
// *******found*******
______
cout << "改换办公室后:" << endl;
showStaff (Zhang);
showStaff (Li);
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示矩形的CRect类,但类CRect的定义并不完整。请按要求完成下列操作,将类CRect的定义补充完成。 (1)定义私有数据成员leftPoint、topPoint、rightPoint、bottomPoint,分别用于表示矩形左上角及右下角的点的坐标,它们都是double型的数据。请在注释//********1********之后添加适当的语句。 (2)完成默认构造函数CRect的定义,指定缺省实参为0,都是double型的数据。请在注释//********2********之后添加适当的语句。 (3)定义函数体为空的析构函数。请在注释//********3********之后添加适当的语句。 (4)在main()函数中定义CRect类的实例rect2,并把rect1的值赋给rect2。请在注释//********4********之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>Class CRect{private: //********1********public: //********2******** //********3******** void SetPoints(double,double,double,double); Void SetLeftPoint(double m){leftPoint=m;) vold SetRightPoint(double m){rightPoint=m;) void SetTopPoint(double m){topPoint=m;} Void SetBottomPoint(double m){ bottomPoint=m;) void Display();};CRect::CRect(double 1,doublet,double r,double b){ leftPoint=1;topPoint=t; rightPoint=r;bottomPoint=b;}void CRect::SetPoints(double1,double t,double r,double b){ leftPoint=1;topPoint=t; rightPoint=r;bottomPoint=b; }void CRect::Display(){ cout<<"1eft-top point is("<<leftPoint<<","<<topPoint<<")"<<'\n'; cout<<"right-bottom point is("<<rightPoint<<","<<bottomPoint<<")"<<'\n';}void main(){ CRect rect0; rect0.Display(); recto.SetPoints(20,20.6,30,40); rect0.Display(); CRect rectl(0,0,150,150); rectl.SetTopPoint(10.5); rectl.SetLeftPoint(10.5); //********4******** rect2.Display();}
问答题使用VC++6.0打开下的源程序文件3.cpp。程序通过继承关系,实现对姓名的控制。类TC1实现对名字访问的接口,TC2实现对名字的设置和输出。
程序输出为
TC2Name
May
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)在类TC1中定义接口函数GetName为纯虚函数。请在注释1后添加适当的语句。
(2)函数GetName2用于获得名字的缓存,但只获得允许读操作这个缓存,请在注释2后添加适当的语句。
(3)实现TC2的构造函数,请在注释3后添加适当的语句。
(4)完成TC2的构造函数,实现对名字的处理。请在注释4后添加适当的语句。
注意:增加或者修改代码的位置已经用符号表示出来,请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
class TC1
{
public:
//********1********
};
class TC2: public TC1
{
public:
void GetName()
{
cout<<"TC2Name"<<endl;
}
//********2********
{
return m_str;
}
//********3********
{
int i;
for(i=0; str[i]!=0; i++)
m_str[i]=str[i];
//********4********
}
private:
char m_str[32];
};
void main()
{
TC1*p;
TC2 obj1("May");
p=
p->GetName();
cout<<obj1.GetName2()<<endl;
return;
}
问答题使用VC++ 6.0打开
下的源程序文件2.cpp。请完成函数fun(char *s),使其具有以下功能:
(1)把s中的大写字母转换成小写字母,把其中的小写字母转换成大写字母。并且在函数中调用写函数WriteFile()将结果输出到2.txt文件中。
例如:s="helloWORLD",则结果为:s="HELLOworld"
(2)完成函数WriteFile(char *s),把字符串输入文件中。
提示:打开文件使用的第二参数为ios_base::binary|ios_base::app。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
void WriteFile(char *s)
{
}
void fun(char *s)
{
}
void ClearFile()
{
ofstream out1;
out1.open("2.txt");
out1.close();
}
int main()
{
ClearFile();
char s[1024];
cout<<"please input a string: "<<endl;
cin.getline(s,1024);
fun(s);
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)在类TestClass中定义name为字符串类型,age为整型,请在注释∥********1********之后添加语句。
(2)设置类TestClass0的基类为TestClass类的定义,请在注释∥********2********后添加语句。
(3)在类TestClass的派生类TestClass0的公有成员中定义析构函数TestClass0, 请在∥********4********后添加。
(4)设置类TestClassl的基类为TestClass类的定义,请在∥********4********后实现。
本程序输出如下结果:
TestClass class constructor
TestClass0 class constructor
TestClass on class constructor
TestClass 1 class constructor
TestClass 1 class constructor
TestClass class constructor
TestClass0 class constructor
TestClass class constructor
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include
Class TestClass
{
∥********1********
public:
TestClass(){cout<<“TestClass
class constructor”<