niusouti.com

请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果

题目

请使用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 56 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; //用于记录集合中元素个数的计数器 puhlic: IntegerSet:counter(0){} //创建一个空集合 IntegerSet(int data[],int size); //利用数组提供的数据创建一个整数集合 void add(int element); //添加一个元素到集合中 void remeve(int element); //删除集合中指定的元素 int getCountconst{return counter;} //返回集合中元素的个数 int getElement(int i)const{retum elem[i];}//返回集合中指定的元素 void showconst; }; void WriteToFile(char*); endif //main.cpp include”IntegerSet.h” include<inmanip> 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++; //计数器加l } void IntegerSet::remove(int element){ //***************333*************** //***************666*************** void IntegerSet::showconst{ 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; }


相似考题

2.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehiele类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将Vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:801501001注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。include<iostream.h>class vehicle{private:int MaxSpeed;int Weight;public://*************found************vehicle(int maxspeed,int weight):——~vehicle{};int getMaxSpeed{return MaxSpeed;}int getWeight{retum Weight;}};//****************found************class bicycle:——public vehicle{private:int Height;public:bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}int getHeight{retum Height;};};//*******************found**************class motorcar:——public vehicle{private:int SeatNum;public:motorcar(int maxspeed。int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}int getSeatNum{return SeatNum;};};//*****************found***************class motorcycle:——{public:motorcycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}};void main{motorcycle a(80,150,100);cout<<a.getMaxSpeed<<endl;cout<<a.getWeight<<endl;cout<<a.getHeight<<endl;cout<<a.getSeatNum<<endl;}

3.请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的CDeepCopy是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数0perator=,以实现深层复制。 要求: 补充编制的内容写在“//************333************”与“//****************666*************”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 //CDeepCopy.h include <iostream> include<string> using namespace std; class CDeepCopy { public: int n;//动态数组的元素个数 int *p;//动态数组首地址 CDeepCopy(int); ~CDeepCopy; CDeepCopy&operator=(const CDeepCopy&r);//赋值运算符函数 }; void writeToFile(char}); //main.cpp include”CDeepCopy.h” CDeepCopy::一CDeepCopy{delete[]P;} CDeepCopy::CDeepCopy(int k){n=k;p=new int[n];}//构造函数实现 CDeepCopy&CDeepCopy::0perator=(const CDeepCopy&r)//赋值运算符函数实现 { //***********333********* //***********666*********** } int main { CDeepCopy a(2),d(3); a.p[O]=1;d.p[O]=666;//对象a,d数组元素的赋值 { CDeepCopy b(3);//调用赋值运算符函数 a.p[O]=88;b=a; cout<<b.p[O];//显示内层局部对象的数组元素 } cout<<d.p[O];//显示d数组元素a.p[O]的值 cout<<”d fade away;\n”; cout<<a.p[O];//显示a数组元素a.p[O]的值 writeToFile(””); return 0; }

更多“请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类IntegerSet和主函数ma ”相关问题
  • 第1题:

    清使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中包含了类Pets(“宠物”) 和主函数main的定义。程序中位于每个“//ERROR****found ****”之后的一行语句有错误,请加以改 正。改正后程序的输出结果应为: Name:sonny Type:dog Name:John Type:dog Name:Danny Typc:cat Name:John Type:dog 注意:只修改每个“//ERROR ****found ****”下的那一行,不要改动程序中的其他内容。 include<iostream> using namespace sm; enum Pets_type{d09,cat,bird,fish}; class Pets{ private: char *name; Pets_type type; public: Pets(const char *name=”sonny”,Pets_type type=dog); Pets&operator=(const Pets&s); ~Pets; void showeonst;}; Pets::Pets(eonst char$naIne,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&Pets::0perator=(const Pets&s){ if(&s==this)//确保不要向自身赋值 return *this; delete[]name; name=new char[strlen(S.name)+1];//ERROR *********found********* strcpy(this一>nmne,name); type=S.type: return *this;} void Pets::showconst cout<<“Name:”<<name<<”Type:”: Pets mypetl,mypet2(’’John”,dog);


    正确答案:
    (1)this一>type=type;
    (2)delete[]name;
    (3)strepy(this->name,s.name);

  • 第2题:

    请使用【答题】菜单命令或直接用VC6打开考生文件夹下的工程prog3,其中声明了ValArray类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此种情况下程序的输出应该是: ValArray vl={1,2,3,4,5} ValArray v2={1,2,3,4,5} 要求: 补充编制的内容写在“//*********333*********”与“//*********666*********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数write To File已经编译为boj文件,并且在本程序中调用。 //ValArray.h include<iostream> using 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;i<size;i++) v[i]=P[i];


    正确答案:

    size=othe].size;
    v=liew int[size]:
    for(int i=0;i<size;i++)
    setAtTay(i,other.v[ij);

  • 第3题:

    请教:2016年计算机二级C++基础练习三简答题9如何解答?

    使用VC6打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码,补充空出的代码。函数sum(intn)返回1,2,3,…,n的和。其中n大于0。

    程序要求使用递归实现上述功能。

    注意:不能修改程序的其他部分,只能补充sum函数。

    试题程序:

    #include

    #include

    intsum(intn)

    {

    }

    voidmain()

    {

    cout<<"1+2+3+…+100="<

    endl;

    return;

    }


    【答案】

      return(n==1)?1:n+sum(n-1);

      【解析】本题主要考查三目运算符?:的使用和递归函数的编制。程序功能是计算前n个自然数的和,n为参数。程序的运算过程如下,不是一般性,假设n为3,首先执行sum(3),因为3不等于1,所以return语句返回的值为3+sum(3-1),然后执行sum(3-1)即sum(2),2不等于1,所以return语句返回的值为2+sum(2-1),然后执行sum(2-1)即sum(1),因为1等于1,所以return语句返回的值为1,即问号后的值,所以最终结果为3+2+1=6,而题中n为100,所以结果为5050。

  • 第4题:

    请使用VC6或使用【答题】菜单打开考生文件夹progl下的工程progl,该工程中包含程序文件main. cpp,其中有Salary(“工资”)类和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句行有错误,请加以改正。改正后程序的输出结果应为: 应发合计:3500应扣合计:67.5实发工资:3432.5 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。 include<iostream> using namespace std; class Salary{ public: Salary(const char *id,double the_base,double the bonus,double the_tax) //ERROR **********found********** :the_base(base),the_bonus(bonus),the_tax(tax) { staff_id=new char[strlen(id)+1]; strcpy(staff_id,id); } //ERROR **********found********** ~Salary{delete * staff_id;} double getGmssPayconst{retum base+bonus;}//返回应发项合计 double getNetPayconst}retum getGmssPay-tax;}//返回实发工资额 private: char * staff id;//职工号 double base;//基本工资 double bonus;//奖金 double tax;//代扣个人所得税 }; int main{ Salary pay(”888888”,3000.0,500.0,67.50); cout<<”应发合计:”<<pay.getGrossPay<<" "; cout<<”应扣合计:”<<pay.getGrossPay一pay.getNetPay<<””; //ERROR**********found********** cout<<”实发工资:”<<pay::getNetPay<<endl; return 0; }


    正确答案:

    (1)base(the—base),bonus(the—bonus),tax(the—tax)
    (2)Salary{delete[]staff_id;}
    (3)cout<<”实发工资:”<<pay.getNetPay<<end1;

  • 第5题:

    请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件 main.cpp,其中有类CPolygon(“多边形”)、CRectangle(“矩形”)、CTriangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为: 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 include<lostream> {tout<<——<<endl;} class CRectangle:public CPolygon{ CRe&angle(int w,int h):width(w),height(h){} int area(void){return(width *height);} class CTriangle:public CPolygon{ int length;//三角形一边长 int height;//该边上的高 public: CTriangle(int l,int h):length(1),height(h){} //*********found********* int area(void){return(——)/2;} }; int main{ CRectangle rect(4,5); CTriangle trgl(4,5); //*********found********* ______ *ppolyl,* ppoly2; ppolyl=&rect; ppoly2=&trgl; ppolyl->printarea; ppoly2->printarea; retun 0;


    正确答案:

    (1)virtual int area(void)=0;
    (2)area
    (3)length*height
    (4)CPolygon