问答题使用VC6打开下的源程序文件modi2.cpp。阅读下列函数说明和C代码,完成空出部分的程序,实现函数fun(char*s1,char*s2)将字符串s2连接在字符串s1之后,构成一个首指针为s1的字符串。注意:不能修改函数的其他部分。#include<iostream.h>#defineMAXLINE1000voidfun(char*sl,char*s2){}voidmain(){chars1[MAXLINE];chars2[256];cout"pleaseinputastring:"endl;cin.getline(s1,MAXLINE);cout"pleaseinputotherstring:"endl;cin.getline(s2,256);fun(s1,s2);couts1endl;return;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明IntSet是一个用于表示正整数集合的类。IntSet的成员函数Intersection的功能是求当前集合与另一个集合的交集。请完成成员函数Intersection。在main函数中给出了一组测试数据,此时程序的输出应该是:求交集前:12358102891l305667求交集后:12358102891130566728要求:补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//Intset.h#include<iostream>usingnamespacestd;constintMax=100;clasSIntSet{public:IntSet()//构造一个空集合{end=-1;}IntSet(inta[],intsize)//构造一个包含数组a中size个元素的集合{if(Size>:Max)end=Max-1;elseend=size-1;for(inti=0;i<=end;i++)element[i]=a[i];}boolIsMemberOf(inta)//判断a是否为集合中的一个元素{for(inti=0;i<=end;i++)if(element[i]==a)returntrue;returnfalse;}intGetEnd(){returnend;}//返回最后一个元素的下标intGetElement(inti){returnelement[i];}//返回下标为i的元素IntSetIntersection(IntSet&set);//求当前集合与集合set的交voidPrint()//输出集合中的所有元素{for(inti=0;i<=end;i++)if((i+1)%20==0)cout<<element[i]<<end1;elsecout<<element[i]<<'';cout<<end1;}private:intelement[Max];intend;};voidwriteToFile(constchar*);}intmain(){inta[]={1,2,3,5,8,i0);intb[]={2,8,9,ii,30,56,67);IntSetsetl(a,6),set2(b,7),set3;cout<<"求交集前:"<<end1;setl.Print(),set2.Print();set3.Print();set3=set1.Intersection(set2);cout<<end1<<"求交集后:"<<end1;set1.Print();set2.Print();set3.Print();writeToFile(””);return0;}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错误,请改正其中的错误,使程序正确运行。并且使程序输出的结果为: OK 注意:错误的语句在//********error********的下面,修改该语句即可。#include<iostream.h>Class CBaSe{public: CBaSe() { a=b=0; )private: int a,b;};class CDerivel:public CBase{public: CDerivel() { } //********error******** virtual void func();};class CDerive2:public CDerivel{public: CDerive2() { a=0; b=0; } Void func() { //********error******** cout<<"OK"<endl: }private: int a; int b;};Void main(){ CDerive2 obj; //********error******** CDerivel *p =(CDerivel*)obj; p->func();}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,该工程中包含程序文件main.cpp,其中有关TVSet(“电视机”)和主函数main的定义。程序中位于每个“//ERROR**********found**********”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: 规格:29英寸,电源:开,频道:5,音量:18 规格:29英寸,电源:关,频道:一1,音量:一1 注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;class TVSet{ //“电视机”类 const int Size; int channel; //频道 int volume; //音量 bool on; //电源开关:true表示开,false表示关public://ERROR*********found********* TVSet(int size){ this一>size(size); channel=0; volume=15; on=false; } int getSize()const{return size;) //返回电视机规格 bool isOn()const{return on;) //返回电源开关状态 //返回当前音量,关机情况下返回一1 int getVolume()const{return isOn ()?volume:一1;) //返回当前频道,关机情况下返回一1 int getChannel()const{return isOn ()?channel:一1;) //ERROR*********found********* void turnOnOff()const//将电源在“开”和“关”之间转换 {on=!on;) void setChannelTo(int chan){ //设置频道(关机情况下无效) if(isOn()“关”) <<”,频道:”<<getChannel() <<”,音量:”<<getVolume()<<endl; } }; int main(){ TVSet tv(29); tv.turnOnOff(); tv.setChannelTo(5); tv.setVolumeTo(18); tv.show state(); tv.turnOnOff(); tv.shoW_State(); return 0; }
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成proj3.h,重载复合赋值运算符+=。
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//proj3.h
#include <iostream>
#include <iomanip>
using namespace std;
class MiniString
{public:
friendostream return output;
}
friend istream //用于输入的临时数组
temp[0] = '/0';
input >> setw(100 ) >> temp;
int inLen = strlen(temp); //输入字符串长度
if( inLen!= 0)
{
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';
//如果s是空指针,则为空字符串
return input;
}
MiniString ( const char * s=""):length (( s!= 0 )?strlen(s) : 0 ){ setString(s ); }
~MiniString() { delete [] sPtr; }//析构函数
//************* 333***********
//+=运算符重载
//************* 666***********
private:
int length; //字符串长度
char * sPtr; //指向字符串起始位置
void setString( const char * string2 ) //辅助函数
{
sPtr = new char [length + 1]; //分配内存
if ( string2 ! = 0 )
strcpy( sPtr, string2 );
//如果string2不是空指针,则复制内容
else smtr [0] = '/0'; //如果string2是空指针,则为空字符串
}
};
//proj3.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "proj3.h"
int main ()
{
MiniString str1 ("World"), str2 ("Hello") ;
void writeToFile (char * ) ;
str2 + = str1; //使用重载的+=运算符
cout << str2 << "/n";
writeToFile ("");
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,输出的结果为: Constructor,i=0, Destnlctor 注意:错误的语句在//********error********的下面,修改该语句即可。#include<iostream.h>class CObj{ int i;public: CObj(); void display(); ~CObj();};//********error********CObj:CObj(){ cout<<"ConstruCtor"<<","; i=0;}//********error********CObj:display(){ cout<<"i="<<i<<","<<endl;}//********error********CObj:CObj(){ cout<<"Destructor"<<endl;}Void main(){ CObj a; a.display();}
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为:
(1,2,3,4,5)
(0,0,0,0,0,0)
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class VectorBase { //向量基类,一个抽象类
int len;
public:
VectorBase (int fen): len(len) {}
int length() const {return len;} //向量长度,即向量中元素的个数
virtual double getElement (int i) const = 0; //取第i各元素的值
virtual double sum() const = 0; //求所有元素的和
void show() const { //显示向量中所有元素
cout << "(";
for (int i =0; i < length() - 1; i++)
cout << getElement(i) << ",";
// ********found********
cout <<______<< ")" << endl; //显示最后一个元素
}
};
class Vector:public VectorBase { //向量类
double * val;
public:
Vector(int fen, double v[] = NULL):VectorBase(len) {
val = new double[len];
for(int i = 0; i < len; i++)
val[i] = (v == NULL? 0.0:v[i]);
}
// **********found**********
~Vector() {______;}
double getElement(int index) const {return val[index];}
double sum() const {
double s = 0.0;
// **********found**********
for (int i = 0; i < length(); i++)
return s;
}
};
class ZeroVector: public VectorBase { //零向量类
public:
ZeroVector(int len):VectorBase(len) {}
// **********found**********
double getElement(int index) const {______;}
double sum() const {return 0.0;
}
};
int main() {
VectorBase * v;
double d[] = {1,2,3,4,5};
v = new Vector(5,d);
v -> show();
delete v;
v = new ZeroVector(6);
v -> show();
delete v;
return 0;
}
问答题使用VC++6.0打开考生文件夹下的源程序文件2.cpp。请完成函数fun(int x)的定义,该函数功能是判定x的所有约数,并且在函数中调用写函数WriteFile将结果输出到2.txt文件中。 例如:x=100的约数为1 2 4 5 10 20 25 50 100。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include<iostream> #include<fstream> #include<cmath> using namespace std; void WriteFile(int c) ofstream out1; out1.open("2.txt",ios_base::binary|ios_base::app); out1<<c<<''; out1.close(); void fun(int x) void ClearFile() ofstream out1; out1.open("2.txt"); out1.close(); int main() ClearFile(); fun(100); return 0;
问答题用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了多个类,但类不能达到输出要求,按照定义的输出修改函数,使得输出如下: TestClass3 TestClass2 TestClass1 其中定义的类并不完整,按照要求完成下列操作,将类的定义补充完整。 (1)类TestClass2公共继承于TestClass1,请在注释//********1********后添加适当的语句。 (2)print函数为虚函数,请在注释//********2********后添加适当的语句。 (3)修改语句,使得P指定对象obj3,请在注释//********3********后添加适当的语句。 (4)修改语句,利用P调用print()函数,完成输出信息,注释//********4********后添加适当的语句。 注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。#include<iostream>using namespace std;Class TestClass1{public: void print(){ cout<<"TestClass1"<<endl; } }; //********1********Class TestClass2 { public: //********2******** void print() { cout<<"TestClass2"<<endl; } }; class TestClass3:public TestClass2 { public: void print() { cout<<"TestClass3"<<endl; }};int main(){TestClass2 obj2;TestClass3 obj3;TestClass1*p;obj2.print();obj3.print();//********3********p=obj 3;//********4********p.print();return 0;}
问答题return 0;
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成列操作,将类的定义补充完整,实现以下功能:
(1)完成CBook类构造函数,对整型变量ID和作者Author进行赋值,请在注释∥********1********后添加适当的语句。
(2)完成类CBooks的析构函数,释放申请的内存,请在注释∥********2********后添加适当的语句。
(3)完成类CBooks的AddBookMember函数,请在注释∥********3********后添加适当的语句。
(4)完成CBooks类,用于由书的ID检索到作者的函数char*GetBookAuthor(int nlD),请在注释∥********4********后添加适当的语句。
(5)程序的输出结果为:
Tom
Harry
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include
#include
Class CBook
{
public:
int ID;
char Author[32];
public:
CBook(int ID Number,char*Author Name)
{
thiS一>ID=ID Number;
∥********1********
}
};
C1ass CBooks
{
private:
Class NOde
{
public:
Node*next;
CBook*book;
}*m pBook;
public:
CBooks()
{
m pBook=NULL:
}
~CBooks()
{
∥********2********
while()
{
Node*P =m
pBook一>next;
delete m pBook
一>book;
delete m pBook;
m—pBook=P;
}
}
int AddBoo kMenber(int
nID,char*Author)
{
Node*p=m—pBook;
Node*q=NULL;
∥********3********
while()
{
if f nID==P一>
book一>ID)
{
return 0;
}
q=p;
P=p一>next;
}
if(P==NULL)
{
P=new Node;
P一>next=NULL;
P—>book= new
CBook(nID,Author);
}
if(q=NULL)
{
m pBook=P;
}
else
{
q一>next=p;
}
return 1;
}
char*GetBookAuthor(int nID)
{
Node*p=m pBook;
∥********4********
while()
{
if(P一>book一>ID==nID)
{
return P一>
book一>AUthor;
}
P=P一>next;
}
return 0;
}
};
int main()
{
CBooks booksl;
booksl.AddBookMenber (1,“Tom”);
books 1.AddBookMenber (3,“Lee”);
books 1.AddBookMenber (4,“Lily”);
bookS 1.AddBookMenber(5,“Harry”);
cout<
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char* des,char* str,char c,char* str2)的功能是:
如果str中包含字符“!”,则替换成"a";
如果str中包含字符“
char* str ="!
char* str2="jklm";
convert(dest,str,str2);
cout
dest
endl;
return;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,通过把类Distance定义为类Point的友元类,来实现计算两点之间距离的功能。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。(1)把类CDistance定义为类CPoint的友元类。请在注释//********1********之后添加适当的语句。(2)定义类CPoint的构造函数,完成给私有数据成员x和y的赋值,并且两个参数的缺省值都为0。请在注释//********2********之后添加适当的语句。(3)完成类CDistance的成员函数length(Point&pPoint,Point&qPoint)的定义,并在其中计算点p、q之间的距离,并且返回结果。假设两点之间的距离distance=sqrt((pPoint.x-qPoint.x)*(pPoint.x-qPoint.x)+(pPoint.y-qPoint.y)*(pPoint.y-qPoint.y))。请在注释//********3********之后添加适当的语句。注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream>#include<cmath>using namespace std;class CPoint{public://********1********//定义类Point的构造函数//********2********void display(){cout<<"x="<<x<<endl;cout<<"y="<<y<<endl;}private:float x,y;};class CDiStance{public:float length(CPoint&p,CPoint &q);};float CDistance::length(CPoint&pPoint,CPoint&qPoint){//********3********}int main(){CPoint mPoint(10,10),nPoint(40,50);mPoint.display();nPoint.display();CDistance len;cout<<len.length(mPoint,nPoint)<<endl;return0;}
问答题使用VC6打开下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,即使程序在界面输出以下类似内容:0012FF6810012FF3C50012FF6C20012FF4440012FF7030012FF4C30023FF7440012FF5420012FF7850012FF5C1006AFDCC~006AFDEF数据可能因为机器的不同,程序的输出的内容会有差异。只要相差为4的连续地址即可。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream>usingnamespacestd;intmain(){int*p,a[5]={1,2,3,4,5};double*q,b[5]={5.0,4.0,3.0,2.0,1.0};intx;p=a;q=b;//********error********for(x=0;x<=5;x++)//********error********coutp+x""(p+x)""q+x""(q+x)endl;return0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called. The value is 10 Copy constructor called. The value is 10 Destructor called. Destructor called. 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。 //proj1.cpp #include <iostream> using namespace std; class MyClass public: //ERROR **********found********** MyClass(int i) value=i;cout<<"Construc-tor called."<<endl; //ERROR **********found********** MyClass(const MyClass p) value=p.value; cout<<"Copy constructorcalled."<<endl; void Print () cout<<"The value is"<<value<<endl; //ERROR **********found********** void ~MyClass() cout<<"Destructor called." <<endl; private: int value; ; int main() MyClass obj1; obj1.Print(); MyClass obj2(obj1); obj2.Print(); return 0;
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp。本程序要求输入一字符串,然后将其中的小写字母改变成大写字母,字符可能输入多行,当输入字符“?”时,默认字符输入结束,最后把结果输出到界面。
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在
∥********error********的下面。
#include
Void main()
{
int i=0;
char Str[12 0];
cout=‘a’)
str[i] =str[i] 一
‘a’+‘A’;
i++;
}
cout<
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程中包含一个头文件number.h,其中包含了类N啪ber、OctNumber、HexNumber和DecNumber的声明;包含程序文件number.cpp,其中包含了上述类的成员函数toString的定义;还包含程序文件proj2.cpp,它以各种数制格式显示输出十进制数11。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为:013,11,OXB注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//mumber.hclassNumber{protected:intn;staticcharbuf[33];public:Number(intk):n(k){)//**********found**********________;//纯虚函数toString的声明};classHexNumber:publicNumber{//16进制数public://**********found**********________//构造函数,参数名为kconstchar*toString()const;};classOctNumber:publicNumber{//八进制数public:OctNumber(intk):Number(k){}constchar*toString()const;};classDecNumber:publicNumber{//十进制数public:DecNumber(intk):Number(k){}constchar*toString()const;},/Imumber.cpp#include"Number.h"#include<iostream>#include<iomanip>#include<strstream>usingnamespacestd;charNumber::buf[33]="";constchar*HexNumber::toString()const{strstreamstr(buf,33);str<<hex<<uppercase<<showbase<<n<<ends;returnbuf;}constchar*OctNumber::toString()const{strstreamstr(buf,33);str<<oct<<showbase<<n<<ends;returnbuf;}constchar*DecNumber::toString()const{strstreamstr(buf,33);Str<<dec<<n<<ends;returnbuf;}________;//按既定的数制显示输出参数对象number的值}intmain(){show(OctNumber(ii));cout<<',';show(DecNumber(11));cout<<',';//**********found**********________;//以16进制格式输出十进制数11cout<<end1;return0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中包含源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句行有错误,请加以改正。改正后程序的输出结果应该是:
书名:C++语言程序设计 总页数:299
已把“C++语言程序设计”翻到第50页
已把“C++语言程序设计”翻到第51页
已把书合上。
书是合上的。
已把“C++语言程序设计”翻到第1页
注意:只修改每个“//ERROR ****found****”下的一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Book{
char * title;
int num_pages; //页数
int cur page; //当前打开页面的页码,0表示书未打开
public:
Boek(const char * theTitle, int pages):num pages(pages)
{
//ERROR ********** found**********
title =new char[strlen(theTitle)];
strcpy(title, theTitle);
cout <<endl <<"书名:"<<title
<<"总页数:" <<num_pages;
}
Book{){ delete []title; }
// ERROR ********** found**********
bool isOpen () const { return num_pages!=0;} //书打开时返回true,否则返回false
int numOfPages()const{ return num_pages;} //返回书的页数
int currentPage()const{ return cur_page;} //返回打开页面的页码
void openAtPage(int page no){ //把书翻到指定页
cout << endl;
if(page_no <1 ||page_no >num_pages){
cout <<"无法翻到第" <<cur_page<<"页。";
close();
}
else{
cur_page = page_no;
cout <<"已把“"<< title <<"”翻到第" << cur_page <<"页";
}
}
void openAtPrevPage{) { openAtPage (cur page-1); } //把书翻到上一页
void openAtNextPage () { openAtPage (cur_page +1); } //把书翻到下一页
void close () { //把书合上
cout << endl;
if(! isOpen ())
cout << "书是合上的。";
else{
//ERROR ********** found**********
num_pages =0;
cout <<"已把书合上。";
}
cout << endl;
}
};
int main () {
Book book ("C++语言程序设计", 299);
book.openAtPage (50);
book.openAtNextPage ();
book.close ();
book.close ();
book.openAtNextPage ();
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有“房间”类Room及其派生出的“办公室”类Office的定义,还有主函数main的定义。请在程序中“//****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 办公室房间号:308 办公室长度:5.6 办公室宽度:4.8 办公室面积:26.88 办公室所属部门:会计科 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>using namespace std;class Room{ //“房间”类 int room no; //房间号 double length; //房间长度(m) double width; //房间宽度(m)public: Room(int the room_no,double the_length,double the_width):room_no(the_room_no),length(the_length),width(the_width){) int theRoomNo()const{return room_no;}//返回房间号 double theLength()const{return length;) //返回房间长度 double theWidth()const{return width;} //返回房间宽度 //********** found********** double theArea()const{______}//返回房间面积(矩形面积)};class Office:public Room{//“办公室”类 char*depart; //所属部门public: Office(int the room no,double the length,double the width,const char*thedepart)//********** found********** :_________{ depart=new char | strlen(the depart)+1 j;//**********found********** strcpy(________); } 一Office(){delete[]depart;) const char*theDepartment()const {return depart;}//返回所属部门};int main(){ //**********found********** Office__________; cout<<”办公室房间号:"<<an office.theRoomNo()<<endl <<”办公室长度:”<<an_office.the-Length()<<endl <<??办公室宽度:”<<an_office.the-Width()<<endl <<--办公室面积:”<<an_office.theArea()<<endl <<”办公室所属部门:”<<an_office.theDepartment()<<endl; return 0;}
问答题请使用VC6或使用【答题】菜单打开
proj1下的工程proj1。该工程中包含程序文件main.cpp,其中有类CDate(“日期”)和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
原日期:2005-9-25
更新后的日期:2006-4-1
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cstdlib>
using namespace std;
class CDate //日期类
{
// ERROR ******found******
protected:
CDate() {};
CDate(int d, int m, int y)
{
// ERROR ******found******
SetDate (int day = d, int month=m, int year=y);
};
void Display(); //显示日期
void SetDate (int day, int month, int year)
//设置日期
{m_nDay = day; m_nMonth = month; m_nYear = year;}
private:
int m_nDay; //日
int m_nMonth; //月
int m_nYear; //年
};
voidCDate::Display() //显示日期
{
// ERROR ******found******
cout << m_nDay << "-" << m_nMonth << "-" << m_nYear;
cout << endl;
}
int main ()
{
CDate d (25,9,2005); //调用构造函数初始化日期
cout << "原日期:";
d.Display();
d.SetDate(1,4,2006); //调用成员函数重新设置日期
cout << "更新后的日期:";
d. Display();
return 0;
}