使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: numbor1=a numher2=a numbcr1=a number2_b 注意:错误的语句在//******error******的下面,修改该语句即可。1 #include2 class CMyClass3 {4 public:5 //******error******6 friend void SetValue(CMyClass obj,char c)7 {8 obj.number1=c;9 obj.number2=c;10 }11 //******error******12 void SetValue(CMyClass obj,char c1,char c2)13 {14 obj.number1=c1;15 obj.number2=c2;16 }17 void display()18 {19 cout20 cout21 }22 private:23 char number1,number2;24 };25 void main()26 {27 CMyClass t;28 SetValue(t,'a');29 t.display();30 //******error******31 t.SetValue(&t,'a','b');32 t.display();33 }
请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 车牌号:冀 ABC1234 品牌:ForLand 类别:卡车 当前档位:0 最大载重量:12 车牌号:冀 ABC1234 品牌:ForLand 类别:卡车 当前档位:2 最大载重量:12 车牌号:沪 XYZ5678 品牌:QQ 类别:小轿车 当前档位:0 座位数:5 车牌号:沪 XYZ5678 品牌:QQ 类别:小轿车 当前档位:一1 座位数:5 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeiostream#includeiomanip#includecmathusing namespace std;class AutoMobile{ //“汽车”类 char*brand; //汽车品牌 char*number; //车牌号 int speed; //档位:1、2、3、4、5,空档:0,倒档:一1public: AutoMobile(conSt char * thebrand,const char * the number):speed(0){ brand=new char[strlen(the brand)+1];//********found********___________;//********found******** ___________; strcpy(number,the number); ~AutoMobile(){delete[]brand;delete[]number;} const char*theBrand()const{return brand;} //返回品牌名称 const char *theNumber()const{return number;} //返回车牌号 int currentSpeed()const{return speed;} //返回当前档位 void changeGearTo(int thespeed){ //换到指定档位 if(speed=-1 speed=5) speed=the speed; } virtual const char*category()const=0; //类别:卡车、小轿车等 virtual void show()const { cout"车牌号:"theNumber()//********found******** "品牌:"____________ "类别:"category() "当前档位:"currentSpeed(); }};class Car:public AutoMobile{ int seats;//座位数public: Car(const char*the brand,const char * the number,int the seats):AutoMobile(thebrand,the number),seats(the seats){} int numberOfSeat()const{return seats;}//返回座位数 const char*category()const{return "小轿车";} //返回汽车类别 void show()const{ AutoMobile::show(); cout "座位数:" number-OfSeat()endl; }};class Truck:public AutoMobile{//"卡车"类 int max load;//最大载重量public: Truck(const char * the brand,const char*the number,int the max load):AutoMobile(the brand,the number),max load(the max load){} int maxLoad()const{return max load;)//返回最大载重量//********found******** const char *category()___________//返回汽车类别 void show()const { AutoMobile::show(); cout"最大载重量:"max.Load()endl; }};int main(){ Truck truck("ForLand","冀ABC1234",12); truck.show(); truck.changeGearTo(2); truck.show(); Car car("QQ","沪XYZ5678",5); car.show(); car.changeGearTo(-1); car.show(); coutendl: return 0 ;}
一个向量(即一批地址连续的存储单元)第一个元素的存储地址是100,每个元素的长度为2,则第5个元素的地址是【 】。
请打开考生文件夹下的解决方案文件projd2,此工程中声明的Array是一个表示数组的类。一个Array对象可以包含多个整型元素。Array的成员说明如下: 成员函数add用于向数组的末尾添加一个元素; 成员函数get用于获取数组中指定位置的元素; 数据成员a表示实际用于存储数据的整型数组; 数据成员size表示数组的容量,数组中的元素个数最多不能超过size; 数据成员Hum表示当前数组中的元素个数。 SortedArray是Array的派生类,表示有序数组。SortedArray重新定义了Array中的add函数,以确保有序数组中的元素始终按照升序排列。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输m结果应为: 10,9,8,7,6,5,4,3,2,1, 1,2,3,4,5,6,7,8,9,10, 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。1 #includeiostream2 using namespace std;34 class Array{5 public:6 Array(unsigned int S)7 {8 size=s;9 num=0;10 a=new int[s];11 }1213 virtual ~Array(){delete[]a;)14 virtual void add(int e)15 {16 if(numsize){17 //**********found**********18 ______________19 num++;20 }21 }2223 int get(unsigned int i)const24 {25 if(isize)26 return a[i];27 return 0 ;28 }2930 protected:31 int*a;32 unsigned int size,num;33 };3435 class SortedArray:public Array{36 public:37 //**********found**********38 SortedArray(unsigned int s)39 :___________{}4041 virtual void add(int e)42 {43 if(num=size)44 return;45 int i=0,j;46 while(inum){47 if(ea[i]){48 for(j=num;ji;j--) {49 //**********found**********50 ___________;51 }52 //**********found**********53 ___________;54 break;55 }56 i++;57 }5859 if(i=num)60 a[i]=e;6l num++;62 }63 };6465 void fun(Arraya)66 {67 int i:68 for(i=10;i=1;i--){69 a.add(i)j70 }71 for(i=0;i10;i++){72 couta.get(i)",";73 }74 coutendl;75 }7677 int main()78 {79 Array a(10);80 fun(a);8l SortedArray sa(10);82 fun(sa);83 return 0;84 }
请打开考生文件夹下的解决方案文件proj1,此工程包含一个源程序文件proj1.cpp。文件中将表示数组元素个数的常量Size定义为4,并用int类型对类模板进行了实例化。文件中位于每个注释“//ERROR***found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: 1 2 3 4 注意:模板参数名用T。只修改注释“//ERROR ********found********”的下一行语句,不要改动程序中的其他内容。//proj1.cpp#includeiostreamusing namespace std;//将数组元素个数Size定义为4//ERROR ********found********conSt int size;templatetypename TClass MyClass{public: Myclass(T*P) { for(int i=0 ; isize;i++) array[i]=p[i]; } void Print();private: T array[size];};templatetypename T//ERROR ********found********void Myclass::Print(){ for(int i=0;isize;i++) coutarray[i]'\t';}int main(){ int intArray[Size]={1,2,3,4};//ERROR ********found******** MyClassdoubleobj {intArray); obj.Print(); Coutendl; return 0;}
请打开考生文件夹下的解决方案文件proj1,其中有线段类Line的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: End point 1=(1,8),End point 2=(5,2),length=7.2111。 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。#includeiostream#includecmathusing namespace std; Class Line; double length(Line); class Line { //线段类 double x1,y1 ;//线段端点1 double x2 ,y2;//线段端点2public://ERROR *******found******* Line(double x1,double y1,double x2,double y2)const{ this-xl=xl: this-yl=yl; this-x2=x2; this-y2=y2; } double getXl()const{return x1;} double getYl()const{return y1;} double getX2()const{return x2;} double getY2()const{return y2;} void show()const{ cout"End point 1=("x1","y1; cout"),End point 2=("x2","y2;//ERROR *******found******* cout"),length="length(this)"。"endl; }};double length(Line 1){//ERROR *******found******* return sqrt((1.x1-1.x2)*(1.x1-1.x2)+(1.y1-1.y2)*(1.y1-1.y2));}int main(){ Line r1(1.0,8.0,5.0,2.0); r1.show(); return 0;}
请打开考生文件夹下的解决方案文件proj2,此工程中声明的Array是一个表示数组的类。一个Array对象可以包含多个整型元素。Array的成员说明如下:成员函数add用于向数组的末尾添加一个元素;成员函数get用于获取数组中指定位置的元素;数据成员a表示实际用于存储数据的整型数组;数据成员size表示数组的容量,数组中的元素个数最多不能超过size;数据成员num表示当前数组中的元素个数。SortedArray是A11ray的派生类,表示有序数组。SortedArray重新定义了Array中的add函数,以确保有序数组中的元素始终按照升序排列。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:10,9,8,7,6,5,4,3,2,1,1,2,3,4,5,6,7,8,9,10,注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeusing namespace std;class Array{public:Array(unsigned int s){Size=s;num=0;a=new int[s];}virtual ~Array( ){delete[ ]a;}virtual void add(int e){if(num//**********found**********_______num++;}}int get(unsigned int i)const{if(ireturn a[i];return0;}protected;int*a;unsigned int Size,num;};class SortedArray:public Array{public://**********found**********SortedArray(unsigned int s):_______{}virtual void add(int e){if(num>=size)return;int i=0,j;while(iif(efor(j=num;j>i;j--){//**********found**********_______;}//**********found**********_______;break;}i++;}if(i==num)a[i]=e;num++;}};void fun(Arraya){int i;for(i=10;i>=1;i--){a.add(i);}for(i=0;icout}cout}int main( ){Array a(10);fun(a);SortedArray sa(10);fun(sa);return0;}
请打开考生文件夹下的解决方案文件proj3,此工程包含一个源程序文件proj3.epp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象myArray中,然后对整数序列按非递减排序,最后由函数writeToFile选择序列中的部分数据输出到文件out.dat中。文件in.dat中的整数个数不大于300个。 要求: 补充编制的内容写在“//************333************”与“//************666************”两行之间。实现对整数序列按非递减排序,并将排序结果在屏幕上输出。不得修改程序的其他部分。 注意:程序最后已将结果输出到文件out.dat中。输出函数writeToFile已经给出并且调用。 1 //proj3.cpp 2 #includelostream 3 #includefstream 4 #includecstring 5 using namespace std; 6 7 class intArray 8 { 9 private: 10 int * array;//整数序列首地址 11 int length;//序列中的整数个数 12 public: 13 //构造函数,从文件中读取数据用于初始化新对象。参数是文件名 14 intArray(char*ilename); 15 void sort();//对整数序列按非递减排序 16 ~intArray(); 17 void writeToFi le(char * filename); 18 }; 19 20 intArray::intArray(char * filename) 2l { 22 ifstream myFile(filename); 23 int len=300; 24 array=new int[len]; 25 length=0; 26 while(myFilearray[length++]); 27 length--; 28 myFile.close(); 29 } 30 31 void intArray::sort(){ 32 //*************** 333*************** 33 34 //***************666*************** 35 } 36 intArray::~intArray() 37 { 38 delete[]array; 39 } 40 41 void intArray::writeToFile 42 (char * filename) 43 { 44 int step=0; 45 ofstream outFile(filename); 46 for(int i=0;ilength;i=i+step) 47 { 48 outFilearray[i]endl; 49 step++; 50 } 5l outFile.close(); 52 } 53 54 void main() 55 { 56 intArray myArray("in.dat"); 57 myArray.sort(); 58 myArray.writeToFile("out.dat"); 59 }
