2015年計(jì)算機(jī)二級《C++》上機(jī)考前沖刺練習(xí)題

字號:

二、基本操作題(18分)
    41、請使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl,此工程中含有一個(gè)源程礙文件 projl.epp。其中位于每個(gè)注釋“//ERROR ****found****”之后的一行語句存在錯(cuò)誤。請改正這些錯(cuò)誤,使程序的輸出結(jié)果為:
    ConstruCtor Called. The value is 10
    Copy ConstruCtor Called. The value is 10
    DestruCtor Called. DestruCtor Called. 注意:只修改注釋“//ERROR ****found****”的下一行語句,不要改動(dòng)程序中的其他內(nèi)容。
    //pwjl.Cpp
    #inClude ’using namespaCe std; Class MyClass{
    publiC:
    //ERROR**********found**********
    MyClass(int i)
    {value=i;Cout<<”ConstruCtor Called.” < //ERROR**********found********** MyClass(eonst MyClass P)
    {
    value = P.value;
    eout<<”Copy ConstruCtor Called.”< }
    void Print()
    {Cout<<”The value is” < //ERROR**********found********* void-MyClass()
    {Cout<<”DestruCtor Called.”< private:
    int value;
    }; int main()
    { MyChas objl
    owl.Print();
    MyClmss obj2(owl); obj2.Print();
    retum 0;
    三、簡單應(yīng)用題(24分)
    42、請使用VC6或使用【答題】菜單打開考生文件夾pr092下的工程pros2。此工程中包含一個(gè)程序文件main.cpp,其中有“部門”類Department和“職工”類Staff的定義,還有主函數(shù)main的定義。在主函數(shù)中定義了兩個(gè)“職工”對象,他們屬于同一部門。程序展示,當(dāng)該部門改換辦公室后,這兩個(gè)人的辦公室也同時(shí)得到改變。請?jiān)诔绦蛑械臋M線處填寫適當(dāng)?shù)拇a并刪除橫線,以實(shí)現(xiàn)上述類定義。此程序的正確輸出結(jié)果應(yīng)為:
    改換辦公室前:
    職工號:0789姓名:張三部門:人事處辦公室:521
    職工號:0513姓名:李四部門:人事處辦公室:521
    改換辦公室后:
    職工號:0789姓名:張三部門:人事處辦公室:311
    職工號:0513姓名:李四部門:人事處辦公室:311
    注意:只在橫線處填寫適當(dāng)?shù)拇a,不要改動(dòng)程序中的其他內(nèi)容,也不要?jiǎng)h除或移動(dòng)“//****found****”。
    #include
    using namespace std;
    class Department{ //“部門”類
    public:
    Department(const char*name,const char*office){
    strcpy(this一>name,nanle);
    //**********found**********
    }
    const char*getName()const{return name;}//返回部門名稱
    //**********found**********
    const char*getOffice()const{________} //返回辦公室房號
    void changeOfficeTo(const char*office){ //改換為指定房號的另一個(gè)辦公室
    strcpy(this一>office,office);
    }
    private:
    char name[20];//部門名稱
    char office[20];//部門所在辦公室房號
    };
    class staff{//“職工”類
    public:
    //**********found**********
    Staff(const char*my—id,const char木my_name,Department&my_dept):——{
    strcpy(this一>staff id,my_id);
    strcpy(this一>name,my_name);
    }
    const char*getlD()const{return staff_id;}
    const char*getName()consl{return name;}
    Department getDepartment()const{return dept;} char staff=id[10];//職工號
    char name[20];//姓名
    Department&dept;//所在部門
    }; void showStaff(Staff&staff){
    cout<<”職工號:”< cout<<”姓名:”< cout<<”部門:”< cout<<”辦公室:”< int main(){
    Department dept(”人事處”,”521”);
    Staff Zhang(”0789”,”張三”,dept),Li(”0513”,”李四”,dept);
    cout<<”改換辦公室前:”< showStaff(Zhang); showStaff(Li);
    //人事處辦公室由521搬到311 //**********found********** ———————————————————————一
    cout<<”改換辦公室后:”< showStaff(Zhang); showStaff(Li);
    return 0; }
    43、
    請使用VC6或使用【答題】菜單打開考生文件夾proj3下的工程proj3,其中包含了類IntegerSet和主函數(shù)main的定義。一個(gè)IntegerSet對象就是一個(gè)整數(shù)的集合,其中包含0個(gè)或多個(gè)無重復(fù)的整數(shù);為了便于進(jìn)行集合操作,這些整數(shù)按升序存放在成員數(shù)組elem的前若干單元中。成員函數(shù)add的作用是將一個(gè)元素添加到集合中(如果集合中不存在該元素),成員函數(shù)remove從集合中刪除指定的元素(如果集合中存在該元素)。請編寫成員函數(shù)remove。在main函數(shù)中給出了一組測試數(shù)據(jù),此時(shí)程序的正確輸出結(jié)果應(yīng)為:
    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
    要求:
    補(bǔ)充編制的內(nèi)容寫在“//***********333***********”與“//***********666***********”之間,不得修改程序的其他部分。
    注意:程序最后將結(jié)果輸出到文件out.dat中。輸出函數(shù)writeToFile已經(jīng)編譯為obj文件,并且在本程序中調(diào)用。
    //IntegorSet.h
    #ifndef INTEGERSET
    #define INTEGERSET
    #include
    using namespace std;
    const int MAXELEMENTS=100;
    //集合最多可擁有的元素個(gè)數(shù)
    class IntegerSet{
    int elem[MAXELEMENTS];
    //用于存放集合元素的數(shù)組
    int counter; //用于記錄集合中元素個(gè)數(shù)的計(jì)數(shù)器
    puhlic:
    IntegerSet():counter(0){}
    //創(chuàng)建一個(gè)空集合
    IntegerSet(int data[],int size);
    //利用數(shù)組提供的數(shù)據(jù)創(chuàng)建一個(gè)整數(shù)集合
    void add(int element);
    //添加一個(gè)元素到集合中
    void remeve(int element);
    //刪除集合中指定的元素
    int getCount()const{return counter;}
    //返回集合中元素的個(gè)數(shù)
    int getElement(int i)const{retum elem[i];}//返回集合中指定的元素
    void show()const;
    };
    void WriteToFile(char*);
    #endif
    //main.cpp
    #include”IntegerSet.h”
    #include
    IntegerSet::IntegerSet(int data[],int size):counter(0){
    for(int i=0;i add(data[i]);
    }
    }
    void IntegerSet::add(int element){
    int j;
    //從后往前尋找第一個(gè)小于等于element的元素
    for(j=counter;j>0;j-)
    if(element>=elem[j一1])break;
    //如果找到的是等于element的元素,說明要添加的元素已經(jīng)存在,直接返回
    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++; //計(jì)數(shù)器加l
    }
    void IntegerSet::remove(int element){
    //***************333***************
    //***************666***************
    void IntegerSet::show()const{
    for(int i=0;i cout< cout< }
    int main(){
    int d[]={5,28,2,4,5,3,2,75,27,66,31};
    IntegerSet S(d,11);S.show();
    S.a(chǎn)dd(6); s.show();
    S.a(chǎn)dd(19); S.show();
    S.remove(2); s.show();
    S.a(chǎn)dd(4); S.show();
    writeToFile(””);
    return 0;
    }