欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

試題

計(jì)算機(jī)二級C++上機(jī)模擬試題

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

2016年計(jì)算機(jī)二級C++上機(jī)模擬試題

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

2016年計(jì)算機(jī)二級C++上機(jī)模擬試題

  一、改錯題

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

  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;

  會導(dǎo)致該題目中的pair 與標(biāo)準(zhǔn)庫的pair 重名,而報(bào)錯。

  如果要改用標(biāo)準(zhǔn)庫,則該題目pair 的名字需要修改例如改為pair1

  【參考答案】

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

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

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

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

  【試題解析】

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

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

  (3 )主要考查對“ 表達(dá)式1? 表達(dá)式2 : 表達(dá)式3” 語句的掌握,這個語句是一個復(fù)合語句,先計(jì)算第一個表達(dá)式,如果為真則整個式子值為表達(dá)式2 的值,否則為表達(dá)式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<

  }

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

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

  例如:s 所指字符串中的內(nèi)容為ABCDEFG12345 ,其中字符A 的ASCII 碼值雖為奇數(shù),但元素所在的下標(biāo)為偶數(shù),因此必需刪除;字符1 的ASCII 碼值為奇數(shù),所在數(shù)組中的下標(biāo)也為奇數(shù),不刪除,最后t 所指的數(shù)組中的內(nèi)容應(yīng)是135 。

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

  文件kt12_2.cpp 的內(nèi)容如下:

  #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'; }

  【試題解析】

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

  【調(diào)試過程】

  #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;// 已經(jīng)檢查可以這樣賦值

  // t[0]='a';

  // t[3]='c';

  for(int i=1;i

  {

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

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

  // 檢測字符串s 是否結(jié)束,如果是就將t[j] 除了有內(nèi)容以外的后面的都賦值為空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);

  }

  【優(yōu)化答案】

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

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

  (1 )完成類ARRAY 的帶一個參數(shù)的構(gòu)造函數(shù),參數(shù)i 為int 型,如果i 不是正數(shù)則輸出錯誤信息并退出,否則申請int 型的大小為i 的空間,然后把i 賦值給類的數(shù)據(jù)成員num 。請?jiān)谧⑨?ldquo;//**1** ”之后添加適當(dāng)?shù)恼Z句。

  (2 )完成類ARRAY 的拷貝初始化構(gòu)造函數(shù),注意解決重復(fù)刪除的問題,請?jiān)谧⑨?ldquo;//**2** ”之后添加適當(dāng)?shù)恼Z句。

  (3 )完成類ARRAY 的重載的運(yùn)算符函數(shù)[] ,參數(shù)i 為int 型,如果i 超界則輸出錯誤信息并退出,否則把下標(biāo)為i 的元素返回,請?jiān)谧⑨?ldquo;//**3** ”之后添加適當(dāng)?shù)恼Z句。

  (4 )完成類ARRAY 的重載的運(yùn)算符函數(shù)= ,同樣需要注意解決重復(fù)刪除的問題,不能只是簡單的賦值,請?jiān)谧⑨?ldquo;//**4** ”之后添加適當(dāng)?shù)恼Z句。

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

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

  #include

  #include

  class ARRAY

  {

  private:

  int *p,num;

  public:

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

  ARRAY(int i)

  { //**1**

  { cout<<" 錯誤! 數(shù)組長度應(yīng)為正。\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<<" 數(shù)組長度不相同!"<

  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;

  【試題解析】

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

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

  #include 會報(bào)錯。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<<" 錯誤! 數(shù)組長度應(yīng)為正。\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<<" 數(shù)組長度不相同!"<

  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<

  }

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

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

計(jì)算機(jī)二級《C++》上機(jī)試題及答案08-12

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

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

計(jì)算機(jī)二級C++上機(jī)考試試題06-16

計(jì)算機(jī)二級《C++》上機(jī)考前沖刺試題06-25

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

計(jì)算機(jī)二級C++模擬試題及答案01-23

2015計(jì)算機(jī)二級Access上機(jī)模擬試題04-17