計(jì)算機(jī)等級考試二級C++上機(jī)模擬試題及答案

    時(shí)間:2024-07-14 17:18:09 計(jì)算機(jī)等級 我要投稿
    • 相關(guān)推薦

    2016計(jì)算機(jī)等級考試二級C++上機(jī)模擬試題及答案

      一、改錯(cuò)題

    2016計(jì)算機(jī)等級考試二級C++上機(jī)模擬試題及答案

      使用VC6打開考生文件夾下的工程kt6_1,此工程包含一個(gè)源程序文件kt6_1.cpp,但該程序運(yùn)行有問題,請改正程序中的錯(cuò)誤,使程序的輸出結(jié)果如下:

      Constructor2

      Constructor1

      i=0

      i=10

      Destructor

      源程序文件kt6_1.cpp清單如下:

      #include

      using namespace std;

      class CSample

      {

      int i;

      public:

      CSample(){cout<<"Constructor1"<

      CSample(int val){cout<<"Constructor2"<

      ~CSample(){cout<<"Destructor"<

      void disp();

      };

      /**********found**********/

      void disp()

      { cout<<"i="<

      void main()

      {

      CSample *a,b(10);

      /**********found**********/

      a->disp();

      /**********found**********/

      b->disp();

      }

      【參考答案】

      (1)將void disp()

      改為:void CSample::disp()

      (2)將a->disp();

      改為:a=new CSample; a->disp();

      (3)將b->disp();

      改為:b.disp();

      【試題解析】

      (1)主要考查類成員函數(shù)定義格式的熟練掌握,對于類體外函數(shù)的實(shí)現(xiàn),應(yīng)該使用作用域符"::",按照返回值類型類名::函數(shù)名(參數(shù)列表)的形式進(jìn)行說明;

      (2)主要考查對動(dòng)態(tài)存儲分配的掌握,根據(jù)前面的定義,a是一個(gè)指針類型的變量,指向一個(gè)對象,但是并沒有被初始化,此時(shí)a中的數(shù)據(jù)無任何意義,應(yīng)該使用動(dòng)態(tài)存儲分配new生成一個(gè)新的對象,并將返回的指針賦值給a;

      (3)主要考查對象指針與對象在調(diào)用成員函數(shù)時(shí)格式的不同,b是一個(gè)對象變量,使用b調(diào)用成員函數(shù)應(yīng)該用"."運(yùn)算符。

      修改后代碼:

      #include

      using namespace std;

      class CSample

      {

      int i;

      public:

      CSample(){cout<<"Constructor1"<

      CSample(int val){cout<<"Constructor2"<

      ~CSample(){cout<<"Destructor"<

      void disp();

      };

      /**********found**********/

      void CSample::disp()//void disp()

      { cout<<"i="<

      void main()

      {

      CSample *a,b(10);

      /**********found**********/

      a=new CSample;a->disp();

      /**********found**********/

      b.disp();

      }

      二、簡單應(yīng)用題

      編寫函數(shù)fun(),它的功能是利用以下所示的簡單迭代方法求方程cos(x)-x=0的一個(gè)實(shí)根。

      xn+1=cos(xn)

      迭代步驟如下:

      (1)取x1初值為0.0。

      (2)x0=x1,把x1的值賦給x0。

      (3)x1=cos(x0),求出一個(gè)新的x1。

      (4)若x0-x1的絕對值小于0.000001,則執(zhí)行步驟(5),否則執(zhí)行步驟(2)。

      (5)所求x1就是方程cos(x)-x=0的一個(gè)實(shí)根,做為函數(shù)值返回。

      程序輸出結(jié)果Root=0.739085。

      注意:部分源程序已存在文件kt6_2.cpp中。

      請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入所編寫的若干語句。

      文件kt6_2的內(nèi)容如下:

      #include

      #include

      using namespace std;

      float fun()

      {

      }

      void main(){

      cout<<"Root="<

      }

      【參考答案】

      float fun()

      {

      float x1=0.0,x0;

      do {

      x0=x1;

      x1=cos(x0);}

      while(fabs(x0-x1)>=1e-6);

      return x1;

      }

      【試題解析】解答本題的關(guān)鍵之處在于看清題中所給的“迭代步驟”,同時(shí)要理解xn+1=cosxn通式的含義,要考慮到x1的初值為0.0。

      fabs(x0-x1)>=0.000001和fabs(x0-x1)>=1e-6的輸出結(jié)果一致,

      但是如果沒有fabs,最后輸出結(jié)果會(huì)直接為Root=1而非Root=0.739085,

      ps:fabs(x)為對x求絕對值。說明:計(jì)算|x|,當(dāng)x不為負(fù)時(shí)返回x,否則返回-x。abs和fabs是一對常用的數(shù)學(xué)函數(shù),abs是整數(shù)取絕對值,而fabs主要用于求浮點(diǎn)數(shù)的絕對值。

      #include

      #include

      using namespace std;

      float fun()

      {

      float x1=0.0,x0;

      do

      {

      x0=x1;

      x1=cos(x0);

      }while(fabs(x0-x1)>=0.000001);

      return x1;

      }

      void main(){

      cout<<"Root="<

      }

      三、綜合應(yīng)用題

      使用VC6打開考生文件夾下的工程kt6_3,此工程包含一個(gè)源程序文件kt6_3.cpp,其中定義了用于表示考生的類Student,請按要求完成下列操作,將程序補(bǔ)充完整。

      (1)定義私有數(shù)據(jù)成員code、english分別用于表示考生的編號、英語成績、它們都是int型的數(shù)據(jù)。

      。請?jiān)谧⑨?ldquo;//**1**”之后添加適當(dāng)?shù)恼Z句。

      (2)完成成員函數(shù)voidStudent::inputinformation()的定義,該函數(shù)用于用戶輸入一個(gè)考生對象的信息,輸入格式如下所示:

      輸入編號:

      英語成績:

      計(jì)算機(jī)成績:

      請?jiān)谧⑨?ldquo;//**2**”之后添加適當(dāng)?shù)恼Z句。

      (3)利用已實(shí)現(xiàn)的類Student的成員函數(shù),完成函數(shù)voidfirstname(Student*A[],intnum)的定義,該函數(shù)根據(jù)考生信息A[],輸出num個(gè)考生中總分最高者的編號及其相應(yīng)的總分,在此不考慮總分相同的情況。請?jiān)谧⑨?ldquo;//**3**”之后添加適當(dāng)?shù)恼Z句。

      注意:除在指定位置添加語句之外,請不要改動(dòng)程序中的其他內(nèi)容。

      源程序文件kt6_3.cpp清單如下:

      #include

      using namespace std;

      class Student

      {

      private:

      //**1**

      int computer;

      int total;

      public:

      void getinformation();

      void computesum();

      int getcode();

      int gettotalscore();

      ~Student();

      };

      void Student::getinformation()

      {

      //**2**

      cout<<"英語成績:";

      cin>>english;

      cout<<"計(jì)算機(jī)成績:";

      cin>>computer;

      }

      void Student::computesum()

      {

      total=english+computer;

      cout<<"編號"<

      }

      int Student::getcode()

      {

      return code;

      }

      int Student::gettotalscore()

      {

      return total;

      }

      void firstname(Student *A[],int num)

      {

      //**3**

      tempsum=(*A[0]).gettotalscore();

      for(int i=1;i

      {

      if(((*A[i]).gettotalscore())>tempsum)

      {

      tempcode=(*A[i]).getcode();

      tempsum=(*A[i]).gettotalscore();

      }

      }

      cout<<"總分最高者--"<

      }

      void main()

      {

      Student*A[3];

      int i,n=3;

      for(i=0;i

      {

      A[i]=new Student;

      A[i]->getinformation();

      }

      for(i=0;i

      {

      A[i]->computesum();

      }

      firstname(A,3);

      }

      【參考答案】

      (1)int code;

      int english;

      (2)cout<<"輸入編號:";

      cin>>code;

      (3)int tempcode,tempsum;

      tempcode=(*A[0]).getcode();

      【試題解析】

      本題是對C++程序設(shè)計(jì)的綜合考查,類的成員及成員函數(shù)的定義與調(diào)用,數(shù)據(jù)的輸入輸出,for循環(huán)語句,if條件判斷語句等多個(gè)知識點(diǎn),其中(3)中為指針數(shù)組的使用,指針數(shù)組是一組指針,每一個(gè)成員都按照指針的操作規(guī)則,但是整個(gè)訪問規(guī)則仍然使用數(shù)組下標(biāo)方式,如A[0]指的是第一個(gè)指針,而* A[0]是取出第一個(gè)指針指向的內(nèi)容。

      #include

      using namespace std;

      class Student

      {

      private:

      //**1**

      int code;

      int english;

      int computer;

      int total;

      public:

      void getinformation();

      void computesum();

      int getcode();

      int gettotalscore();

      ~Student();

      };

      void Student::getinformation()

      {

      //**2**

      cout<<"輸入編號:";

      cin>>code;

      cout<<"英語成績:";

      cin>>english;

      cout<<"計(jì)算機(jī)成績:";

      cin>>computer;

      }

      void Student::computesum()

      {

      total=english+computer;

      cout<<"編號"<

      }

      int Student::getcode()

      {

      return code;

      }

      int Student::gettotalscore()

      {

      return total;

      }

      void firstname(Student *A[],int num)

      {

      //**3**

      int tempsum,tempcode;

      tempcode=(*A[0]).getcode();

      tempsum=(*A[0]).gettotalscore();

      for(int i=1;i

      {

      if(((*A[i]).gettotalscore())>tempsum)

      {

      tempcode=(*A[i]).getcode();

      tempsum=(*A[i]).gettotalscore();

      }

      }

      cout<<"總分最高者--"<

    【計(jì)算機(jī)等級考試二級C++上機(jī)模擬試題及答案】相關(guān)文章:

    計(jì)算機(jī)等級二級C語言上機(jī)模擬試題及答案10-25

    2016最新計(jì)算機(jī)二級C++上機(jī)試題及答案03-03

    計(jì)算機(jī)二級考試C++試題及答案03-27

    2017計(jì)算機(jī)等級考試二級模擬試題03-09

    計(jì)算機(jī)二級考試模擬試題及答案03-13

    2016年計(jì)算機(jī)二級C++模擬試題及答案03-07

    2016年9月計(jì)算機(jī)二級C++上機(jī)考試沖刺試題及答案03-13

    2017年9月計(jì)算機(jī)二級C++考試模擬試題及答案03-05

    計(jì)算機(jī)二級VB上機(jī)試題及答案03-14

    91久久大香伊蕉在人线_国产综合色产在线观看_欧美亚洲人成网站在线观看_亚洲第一无码精品立川理惠

      亚洲制服丝袜精品久久100部 | 日本一道综合久久aⅴ免费 色五月这里只有精品 | 五月婷婷亞洲綜合色色 | 亚洲天天做夜夜做天天欢人人 | 亚洲中文字幕欧美另类 | 亚洲色欧美色2019在线 |