2016年計算機二級《C++》考前押密模擬應用題

字號:

三、簡單應用題
    42請使用VC6或使用【答題】菜單打開考生文件夾proj2下的工程proj2,該工程中包含一個程序文件main.Cpp,其中有坐標點類point、線段類Line和三角形類Triangle的定義.還有main函數(shù)的定義。程序中兩
    
    Side l:9.43398 Side 2:5
    Side 3:8 area:20 注意:只在橫線處填寫適當?shù)拇a,不要改動程序中的其他內(nèi)容,也不要刪除或移動“//****found****”。
    #inClude #nClude using namespaCe std;
    Class Point{//坐標點類 publiC:
    Const double x,y;
    Point(double x=0.0,double Y=0.0):x(x),Y(Y){} //**********found**********
    double distanCeTo(_____) Const{
    //到指定點的距離
    return sqrt((x-P.X)*(x-P.x)+(Y-P.Y)*(Y-P.Y)); }
    };
    Class Line{//線段類 publiC:
    eonst Point pl,p2;//線段的兩個端點
    //**********found**********
    Line(Point pl,Point p2):——{}
    double length()eonst/retum pl.distanCeTo(p2);}//線段的長度 };
    Ct彝SS Triangle{//三角形類 publiC:
    Const Point pl,p2,p3;//三角形的三個頂點 //**********found**********
    Triangle(_____):pl(p1),p2(p2),p3(p3){} double lengthl()Const{//邊pl,p2的長度
    retum Line(pl,p2).1ength(); }
    double length2()Const{//邊p2,p3的長度 return Line(p2,p3).1ength();
    }
    double length3()Const{//邊p3,pl的長度 returnLine(p3,pl).1ength();
    }
    double area()Const{//三角形面積
    //**********found********** double s=_____;
    return sqrt(s{(S-lengthl())*(s-length2())*(s—length3())); }
    };
    int main(){
    Triangle r(Point(0.0,8.0),Point(5.0,0.0),Point(0.0,0.0)); Cout<<”Side l:”<
    Cout<<”Side 2:”<
    retum 0;
    參考解析:
    (1)eonst Point&P
    (2)pl(p1),p2(p2)
    (3)Point pl,Point p2,Point p3
    (4)(1engthl()+length2()+length3())/2
    四、綜合應用題
    43請使用VC6或使用【答題】菜單打開考生文件夾proj3下的工程proj3,其中聲明的DataList類,是一個用于表示數(shù)據(jù)表的類。DataList的重載運算符函數(shù)0perator+,其功能是求當前數(shù)據(jù)表與另一個相同長度的數(shù)據(jù)表之和;即它返回一個數(shù)據(jù)表,其每個元素等于相應兩個數(shù)據(jù)表對應元素之和。請編寫這個operator+函數(shù)。程序的正確輸出應該是:
    兩個數(shù)據(jù)表: 1,2,3,4,5,6 3,4,5,6,7,8 兩個數(shù)據(jù)表之和:
    4,6,8,10,12,14 要求:
    補充編制的內(nèi)容寫在“//********333********”與“//********666********”之間,不得修改程序
    的其他部分。
    注意:程序最后將結果輸出到文件。ut.dat中。輸出函數(shù)writeToFile已經(jīng)編譯為。bj文件,并且在本程序中調用。
    //DataList.h
    #inClude using namespaCe std;
    ClaSS DataList{//數(shù)據(jù)表類 intfen;
    double*d; publiC:
    DataList(int len,double data[]=NULL); DataList(DataList&data);
    int length()Const{retum len;}
    double getElement(int i)Constt return d[i];}
    DataList operator+(Const DataList&list)Const;//兩個數(shù)據(jù)表求和 void show()Const;//顯示數(shù)據(jù)表
    };
    void writeToFile(Char$,Const DataList&); //main.Cpp
    #inClude”DataList.h”
    DataList::DataList(int len,double data[]):len(ien){ d=new double[1en];
    for(int i=0;i
    d㈨i=(data==NULL?0.0:data[i]);
    }
    DataList::DataList(DataList&data):len(data.1en){ d=new double[1en];
    for(int i=0;i
    DataList DataList::0perator+(Con8t DataList&list,)Const{//兩個數(shù)據(jù)表求和 double*dd=new double[1ist.1ensth()];
    //********333********
    //********666********
    return DataList(1ist.1ength(),dd); }
    void DataList::show()Const{//顯示數(shù)據(jù)表 f()r(int。i=0;i
    eout<
    Cout<
    int main(){
    double sl[]={1,2,3,4,5,6};
    double s2[]={3,4,5,6,7,8};
    DataList list1(6,sl),list2(6,s2);//定義兩個數(shù)據(jù)表對象 eout<<”兩個數(shù)據(jù)表:”<
    listl.show(): list2.show(); eout<
    (1istl+list2).show();
    writeToFile(””,listl+list2); return 0:
    參考解析:
    f0T(int i=0;i
    dd[i]=d[i]+list.d[i];