请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含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 31 6 19 4 2 3 4 4 5 5 6 19 27 28 31 66 75 要求: 补充编制的内容写在“//*********333*********”与“//*********666*********”之间。不得修改程序的其他部分。 注意:相关文件包括:main.cpp、Integers.h。 程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//Integers.h#ifndef INTEGERS#define INTEGERS#includeiostreamusing namespace std;const int MAXELEMENTS=100;//集合最多可拥有的元素个数class Integers{ int elem[MAXELEMENTS];//用于存放集合元素的数组 int counter;//用于记录集合中元素个数的计数器public: Integers():counter(O){)//创建一个空集合 Integers(int data[],intSize);//利用数组提供的数据创建一个整数集合 void add(int element);//添加一个元素到集合中 void remove(int element);//删除集合中指定的元素 int getCount()const{return counter;)//返回集合中元素的个数 int getElement(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 (intelement){ i f(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()const{ for(int i=0;igetCount();i++) coutseLw(4)getElement;(i); coutendl;}int main(){ int d[]={5,2 8,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;}
请打开考生文件夹下的解决方案文件proj2,其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为: (1,2,3,4,5) (0,0,0,0,0,0) 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeiostreamusing namespace std;class VectorBase{//向量基类,一个抽象类 int len;public: VectorBase(int len):len(len){} int length()const{returnlen;}//向量长度,即向量中元素的个数 virtual double getElement(int i)const=0;//取第i个元素的值 virtual double sum()const=0;//求所有元素的和 void show()const{//显示向量中所有元素 Cout"("; for(int i=0;ilength()-1;i++) coutgetElement(i)",", //*******found******* cout___________")"endl;//显示最后一个元素 }};class Vector:public VectorBase{//向量类 double * val;public: Vector(int len,double v[]=NULL):VectorBase(len){ val=new double[len]; for(int i=0;ilen;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;ilength();i++)_____________; return s; }};class ZeroVector:public VectorBase{//零向量类 public: ZeroVector(int len):Vector-Base(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 ;}
请打开考生文件夹下的解决方案文件proj1,其中在编辑窗口内显示的主程序文件中定义有类AAA和主函数main。程序文本中位于每行“//ERROR ****found****”下面的一行有错误,请加以改正。改正后程序的输出结果应该是: sum=60 注意:只修改每个“//ERROR ****found****”下面的一行,不要改动程序中的其他任何内容。#includeiostreamusing namespace std;class AAA { int a[10];int n;//ERROR ********found********private: AAA(int aa[], int nn): n(nn){//ERROR ********found******** for(int i=0;in; i++)aa[i]=a[i]; } int Geta(int i) {return a[i];}};int main(){ int a[6]={2,5,8,10,15,20); AAA x(a,6); int sum=0 ;//ERROR ********found******** for(int i=0;i6;i++) sum+=x.a[i]; cout"sum=”sumendl ; return 0;}
请打开考生文件夹下的解决方案文件proj2,此工程中包含一个头文件shape.h,其中包含了类Shape、Point和Triangle的声明;包含程序文件shape.cpp,其中包含了类Triangle的成员函数和其他函数的定义;还包含程序文件proj2.cpp,其中包含测试类Shape、Point和Triangle的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为:此图形是一个抽象图形,周长=0,面积=0此图形是一个三角形,周长=6.82843,面积=2注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//shape.hclass Shape{public:virtual double perimeter( )const{return0;}//返回形状的周长virtual doublearea( )const{return0;}//返回形状的面积virtual const char*name( )const{return"抽象图形";}//返回形状的名称};class Point{//表示平面坐标系中的点的类double x;double y;public://*******found*******Point(double x0,doub2e y0):_______{}//用x0、y0初始化数据成员x、ydouble getX( )const{return x;}double getY( )const{return y;}};class Triangle:public Shape{//*******found*******_______;//定义3个私有数据成员public:Triangle(Point p1,Point p2,Point p3):point1(p1),point2(p2),point:3(p3){}double perimeter( )const;double area( )const;const char*narne( )const{return"三角形";}};//shape.cpp#include"shape.h"{}includedouble length(Point p1,Pointp2){return sqrt((p1.getX( )-p2.getX( ))*(p1.getX( )-p2.getX( ))+(p1.getY( )-p2.getY( ))*(p1.getY( )-p2.getY( )));}double Triangle::perimeter( )const{//一个return语句,它利用length函数计算并返回三角形的周长//*******found*******_______;}double Triangle::area( )const{double s=perimeter( )/2.0;return sqrt(s*(s-length(point1,point2))*(s-length(point2,point3))*(s-length(point3,pointl)));}//proj2.cpp#include"shape.h"#includeusing namespace std;//*******found*******_______//show函数的函数头(函数体以前的部分){cout}int msin( ){Shape s;Triangle tri(Point(0,2),Point(2,0),Point(0,0));show(s);Show(tri);return0;}
请打开考生文件夹下的解决方案文件proj1,该工程中包含程序文件main.cpp,其中有关TVSet(“电视机”)和主函数main的定义。程序中位于每个“//ERROR ***********found***********”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: 规格:29英寸,电源:开,频道:5,音量:18 规格:29英寸,电源:关,频道:-1,音量:-1 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。#includeiostreamusing 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{returnon;} //返回电源开关状态 //返回当前音量,关机情况下返回-1 int getVolume()const{returnison()?volume:-1;} //返回当前频道,关机情况下返回-1 int getChannel()const{ return isOn()?channel:-1;}//ERROR *********found*********void turnOnOff()const //将电源在“开”和“关”之间转换 {on=!on;} void setChannelTo(int chan){ //设置频道(关机情况下无效) if(isOn()chan=0chan=99) channel=chan; } void setVolumeTo(int vol){ //设置音量(关机情况下无效) if(isOn()vol=0vol=30) volume=vol; } void show State(){//ERROR *********found********* cout"规格:"getsize()"英寸" ",电源:"(isOn()?"开":"关") ",频道:"getChannel() ",音量:"getVolume()endl; } }; int main(){ TVSet tv(2 9); tv.turnOnOff(); tv.setChannelTo(5); tv.setVolumeTo(18); tv.show State(); tv.turnOnOff(); tv.show State(); return 0;}
使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数sum(int n)计算在n范围内,能被7和11整除的所有整数的和(包括n在内)。 注意:不能修改程序的其他部分,只能补充sum()函数。1 #include2 double sum(int n)3 {45 }6 void main()7 {8 cout9 cout10 cout11 return;12 }
请打开考生文件夹下的解决方案文件proj3,其中包含了类Polynomial(“多项式”)的定义。形如5x4+3.4x2-7x+2的代数式称为多项式,其中的5为4次项系数,3.4为2次项系数,-7为1次项系数,2为0次项(常数项)系数。此例缺3次项,意味着3次项系数为0,即省略了0x3。在Polynomial中,多项式的各个系数存储在一个名为coef的数组中。例如,对于上面的多项式,保存在coef[0]、coef[1]…coef[4]中的系数依次为:2.0、-7.0、3.4、0.0、5.0,也即对于i次项,其系数就保存在coef[i]中。成员函数getValue计算多项式的值,多项式中x的值是由参数指定的。请补充完成文件Polynomial.cpp中成员函数getValue的定义。此程序的正确输出结果应为:Value of p1whenx=2.0:59.8Value of p2whenx=3.0:226.8注意:只在函数getValue的“//********333********”和“********666********”之间填入 若干语句,不要改动程序中的其他内容。//Polynomiac.h#includeusing namespace std;class Polynomial{//“多项式”类public:Polynomial(double coef[ ],int num):coef(new double[num]),num_of_terms(num){for(int i=0;ithis->coef[i]=coef[i];}~Polynomial( ){delete[ ]coef;}//返回指定次数项的系数double getCoefficient(int power)const{return coef[power];}//返回在x等于指定值时多项式的值double getValue(double x)const;private://系数数组,coef[0]为0次项(常数项)系数,coef[1]为1次项系数,coef[2]为2次项(平方项)系数,余类推。double*coef;int num_of_terms;};void writeToFile(const char*path);//Polymomial.cpp#include"Polynomial.h"double polynomial::getValue(double x)const{//多项式的值value为各次项的累加和double value=coef[0];//********333********//********666********return valme;}//main.cpp#include"Polynomial.h"int main( ){double p1[ ]={5.0,3.4,-4.0,8.0},p2[ ]={0.0,-5.4,0.0,3.0,2.0};Polynomial polyl(p1,sizeof(p1)/sizeof(double)),poly2(p2,sizeof(p2)/sizeof(double));coutcoutwriteToFile(" ");return0;}
请打开考生文件夹下的解决方案文件proj1,此工程中包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Name:sonny Type:dog Name:John Type:dog Name:Danny Type:cat Name:John Type:dog 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。#includeiostreamusing namespace std:enum Pets type{dog,cat,bird,fish);class Pets{private: char*name; Pets_type type;public: Pets(consdt char *name="sonny",Pets type type=dog); Petsoperator=(const Petss); ~Pets(); void show()const;};Pets::PetS(const char *name,Pets_ype type)//构造函数{ this - namQ = new char[strlen(name)+1]; strcpy(this-name,name);//ERROR *********found********* type=type ;}Pets::~Pets()//析构函数,释放Flame所指向的字符串{//ERROR *********found********* name='/0';}Pets Pets::operator=(constPetss){if(s==this)//确保不要向自身赋值 return*this; 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; } coutendl;}int main(){ Pets mypetl,mypet2("John",dog); Pets youpet("Danny",cat); mypet1.show(); mypet2.show(); youpet.show(); youpet=mypet2; youpet.show(); return 0;}
