<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
            試題

            計算機二級C++上機模擬試題

            時間:2025-03-14 13:22:01 試題 我要投稿

            2016年計算機二級C++上機模擬試題

              計算機二級C++考試按照新大綱需要學習的內容有:C++語言概述、C++語言數據類型、運算符和表達式、基本控制語句、數組、指針與引用、函數、類和對象繼承、模板等內容。以下為大家整理了關于C++上機模擬考試題,希望能幫助到大家!

            2016年計算機二級C++上機模擬試題

              一、改錯題

              使用VC6 打開考生文件夾下的工程kt12_1 ,此工程包含一個源程序文件kt12_1.cpp ,但該程序運行有問題,請改正函數中的錯誤,使該程序的輸出結果為:

              100

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

              #include

              template

              class pair

              {

              T value1,value2;

              public:

              pair(T first,T second)

              {value1=first;value2=second;}

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

              char getmax();

              };

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

              T pair::getmax()

              {

              T retval;

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

              retval=value1>value2??value1:value2;

              return retval;

              }

              void main()

              {

              pairmyobject(100,75);

              cout<

              }

              【說明】題目里的#include

              如果改為#include

              using namespace std;

              會導致該題目中的pair 與標準庫的pair 重名,而報錯。

              如果要改用標準庫,則該題目pair 的名字需要修改例如改為pair1

              【參考答案】

              (1 )將char getmax (); 改為:T getmax ();

              (2 )缺少模板的聲明,前面需要加上:template

              (3 )將retval = value1>value2?? value1 : value2;

              改為:retval = value1>value2? value1 : value2;

              【試題解析】

              (1 )主要考查對模板使用的理解,該函數屬于模板類定義的一部分,對于返回值類型,應該使用模板類名稱T ,這樣編譯的時候才能被接受;

              (2 )主要考查是模板的使用,前面的模板類已經聲明完成了,在類的外面定義類的成員函數時仍然需要使用模板的聲明,這樣在后面的函數定義體中才能使用模板類;

              (3 )主要考查對“ 表達式1? 表達式2 : 表達式3” 語句的掌握,這個語句是一個復合語句,先計算第一個表達式,如果為真則整個式子值為表達式2 的值,否則為表達式3 的值,題目中錯誤的使用了兩個問號。

              #include

              using namespace std;

              template

              class pair1

              {

              T value1,value2;

              public:

              pair1(T first,T second)

              {value1=first;value2=second;}

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

              T getmax();//char getmax();

              };

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

              template T pair1::getmax()//T pair1::getmax()

              {

              T retval;

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

              retval=value1>value2?value1:value2;//retval=value1>value2??value1:value2;

              return retval;

              }

              void main()

              {

              pair1myobject(100,75);

              cout<

              }

              二、簡單應用題

              請編寫函數fun() ,其功能是將s 所指字符串中除了下標為奇數、同時ASCII 值也為奇數的字符之外,其余的所有字符都刪除。字符串中剩余的字符所形成的一個新的字符串放在t 所指的數組中。

              例如:s 所指字符串中的內容為ABCDEFG12345 ,其中字符A 的ASCII 碼值雖為奇數,但元素所在的下標為偶數,因此必需刪除;字符1 的ASCII 碼值為奇數,所在數組中的下標也為奇數,不刪除,最后t 所指的數組中的內容應是135 。

              請勿修改主函數main 和其他函數中的任何內容,僅在函數fun 的花括號中填寫若干語句。

              文件kt12_2.cpp 的內容如下:

              #include

              #include//#include

              #include

              #include//#include

              using namespace std;

              void fun(char*s,char t[])

              {

              }

              void main()

              {

              char s[100],t[100];

              cout<<"Please enter string S:"<

              gets(s);

              fun(s,t);

              puts(t);

              }

              【參考答案】

              void fun(char *s,char t[ ])

              { int i,j=0,n;

              n=strlen(s);

              for(i=0;i

              if(i%2!=0&&s[i]%2!=0)

              { t[j]=s[i];j++;}

              t[j]='\0'; }

              【試題解析】

              本體的解題思路是要先搞清楚在字符參與數值運算時,用的是其ASCII 碼值來進行計算。其次是判斷某數是奇數的方法,即判斷該數與2 的余數是否為0 。

              【調試過程】

              #include

              #include//#include

              #include

              #include//#include

              using namespace std;

              void fun(char*s,char t[])

              {

              int len,j=0,m;

              len=strlen(s);

              // cout<

              // t[0]=len;// 已經檢查可以這樣賦值

              // t[0]='a';

              // t[3]='c';

              for(int i=1;i

              {

              // 檢測ASCII 是否為奇數,如果是奇數就賦值給t[j]

              if(s[i]%2==1){t[j]=s[i];j++;}//if(((int)s[i])%2==1){t[j]=s[i];j++;}

              // 檢測字符串s 是否結束,如果是就將t[j] 除了有內容以外的后面的都賦值為空NULL

              }

              // cout<

              //cout<

              /* int m;

              m=s[0]%2;

              cout<

              */

              /* int ascii;

              char a = 'c';

              ascii = (int)a;

              cout<

              */

              /* int ascii;

              char a = s[0];

              ascii = (int)a;

              cout<

              */

              m=j;

              for(int k=m;k<100;k++)// 之前k=len 先只嘗試將比s 長的后面都賦值為空NULL

              {

              t[j]=NULL;

              j++;

              }

              }

              void main()

              {

              char s[100],t[100];

              cout<<"Please enter string S:"<

              gets(s);

              fun(s,t);

              puts(t);

              }

              【優化答案】

              三、綜合應用題

              使用VC6 打開考生文件夾下的工程kt12_3 。此工程包含一個kt12_3.cpp ,其中定義了類ARRAY ,但類的定義并不完整。請按要求完成下列操作,將程序補充完整。

              (1 )完成類ARRAY 的帶一個參數的構造函數,參數i 為int 型,如果i 不是正數則輸出錯誤信息并退出,否則申請int 型的大小為i 的空間,然后把i 賦值給類的數據成員num 。請在注釋“//**1** ”之后添加適當的語句。

              (2 )完成類ARRAY 的拷貝初始化構造函數,注意解決重復刪除的問題,請在注釋“//**2** ”之后添加適當的語句。

              (3 )完成類ARRAY 的重載的運算符函數[] ,參數i 為int 型,如果i 超界則輸出錯誤信息并退出,否則把下標為i 的元素返回,請在注釋“//**3** ”之后添加適當的語句。

              (4 )完成類ARRAY 的重載的運算符函數= ,同樣需要注意解決重復刪除的問題,不能只是簡單的賦值,請在注釋“//**4** ”之后添加適當的語句。

              注意:除在指定位置添加語句之外,請不要改動程序中的其他內容。

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

              #include

              #include

              class ARRAY

              {

              private:

              int *p,num;

              public:

              ARRAY(){p=new int[10],num=10;}

              ARRAY(int i)

              { //**1**

              { cout<<" 錯誤! 數組長度應為正。\n";

              exit(0);

              }

              p=new int[i];

              num=i;

              }

              ARRAY(const ARRAY &a);

              int &operator[](int i);

              ~ARRAY(){delete p;}

              ARRAY &operator=(const ARRAY &a);

              friend ARRAY operator+(ARRAY &a,ARRAY &b);

              friend ostream& operator<<(ostream &os,ARRAY &a);

              };

              ARRAY::ARRAY(const ARRAY &a)

              {

              //**2**

              for(int i=0;i

              p[i]=a.p[i]; }

              int &ARRAY::operator[](int i)

              { //**3**

              {

              cout<<" 越界訪問!";

              exit(0);

              }

              return p[i];

              }

              ARRAY &ARRAY::operator=(const ARRAY &a)

              {

              num=a.num;

              p=new int[num];

              for(int i=0;i

              p[i]=a.p[i];

              //**4**

              }

              ARRAY operator+(ARRAY &a,ARRAY &b)

              {

              if(a.num!=b.num)

              {

              cout<<" 數組長度不相同!"<

              exit(0);

              }

              ARRAY t(a.num);

              for(int i=0;i

              t.p[i]=a.p[i]+b.p[i];

              return t;

              }

              ostream &operator<<(ostream &os,ARRAY &a)

              {

              int i=0;

              for(;i

              {

              cout<

              if(!((i+1)%10))cout<

              return os; }

              void main()

              { ARRAY a(3);

              a[0]=a[1]=a[2]=3;

              cout<<'a'<

              ARRAY b(a);

              cout<<'b'<

              ARRAY c(2);

              c=a+b+b;

              cout<<'c'<

              c=((b=(a+b))+c);

              cout<<'a'<

              a[7]=3;

              cout<

              }

              【參考答案】

              (1 )if(i<=0)

              (2 )num=a.num;

              p=new int[num];

              (3 )if(i>=num||i<0)

              (4 )return *this;

              【試題解析】

              主要考查對一個特殊的類-- 安全 數組的掌握,其中涉及了友元函數、重載函數等,其中(2 )中必需申請新的空間,這樣可以使得兩個對象分別占用不同的兩個空間,在自動調用析構函數時不會遇到重復刪除的問題,這種方法要掌握。

              這里頭文件如果改為:#include

              #include 會報錯。fatal error C1001: INTERNAL COMPILER ERROR

              (compiler file 'msc1.cpp', line 1786)

              error C2433 :'ostream' : 'friend' not permitted on data declarations

              #include

              #include

              class ARRAY

              {

              private:

              int *p,num;

              public:

              ARRAY(){p=new int[10],num=10;}

              ARRAY(int i)

              { //**1**

              if(i<=0)

              { cout<<" 錯誤! 數組長度應為正。\n";

              exit(0);

              }

              p=new int[i];

              num=i;

              }

              ARRAY(const ARRAY &a);

              int &operator[](int i);

              ~ARRAY(){delete p;}

              ARRAY &operator=(const ARRAY &a);

              friend ARRAY operator+(ARRAY &a,ARRAY &b);

              friend ostream& operator<<(ostream& os,ARRAY& a);

              };

              ARRAY::ARRAY(const ARRAY &a)

              {

              //**2**

              num=a.num;

              p=new int[num];

              for(int i=0;i

              p[i]=a.p[i]; }

              int &ARRAY::operator[](int i)

              { //**3**

              if (i>=10||i<0)

              {

              cout<<" 越界訪問!";

              exit(0);

              }

              return p[i];

              }

              ARRAY &ARRAY::operator=(const ARRAY &a)

              {

              num=a.num;

              p=new int[num];

              for(int i=0;i

              p[i]=a.p[i];

              //**4**

              return *this;

              }

              ARRAY operator+(ARRAY &a,ARRAY &b)

              {

              if(a.num!=b.num)

              {

              cout<<" 數組長度不相同!"<

              exit(0);

              }

              ARRAY t(a.num);

              for(int i=0;i

              t.p[i]=a.p[i]+b.p[i];

              return t;

              }

              ostream &operator<<(ostream &os,ARRAY &a)

              {

              int i=0;

              for(;i

              {

              cout<

              if(!((i+1)%10))cout<

              return os; }

              void main()

              { ARRAY a(3);

              a[0]=a[1]=a[2]=3;

              cout<<'a'<

              ARRAY b(a);

              cout<<'b'<

              ARRAY c(2);

              c=a+b+b;

              cout<<'c'<

              c=((b=(a+b))+c);

              cout<<'a'<

              a[7]=3;

              cout<

              }

            【計算機二級C++上機模擬試題】相關文章:

            2016計算機二級C++上機模擬試題及答案07-16

            計算機二級《C++》上機試題及答案08-12

            2016計算機二級C++上機試題及答案08-02

            2016年計算機二級C++上機考試模擬試題及答案05-22

            計算機二級C++上機考試試題06-16

            計算機二級《C++》上機考前沖刺試題06-25

            全國計算機二級《C++》上機試題及答案08-15

            計算機二級C++模擬試題及答案01-23

            2015計算機二級Access上機模擬試題04-17

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院