问答题请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2。此工程包含一个程序文件main.cpp,其中的Array是一个表示数组的模板类。Array的成员a用于指向存放数据的数组,size表示该数组的大小。此程序的输出结果应为: 8 29,20,33,12,18,66,25,14 66,33 注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动//************found************。 //源程序 #include <iostream> #include <cstdlib> using namespace std; template<class Type> class Array//数组类 Type *a; int size; public: Array(Type b[],int mm):size(mm)//构造函数 if(size<2) cout<<"数组长度太小,退出运行!/n";exit(1); //************found************ a=______;//a指向申请的数组空间 for(int i=0; i<size; i++) a[i]=b[i]; //************found************ ~Array()______; //析构函数 void MaxTwo(Type& x1,Type& x2) const;//由x1和x2带回数组a中的两个最大值 //************found************ int Length() const______;//返回数组长度 Type operator[](int i) const //下标运算符重载成员函数 if(i<0 || i>=size)cout<<"下标越界!"<<endl;exit(1); return a[i]; ; //由x1和x2带回数组a中的两个最大值 template<class Type> void Array<Type>::MaxTwo(Type& x1,Type& x2) const //补充完整函数体的内容 //将数组a中头两个数据赋值给x1和x2,使得x1>=x2 //************found************ ______?(x1=a[0],x2=a[1]):(x1=a[1],x2=a[0]); for(int i=2;i<size; i++) if(a[i]>x1)x2=x1; x1=a[i]; else if(a[i]>x2) x2=a[i]; void main() int s1[8]=29,20,33,12,18,66,25,14; Array<int> d1(s1,8); int i,a,b; d1.M axTwo(a,b); cout<<d1.Length()<<endl; for(i=0;i cout<<d1[7]<<endl;cout<<a<<","<<b<<endl;
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示人基本信息的类CHumanInf0,但类CHumanlnfo的定义并不完整。请按要求完成下列操作,将类CHumanlnfo的定义补充完成:
(1)定义私有数据成员bloodType用于表示血型,血型为char型的数据。请在注释“ ∥********1********”之后添加适当的语句。
(2)完成构造函数的定义,要求具有缺省值,缺省值为身高175,体重70,血型A。请在注释“ ∥********2********”之后添加适当的语句。
(3)完成类外CHumanInfo成员函数Setlnfo的定义。请在注释“ ∥********3********之后添加适当的语句.
(4)在主函数中调用成员函数Setlnfo,把对象d2的三个私有数据成员分别设定为身高170,体重64,血型为B。请在注释“ ∥********4********”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include
C1ass CHumanInf0
{
private:
int height;
int weight;
∥********1********
public:
∥********2********
:height(ht),weight(wt),
bloodType(bt){};
CHumanInfo(CHumanlnfo
int GetHeight()
{
return height;
}
int GetWeight()
{
return weight;
}
int GetBloodType()
{
return bloodType;
}
void SetInfo(int ht,int
wt,char bt);
void Display();
);
∥********3********
{
height=ht;
weight=wt ;
bloodType=bt;
}
void CHumanInfo::Display()
{
cout<<”Humanlnfo:”;
cout<
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Name: sonny Type: dog
Name: John Type: dog
Name: Danny Type: eat
Name: John Type: dog
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
enum Pets_type{dog, cat, bird, fish};
class Pets{
private:
char*name;
Pets_type type;
public:
Pets(const char*name="sonny", Pets type type=dog);
Pets
~Pets();
void show()const;
};
Pets::Pets(const char*name, Pets_type type)
//构造函数
{
this->name=new char[strlen (name)+1];
strcpy(this->name, name);
//ERROR********found********
type=type;
}
Pets::~Pets()//析构函数,释放name所指向的字符串
{
//ERROR*********found*********
name='/0';
}
Pets
delete[]name;
name=new char[strlen(s.name)+1];
//ERROR********found********
strcpy(this->name, name);
type=s.type;
return*this;
}
void Pets::show()const
{
cout<<"Name:"<<name<<"Type:";
switch(type)
{
case dog: cout<<"dog"; break;
case cat: cout<<"cat"; break;
case bird: cout<<"bird"; break;
case fish: cout<<"fish"; break;
}
cout<<endl;
}
int main()
{
Pets mypet1, mypet2("John", dog);
Pets youpet("Danny", cat);
mypet1.show();
mypet2.show();
youpet.show();
youpet=mypet2;
youpet.show();
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。请完成以下部分,实现在屏幕上输出为:1estClass3TestClass2这个程序需要修改的部分,请按照以下部分实现。(1)类TestClass0不能被实例化,请定义一个纯虚函数print,在注释∥*******1*******后添加适当的语句。(2)类TestClass1私有虚继承类TestClass0,请在注释∥********2********后添加适当的语句。(3)类TestClass2公有继承类TestClass0,请在注释∥*******3*******后添加适当的语句。(4)类TestClass3公有继承类TestClass2与TestClass1,请在注释∥*******4*******后添加适当的语句。注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。#include<iostream.h>Class TeStClass0{ //********1********};//********2********Class TestCiass1:{public: void print() { cout<<“TestClass1”<<end1; )};//********3********C1ass TeStClass2:{public: void print() { cout<<“TestClass2”<<end1; }},//********4********C1ass TeStClass3:{public: void print() { cout<<“TestClass3”<<end1; }};Void main(){ TeStClasS3 C3; TestClass2 c2 ; c3.print(); c2.print(); return;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果应为: 2 3 4 5 27 28 31 66 75 2 3 4 5 6 27 28 31 66 75 2 3 4 5 6 19 27 28 31 66 75 3 4 5 6 19 27 28 31 66 75 3 4 5 6 19 27 28 31 66 75 要求: 补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数WriteTOFile已经编译为obj文件,并且在本程序中调用。 //IntegorSet.h #ifndef INTEGERSET #define INTEGERSET #include<iostream> using namespace std; const int MAXELEMENTS=100: //集合最多可拥有的元素个数 class IntegerSet{ int elem[MAXELEMENTS]; //用于存放集合元素的数组 int counter;//用于记录集合中元素个数的计数器 public: IntegerSet():counter(0){} //创建一个空集合 IntegerSet(int data[],int size); //利用数组提供的数据创建一个整数集合 void add(int element); //添加一个元素到集合中 void remove(int element); //删除集合中指定的元素 int getCount()const{return counter;} //返回集合中元素的个数 int getElement(int i)const{return elem[i];}//返回集合中指定的元素 void show()const; }; void WriteToFile(char*); #endif //main.cpp #include"IntegerSet.h" #include<iomanip> IntegerSet::IntegerSet(int data[],int size):counter(0){ for(int i=0;i<size;i++) add(data[i]); } void IntegerSet::add(int element){ int j; //从后往前寻找第一个小于等于element的元素 for(j=counter;j>0;j--) if(element>=elem[j一1])break; //如果找到的是等于element的元素,说明要添加的元素已经存在,直接返回 if(j>0) if(element==elem[j-1])return; //如果找到的是小于element的元素,j就是要添加的位置 //该元素及其后面的元素依次后移,腾出插入位置 for(int k=counter;k>j;k--) elem[k]=elem[k-1]; elem[j]=element;//将element插入到该位置 counter++; //计数器加1 } void IntegerSet::remove(int element){ //********333******** //********666******** } void IntegerSet::show()const{ for(int i=0;i<getCount();i++) cout<<setw(4)<<getElement(i); cout<<endl: } int main(){ int d[]={5,28,2,4,5,3,2,75,27,66,31}; IntegerSet S(d,11); S.show(); s.add(6); s.show(); s.add(19); s.show(); s.remove(2); s.show(); s.add(4); s.show(); WriteToFile(" "); return 0; }
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数factor(int*des,int&length,int n)实现的功能是:将n所有因数存放到des中,通过length引用返回因数的个数,比如fator(a,length,20)执行后,则a={1,2,4,5,10,20},length=6。
注意:只能补充函数factor 0,请勿改动其他部分的内容。
#include
#define MAXNLEN 100
void factor(int*des,int&length,int n)
{
}
void main()
{
int a[MAXNLEN],length;
int n=2 0;
factor(a,length,n);
for(int i=0;i
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成函数fun(int x),该函数功能是判定x的所有的约数,并且在函数中调用写函数WriteFile0将结果输出到modi2.txt文件中。
例如:x=10的约数为1,2,5,10。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
#inClude
#include
#inClude
using namesPace std;
void WriteFile(int c)
{
ofstream out1;
out1.open(“modi2.txt”,ios
base::binary | ios—base::app);
out1<
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,即使程序在界面输出以下类似内容: 0012FF68 1 0012FF3C 5 0012FF6C 2 0012FF44 4 0012FF70 3 0012FF4C 3 0023FF74 4 0012FF54 2 0012FF78 5 0012FF5C 1 006AFDCC~006AFDEF数据可能因为机器的不同,程序的输出的内容会有差异。只要相差为4的连续地址即可。 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream>using namespace std;int main(){ int*p,a[5]={1,2,3,4,5}; double*q,b[5]={5.0,4.0,3.0,2.0,1.0}; int x; p=a; q=b; //********error******** for(x=0;x<=5;x++) //********error******** cout<<p+x << " " <<(p+x)<<" "<<q+x<<" "<<(q+x)<<endl; return 0;}
问答题使用VC++6.0打开考生文件夹下的源程序文件2.cpp。完成fun函数,其功能是求出M行N列二维数组每行元素中的最小值,并计算它们的和,并通过形参传回主函数并输出。 注意:不能修改程序的其他部分,只能修改fun函数。 试题程序: #include<iostream.h> #define M 2 #define N 4 void fun(int a[M][N],int *sum) void main() int x[M][N]=7,6,5,2,4,2,8,3; int s; fun(x,&s); cout<<s<<end1; return;
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)利用define定义常量TRUE为1,定义常量FALSE为0,请在注释∥********1********后添加适当的语句。
(2)在类A2前增加A1的声明,请在注释∥********2********后添加适当的语句。
(3)在类C1中声明友元函数bool func(A2
bool func(A2&obj 1,A1&obj 2)
{
∥********4********
return
}
int main()
{
A2 obj 0;
A1 obj 1;
cout<
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,并使程序的执行结果为:
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
#include
void main()
{
int i, j , k;
for(i=5;i>=i;i一一)
{
∥********error********
for(j=1;j=i;k++)
cout0;k++)
cout<
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成proj3.h,实现默认构造函数MiniString(const char*s="")和析构函数~MiniString()。
要求:
补充编制的内容写在“//********333********”与“//********666*******”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//proj3.h
#include <iostream>
#include <iomanip>
using namespace std;
class MiniString
{public:
friend ostream return output;
}
friend istream
//用于输入的临时数组
temp[0] = '/0';
//初始为空字符串
input >> setw{{U}} {{/U}}>> temp;
int inLen = strlen(temp); //输入字符串长度
if( inLen! = O)
{
s.length = inLen; //赋长度
if( s.sPtr! = 0) delete []s.sPtr;
//避免内存泄
s.sPtr = new char [ s. length +1];
strcpy( s.sPtr, temp );
//如果s不是空指针,则复制内容
}
else s.sPtr [0] = '/0';
return input;
}
//************* 333***********
//************* 666***********
private:
int length;
//字符串长度(不超过100个字符)
char * sPtr;
//指向字符串起起始位置
};
//proj3.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "proj3.h"
int main ()
{
void writeToFile (char * );
MiniString strl ( "Happy" );
cout << str1 << "/n";
writeToFile ("");
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程prog3,其中声明的MyString类是一个用于表示字符串的类。成员函数endsWith的功能是判断此字符串是否以指定的后缀结束,其参数s用于指定后缀字符串。如果参数s表示的字符串是MyString对象表示的字符串的后缀,则返回true;否则返回false。注意,如果参数s是空字符串或等于MyString对象表示的字符串,则结果为true。例如,字符串“cde”是字符串“abcde”的后缀,而字符串“bde”不是字符串“abcde”的后缀。请编写成员函数endsWith。在main函数中给出了一组测试数据,此种情况下程序的输出应为:s1=abcdes2=cdes3=bdes4=s5=abcdes6=abcdefs1endsWiths2:trues1endsWiths3:falses1endsWiths4:trues1endsWiths5:trues1endsWiths6:false要求:补充编制的内容写在“//**********333**********”与“//**********666**********”之间。不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中,输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//Mystring.h#include<iostream>#include<string.h>usingnamespacestd;classMyString{public:MyString(constchar*s){Size=strlen(s);str=newchar[size+1];strcpy(str,s);}~MyString()(delete[]str;}boolendsWith(constchar*s)const;private:char*Str;intsize;};voidwriteTOFile(constchar*);//main.cpp#include"MyString.h"boolMyString::endsWith(constchar*s)const{//********333********//********666********}intmain(){chars1[]="abcde";chars2[]="cde";chars3[]="bde";chars4[]=…';chars5[]="abcde";chars6[]="abcdef";MyStringstr(s1);cout<<"s1="<<s1<<end1<<"s2="<<s2<<end1<<"s3="<<s3<<end1<<"s4="<<s4<<end1<<"s5="<<s5<<end1<<"s6="<<s6<<end1;cout<<b001aipha<<"s1endSWiths2:"<<Str.endsWith(s2)<<end1<<"s1endSWiths3:"<<Str.endsWith(s3)<<end1<<"s1endSWiths4:"<<Str.endsWith(s4)<<end1<<"s1endSWiths5:"<<Str.endsWith(s5)<<end1<<"s1endSWithS6:"<<Str.endsWith(s6)<<end1;writeToFile("");return0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called. The value is 10 Max number is 20 Destructor called. 注意:只能修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。 // proj1.cpp #include <iostream> using namespace std; class MyClass public: //ERROR**********found********** void MyClass(int i) value=i;cout<<"Constructorcalled."<<endl; int Max(int x,int y)return x>y?x:y;//求两Ai整数的最大值 //ERROR**********found********** int Max(int x,int y,int z=0) //求三个整数的最大值 if(x>y) return x>z?x:z; else return y>z?y:z; int GetValue()constreturn value; ~MyClass()cout<<"Destructorcalled."<<endl; private: int value; ; int main () MyClass obj(10); cout<<"The value is"<<value()<<endl; cout<<"Max number is"<<obj.Max(10,20)<<endl; return 0;
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABCDEFG“反转”后,得到字符串GFEDCBA。请编写成员函数reverse。在main函数中给出了一组测试数据,此时程序运行中应显示:
读取输入文件...
---反转前---
STR1=ABCDEF
STR2=ABCDEFG
---反转后---
STR1=FEDCBA
STR2=GFEDCBA
要求:
补充编制的内容写在“//********333********”与“//*********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中,输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//mgsering.h
#include<iostream>
#include<cstring>
using namespace std;
class MyString{
public:
MyString(const char*s)
{
str=new char[strlen(s)+1];
strcpy(str, s);
}
~MyString() {delete [] str; }
void reverse();
friend ostream
return os;
}
private;
char*str;
};
void writeToFile(char*, const MyString
//main.cpp
#include"mystring.h"
#include<fstream>
void MyString::reverse()
{
//******333******
//*******666******
}
int main()
{
char inname[128], pathname[80];
strcpy(pathname, "");
sprintf(inname, "in.dat", pathname);
cout<<"读取输入文件.../n/n";
ifstream infile (inname);
if(infile.fail()){
cerr<<"打开输入文件失败!";
exit(1);
}
char buf[4096];
infile.getline(buf, 4096);
MyString str1("ABCDEF"), str2("ABCDEFG"), str3(buf);
cout<<"---反转前---/n";
cout<<"STR1="<<str1<<endl;
cout<<"STR2="<<str2<<endl<<endl;
str1.reverse();
str2.reverse();
str3.reverse();
cout<<"---反转后---/n";
cout<<"STR1="<<str1<<endl;
cout<<"STR2="<<str2<<endl<<endl;
writeToFile(pathname, str3);
return 0;
}
问答题使用VC6打开考生文件夹下的工程test3_3,此工程包含一个源程序文件test3_3.cpp,其中建立了普通的基类base用于存储边的信息,建立派生类triangle和square,用于存储三角形和正方形的信息。按要求完成下列操作,将类定义补充完整。 (1)定义基类base的保护数据成员x和y用于记录边的长度,它们都是int型的数据。请在注释“//**1**”之后添加适当的语句。 (2)完成基类base默认构造函数的定义,把数据成员x和y分别初始化为参数a和b的值,要求使用作用域符“::”。请在注释“//**2**”之后添加适当的语句。 (3)完成派生类triangle函数disp的定义,使其以“三角形面积:”的格式将三角形的面积输出到屏幕上。请在注释“//**3**”之后添加适当的语句。 (4)添写派生类square的默认构造函数的定义,使其调用基类的构造函数,记录下正方形的边长信息a,其缺省值为0。请在注释“//** 4 **”之后添加适当的语句。 输出结果如下: 三角形面积:200 正方形面积:400 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件test3_3.cpp清单如下: #include<iostream.h> class base protected: //** 1 ** public: base(int a,int b) //** 2 ** virtual void disp()cout<<"调用base基类"<<endl;) ; class triangle:public base public: triangle(int a,int b):base(a,b); void disp() //** 3 ** ; class square:public base public: //** 4 ** void disp()cout<<"正方形面积:"<<x*y<<endl; ; void main() base *p; triangle t(20,20); square s(20); p= p->disp(); p= p->disp();
问答题基本操作
请使用"答题"菜单或使用VC6打开考生文件夹proj1下的工程proj1。其中在编辑窗口内显示的主程序文件中定义有Xabc类和主函数main。在程序文本中位于每行"// ERROR **********found**********"下面的一行有错误,请加以更正。更正后程序的运行结果为:s=20
注意:只允许修改每个"// ERROR **********found**********"下面的一行语句,不允许改动程序中的其他任何内容。
问答题使用VC6打开考生文件夹下的工程test18_3,此工程包含一个源程序文件test18_3.cpp,其中定义了用于表示雇员的类 Employee,但类Employee的定义并不完整。请按要求完成下列操作,将类Employee的定义补充完整。 (1)补充类Employee的构造函数,请使用字符串函数将数据成员name,address,city,province和zipcode分别初始化为参数*nm,*addr,*city,*prov和*zip的值。请在注释“//**1**”之后添加适当的语句; (2)完成类Employee的成员函数ChangeName(char * newName)的定义,将newName指向的内容赋值给数据成员name,请在注释“//**2**”之后添加适当的语句; (3)完成类Employee的成员函数Display()的定义,先使用指针buffer动态申请一个char型的200单位大小的空间,然后使用字符串函数将数据成员name和address依次复制其中,最后返回该指针buffer,将请在注释“//**3**”之后添加适当的语句。输出结果如下: 王东建国路大街105号 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件test18_3.cpp清单如下: #include <iostream.h> #include <string.h> class Employee protected: char name[20]; char address[100]; char city[20]; char province[20]; char zipcode[10]; public: Employee(char *nm, char *addr,char *city, char *prov, char *zip); void ChangeName(char *newName); char *Display(); ; Employee::Employee(char *nm, char *adr, char *cit, char *prov, char *zip) //**1** strcpy(city, cit); strcpy(province,prov); strcpy(zipcode,zip); void Employee::ChangeName(char * newName) //**2** char *Employee::Display() //**3** strcat(buffer, address); return buffer; void main() Employee emp("李华","建国路大街105号","石家庄","河北","103400"); emp. ChangeName ("王东"); cout<<emp.Display()<<end1;
问答题请编写一个函数prim(int num),该函数实现判别参数num是否为素数,在主函数中利用prime()函数验证哥德巴猜想——任何比2大的偶数都可表示为两个素数之和基本功能,根据main函数的调用情况给出正确的返回值。
注意:部分源程序已存在文件test36_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数prim的花括号中填写若干语句。
文件test36_1.cpp的内容如下:
#include <iostream.h>
const LEN=100;
int prim(int num)
{
}
void main()
{
int a=7;
int cnt=0;
cout<<"a is 7:/n";
int *s;
s=new int[LEN];
for(int i=2;i<a;i++)
{
if(!prim(i))
{
s[cnt]=i;
cnt++;
}
}
for (i=0;i<cnt;i++)
{
for (int j=i+1;j<cnt;j++)
{
if (s [i] +s [j] ==a)
cout<<s[i]<<'/t'<<s[j]<<'/t'<<end1;
}
}
}
问答题cout<<s[i]<<'/t'<<s[j]<<'/t'<<end1;
