请打开考生文件夹下的解决方案文件proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵则调用max_value函数,返回值为3。请编写成员函数max_value。要求:补充编制的内容写在“//*******333*******”与“//*******666*******”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为Obj文件,并且在本程序中调用。//Matfix.h#include#includeusing namespace std;const int M=18;conSt int N=18;Class Matrix{int array[M][N];public:Matrix( ){}int getElement(int i,int j)const{return array[i][j];}VOid setElement(int i,int j,int value){array[i][j]=value;}int max value( )const;void show(const char*s)const{coutfor(int i=0;icoutfor(int j=0;jcout[j];}}};void readFromFile(const char*,Matrix);void writeToFile(char*,const Matrix);//main.cpp#include"Matrix.h"#includevoid readFromFile(const char*f,Matrixm){ifstream infile(f);if(infile.fail( )){cerrint k;for(int i=0;ifor(int j=0;jinfile>>k;m.setElement(i,j,k);}}int Matrix::max_value( )const{//*******333*******//*******666*******}int main( ){Matrix m;readFromFile(" ",m);m.show("Matrix:");coutwriteToFile(" ",m);return0;}
请打开考生文件夹下的解决方案文件proj3,其中定义了用于表示特定数制的数的模板类Number和表示一天中的时间的类TimeOfDay;程序应当显示: 01:02:03.004 06:04:06.021 但程序中有缺失部分,请按照以下的提示,把缺失部分补充完整: (1)在“//**1** ****found****”的下方是一个定义数据成员seconds的语句,seconds用来表示“秒”。 (2)在“//**2** ****found****”的下方是函数advanceSeconds中的一个语句,它使时间前进k秒。 (3)在“//**3** ****found****”的下方是函数advance中的一个语句,它确定增加k后n的当前值和进位,并返回进位。例如,若n的当前值是表示时间的55分,增加10分钟后当前值即变为5分,进位为1(即1小时)。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。填写的内容必须在一行中完成,否则评分将产生错误。//proj3.cpp#includeiostream#includeiomanipusing namespace std;templateint base //数制为base的数class Number{ int n; //存放数的当前值public:Number(int i):n(i){)//i必须小于base int advance(int k);//当前值增加k个单位 int value()const{return n;}//返回数的当前值};Class TimeOfDay{public: Number24hours ; //小时(0~23) Number60minutes;//分(0~59)//**1** *******found*******____________;//秒(0~59)Number1000milliseconds;//毫秒(0~999) TimeOfDay(int h=0,int m=0,int s=0,int milli=0) :hourS(h),minuteS(m),Seconds(S),milliseconds(milli){} void advanceMillis(int k){advanceSeconds (milliseconds.advance(k));} //前进k毫秒 void advanceSeconds(int k) //前进k秒 {//**2** *******found*******___________;}void advanceMinuteS(int k){advanceH0ur(minutes.advance(k));}//前进k分钟 void advanceH0ur (int k) {hours.advance(k);) //前进k小时 void show()const{//按“小时:分:秒.毫秒”的格式显示时间 int C=cout.fiii('0');//将填充字符设置为'0' coutsetw(2)hours.value()':' //显示小时 Setw(2)minuteS.value()':' //显示分 setw(2)Seconds.value()'.' //显示秒 setw(3)milliseconds.value(); //显示毫秒 cout.fill(c); //恢复原来的填充字符 }};templateint baseint NumberbaSe::advance(int k){ n+=k; /增加k个单位 int s=0 ; //s用来累计进位//**3** *******found*******while(n=base)_____________//n到达或超过base即进位 return s; //返回进位}int main(){ TimeOfDay time(1,2,3,4);//初始时间:1小时2分3秒4毫秒 time.show(); //显示时间 time.advanceHour (5); //前进5小时 time.advanceSeconds(122);//前进122秒(2分零2秒) time.advanceMillis(1017);//前进1017毫秒(1秒零17毫秒) coutendl; time.show(); //显示时间 coutendl: return 0;}
下面正确的函数定义是( )。
使用VC6打开考生文件夹下的源程序文件modil.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********的下面。#includeiostreamusing 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******** coutP+x “ ” (p+x)“”q+x“”(q+x)end1; return 0;}
请打开考生文件夹下的解决方案文件proj2,该工程中含有一个源程序文件proj2.cpp,请将堆栈类的定义补充完整。使程序的输出结果为:The element of stack are:4 3 2 1注意:请勿修改主函数main和其他函数中的任何内容,只在横线处编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//proj2.cpp#includeusing namespace std;const int Size=5;class Stack;class Item{public://********found********Item(const intval):_______{}//构造函数对item进行初始化private:int item;Item*next;friend class Stack;};Class Stack{public:Stack( ):top(NULL){}~Stack( );int Pop( );void Push(const int);private:Item*top;};Stack::~Stack( ){Item*p=top,*q;while(p!=NULL)(q=p->next;//********found********_______;//释放p所指向的节点p=q;}}int Stack::Pop( ){Item*temp;int ret;//********found********_______;//使temp指向栈顶节点ret=top->item;top=top->next;delete temp;return ret;}void Stack::Push(const intval){Item*temp=new Item(val);//********found********_______;//使新节点的next指针指向栈顶数据top=temp;}int main( ){Stack s;for(int i=1;is.Push(i);coutfor(i=1;icoutreturn0;}
请打开考生文件夹下的解决方案文件proj1,此工程中包含程序文件main.cpp,其中有ElectricFan(“电风扇”)类和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 品牌:清风牌,电源:关,风速:0 品牌:清风牌,电源:开,风速:3 品牌:清风牌,电源:关,风速:0 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。#includeiostreamusing namespace std;class ElectricFanf //“电扇”类 char*brand; int intensity; //风速:0一关机,1一弱,2一中,3一强public: ElectricFan(const char*the_brand):intensity(0){ brand=new char[strlen(the brand)+1]; strcpy(brand,the brand); } ~ElectricFan(){delete[]brand;)//ERROR ********found******** const char*theBrand()const{return*brand;}//返回电扇品牌 int theIntensity()const{return intensity;}//返回风速 bool isOn()const{return intensity0;}//返回电源开关状态//ERROR ********found******** void turnOff(){intensity=1 ;}//关电扇 void setIntensity(int inten){//开电扇并设置风速//ERROR ********found******** if(intensity=1intensity=3) intensity=inten; } void show(){ cout"品牌:"theBrand()"牌" ",电源:"(isOn()?"开":"关") ",风速:"theIntensity()endl ; }};int main(){ElectricFan fan("清风");fan.show();fan.SetIntenSity(3);fan.show();fan.turnOff();fan.show();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,并把rectl 的值赋给rect2。请在注释//********4********之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。1 #include2 class CRect3 {4 private:5 //********1********67 public:8 //********2********910 //********3********1112 vold SetPoints(double,double,double,double);13 void SetLeftPoint(double m){leftPoint=m;}14 void SetRightP0int(double m)( rightPoint=m;}15 void SetTopPoint(double m){topPoint=m;}16 void SetBottomPoint(double m){bottomPoint=m;}17 void Display();18 };1920 CRect::CRect(double 1,double t,double r,double b)21 {22 leftPoint=1; topPoint=t;23 rightPoint=r;bottomPoint=b;24 }25 void CRect::SetPointS(double 1,double t,double r,double b)26 {27 leftPoint=1;topPoint=t;28 rightPoint=r;bottomP0int=b;29 }30 void CRect::Display()31 {32 cout33 cout34 }35 void main() {36 CRect rect0;37 rect0.Display();38 rect0.SetPoints(20,20.6,30,40);39 rect0.Display();40 CRect rect1(0,0,150,150);41 rect1.SetTopPoint(10.5);42 rect1.SetLeftPoint(10.5);43 //********4********4445 rect2.Display();46 }
请打开考生文件夹下的解决方案文件proj3,本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成晕载赋值运算符函数,完成深复制功能。 屏幕上输出的正确结果应该是: Hello! Happy new year! 要求: 补充编制的内容写在“//**********333**********”与“//**********666**********”两行之间。不得修改程序的其他部分。 注意: 程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//proj3.h#includeiostream#includeiomanipusing namespace std;class Ministring{ public: friend ostreamoperator(ostreamoutput,const Minis-trings)//重载流插入运算符 { outputs.sPtr; return output; } friend istreamoperator(istreaminput,Ministrings)//重载流提取运算符 {char temp[100];//用于输入的临时数组 temp[0]='\0';//初始为空字符串 inputsetw(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; } void modstring(const char*string2)//更改字符串内容 { if(string2 !=0)//如果string2不是空指针,则复制内容 { if(strlen(string2)!=length) { length=strlen(string2); delete[]sPtr; sPtr=new char[length+1]; //分配内存 } strcpy(sPtr,string2); } else sPtr[0]='\0';//如果string2是空指针,则为空字符串 } Ministring operator =( const Ministringotherstring); Ministring(const char * s =" "):length((s!=0)?strlen(s):0)//构造函数 { sPtr=0 ; if(length!=0) setstring(S); } ~Ministring()//析构函数 {delete[]sPtr;} private: int length; //字符串长度 char * sPtr; //指向字符串起始位置 void setstring(const char * string2) //辅助函数 { sPtr=new char[strlen(string2)+1]; //分配内存 if(string2 !=0)strcpy(sPtr,string2);//如果string2不是空指针,则复制内容 else sPtr[0]='\0';//如果string2是空指针,则为空字符串 }};//proj3.cpp#includeiostream#includeiomanipusing namespace std;#include"proj3.h"MinistringMinistring::operator=(const Ministringotherstring){//重载赋值运算符函数。提示:可以调用辅助函数setstring//*************333*************//*************666*************}int main(){ Ministring strl("Hello!"),str2; void writeToFile(const char *); str2=str1 ;//使用重载的赋值运算符 str2.modstring("Happy new year!”); coutstr1'\n': coutstr2'\n'; writeToFile(" "); return 0 ;}
请打开考生文件夹下的解决方案文件proj3,其中包含了类Integers和主函数main的定义。一个In—tegers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数remove的作用是从集合中删除指定的元素(如果集合中存在该元素),成员函数sort的作用是将集合中的整数按升序进行排序。清编写这个sort函数。此程序的正确输出结果应为: 5 28 2 4 5 3 2 75 27 66 31 5 28 2 4 5 3 2 75 27 66 31 6 5 28 2 4 5 3 2 75 27 66 31 6 19 5 28 4 5 3 2 75 27 66 31 6 19 5 28 4 5 3 2 75 27 66 3l 6 19 4 2 3 4 4 5 5 6 19 27 28 3l 66 75 要求: 补充编制的内容写在“//*********333*********”与“//*********666*********”之间。不得修改程序的其他部分。 注意:相关文件包括:main.epp、Integers.h。 程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//Integers.h#ifndef INTEGERS#defime INTEGERS#includeiostreamusing namespace std;conSt int MAXELEMENTS=100;//集合最多可拥有的元素个数class Integems{ int elem[MAXELEMENTS];//用于存放集合元素的数组 int Counter;//用于记录集合中元素个数的计数器public: Integers():counter(0){)//创建一个空集合 Integers(int data[],intsize);//利用数组提供的数据创建一个整数集合 void add(int element);//添加一个元素到集合中 void remove(int element);//删除集合中指定的元素 int getCount()const{return counter;)//返回集合中元素的个数 int qetElement(int i)const{return elem[i];)//返回集合中指定的元素 void sort();//将集合中的整数按由小到大的次序进行排序 void show()const;//显示集合中的全部元素};void writeToFile(conSt char*path);#endif//main.cpp#include"Integers.h"#includeiomanipIntegers::Integers(int data[],int size):counter(0){ for(int i=0;isize ; i++) add(data[i]); } void Integers:: add (int element){ if(counterMAXELEMENTS) elem[counter ++]=element;}void Integers::remove(int element){ int j; for(j=counter-1 ; j=0;j--) if(elem[j]==element) break; for(int i=j ; icounter-1;i++) elem[i]=elem[i+1]; counter--; } void Integers::sort(){ //*********333*********//*********666*********}void Integers::show()aonst{ for(int i=0;igetCount();i++) toutsetw(4)getElement(i); aoutendl;}int main(){ int d[]={5,28,2,4,5,3,2,75,27,66,31}; Integers s(d,11); s.Show(); s.add(6); s.show(); s.add(19); s.show(); s.Eemove(2); s.show(); s.add(4); s.show(); s.sort(); s.show(); writeToFile(" "); return 0;}
请打开考生文件夹下的解决方案文件pro]3,其中声明的CDeepCopy是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数operator:,以实现深层复制。 要求: 补充编制的内容写在“//********333********”与“//********666********”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//CDeepCopy.h#includeiostream#includestringusing namespace std;class CDeepCopy{public: int n; //动态数组的元素个数 int *p; //动态数组首地址 CDeepCopy(int); ~CDeepCopy(); CDeepCOpyoperator=(constCDeepCopyr);//m值运算符函数};void writeToFile(char*);//main.cpp#include"CDeepCopy.h"CDeepCopy::~CDeepCopy(){delete[]p;}CDeepCopy::CDeepCopy(int k){n=k;p=new int[n];)//构造函数实现CDeepCopy CDeepCopy:: operstor=(const CDeepCopyr)//赋值运算符函数实现{//********333********//********666********}int msin(){ CDeepCopy a(2),d(3); a.p[0]=1;d.p[0]=666;//对象a,d数组元素的赋值 { CDeepCopy b(3); a.p[0]=88 ; b=a;//调用赋值运算符函数 coutb.p[0];//显示内层局部对象的数组元素 coutd.p[0];//显示d数组元素a.p[0]的值cout"d fade away;\n";couta.p[0];//显示a数组元素a.p[0]的值 writeToFile(" "); return 0;}
请打开考生文件夹下的解决方案文件proj3,其中声明了ValArray类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此种情况下程序的输出应该是: ValArray v1={1,2,3,4,5} ValArray v2={2,2,2,2,2} 要求: 补充编制的内容写在“//*******333*******”与“//*******666*******”之间。不要修改程序的其他部分。 注意: 相关文件包括:main.cpp、ValArray.h。 程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//ValArray.h#includeiostreamusing namespace std;Class ValArray{ int * v; int size;public: ValArray(const int*p,int n):size(n) { v=new int[size]; for(int i=0;isize;i++) v[i]=p[i]; } ValArray(const ValArrayother); ~ValArray(){delete[]v;} void setElement(int i,int val) { v[i]=val ; } void print(ostreamout)const { out'{'; for(int i=0 ; isize-1;i++) out v[i] ”,”j out v[size-1] '}'; }};void writeToFile(const char *);//main.cpp#include"ValArray.h"ValArray::ValArray (const ValArrayother){//*********333*********//*********666*********}int main(){ const int a[]={1,2,3,4,5); ValArray v1(a,5); ValArray v2(v1); for(int i=0 ; i5;i++) v2.setElement(i,2); cout"ValArray v1="; v1.print(cout); coutend1: cout"ValArray v2="; v2.print(cout); coutendl: writeToFile(" "); return 0;}
请打开考生文件夹下的解决方案文件proj3,其中声明了SortedList类,是一个用于表示有序数据表的类。其成员函数insert的功能是将一个数据插入到一个有序表中,使得该数据表仍然保持有序。请编写这个insert函数。程序的正确输出应为: 插入前: 1,2,4,5,7,8,10 插入6和3后: 1,2,3,4,5,6,7,8,10 要求: 补充编制的内容写在“//*******333*******”与“//******666******”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//SortedList.h#includeiostreamusing namespace std;class SortedList {//有序数据表类 int len; double*d;public: SortedList(int len,doubledata[]=NULL); ~SortedList(){delete[]d;} int length()const{returnlen;)//有序数据表长度(即元素的个数) double getElement(int i)const{return d[i];) void insert(double data); void show () const;//显示有序数据表};void writeToFile (char *,const SortedList);//main.cpp#include"SOrtedList.h"SortedList::SortedList f intlen,double data[]):len(len){ d=new double[len]; for(int k=0 ; klen;k++) d[k]=(data==NULL?0.0:data[k]); for(int i=0;ilen-1;i++){ int m=i; for(int j=i;jlen ; j++) if(d[j]d[m])m=j; if(mi){ double t=d[m]; d[m]=d[i]; d[i]=t; } }}void SortedList::insert(double data){//******333******//******666******}void SortedList::show()const{//显示有序数据表 for(int i=0;ilen-1;i++) coutd[i]","; coutd[len-1]endl;}int main(){ double s[]={5,8,1,2,10,4,7}; SortedList list(7,s); cout"插入前:"endl; list.show(); list.insert(6.0); list.insert(3.0); cout"插入6和3后:"endl; list.show(); writeToFile(" ",list); return 0;}
请打开考生文件夹下的解决方案文件proj3,此工程中包含一个源程序文件proj3.epp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋值给myArray。类intArray重载了“=”运算符。程序中给出了一个测试数据文件input,不超过300个的整数。程序的输出是: 10 11 13 16 20 要求: 补充编制的内容写在“//********333********”与“//********666********”之间。实现重载赋值运算符函数,并将赋值结果在屏幕输出。格式不限。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//intArray.hclass intArray{private: int*array; int length;public: intArray(char*filename); intArray(); intArrayoperator=(const intArraysrc); ~intArray(); void show();};void writeToFile (const char * path);//main.cpp#includeiostream#includefstzeam#includecstring#include "intArray.h"using namespace std;intArray::ihtArray(){ lencsth=10: array=new int[lenc3th];intArray:: intArray (char*filename){ ifstream myFile(filename); array=new int[300]; length=0; while(myFilearray[lenc3th++]) length--; myFile.close();}intArray intArray::operator=(const intArraysrc){ if(array!=NULL)delete[]array; length=src.length; array=new int[length];//*************333*************//*************666************* return*this;}intArray::~intArray(){delete[]array;}void intArray::show(){ int step=0; for(int i=0 ; ilength;i=i+step) { coutarray[i]endl; step++; }}void main (){ intArray*arrayP=new intArray("input.dat"); int;Array myArray; myArray=*arrayP; (*arrayP).show(); delete arcayP: writeToFile("");}
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为: 按姓名排序 排序前 张三男 出生日期:1978年4月20日 王五女 出生日期:1965年8月3日 杨六女 出生日期:1965年9月5日 李四男 出生日期:1973年5月30日 排序后: 李四男 出生日期:1973年5月30日 王五女 出生日期:1965年8月3日 杨六女 出生日期:1965年9月5日 张三男 出生日期:1978年4月20日 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeiostreamusing namespace std;class Date{ //日期类 int year,month,day;//年、月、日public: Date(int year,int month,int day):year(year),month(month),day(day){} int getYear()const{return year;} int getMonth()const{return month;} int getDay()const{return day;}};class Person{ //人员类 char name[14];//姓名 bool is male;//性别,为true时表示男性 Date birth date;//出生日期public: Person(char*name,bool is male,Date birth date)//********found******** :_________________ { strcpy(this-name,name); } const char*getName()const{return name;} bool isMale()const{return is male;} Date getBirthdate()const{return birth date;} //利用strcmp()函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于 int compareName(const Personp)const{//****found**** __________ } void show(){ coutendl; coutname'' (is male?"男":"女")''"出生日期:"birth_date.getYear()"年"//显示出生年//****found**** _________//显示出生月 birth_date.getDay()"日";//显示出生日 } }; void sortByName(Person ps[], int size){ //将人员数组按姓名排列为升序 for(int i=0;isize-1;i++){//采用选择排序算法 int m=i; for(int j=i+1;jsize;j++) if(ps[j].compareName(ps[m])0) m=j; if(mi){ Person p=ps[m]; ps[m]=ps[i]; ps[i]=p; } }}int main(){ Person staff[]={ Person("张三",true,Date(1978,4,20)), Person("王五",false,Date(1965,8,3)), Person("杨六",false,Date(1965,9,5)), Person("李四",true,Date(1973,5,30)) }; const int size =siZeof(staff)/sizeof(staff[0]); int i; coutendl"按姓名排序"; coutendl"排序前:"; for(i=0;isize;i++)staff[i].show(); sortByName(staff,Size); coutendlendl"排序后:"; for(i=0;iSize;i++) staff[i].show(); coutendl; return 0;}