计算机等级考试二级C++上机模拟试题

时间:2020-08-29 08:27:29 计算机等级 我要投稿

2016计算机等级考试二级C++上机模拟试题

  一、改错题

2016计算机等级考试二级C++上机模拟试题

  使用VC6打开考生文件夹下的工程kt6_1,此工程包含一个源程序文件kt6_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:

  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)主要考查类成员函数定义格式的熟练掌握,对于类体外函数的实现,应该使用作用域符"::",按照返回值类型类名::函数名(参数列表)的形式进行说明;

  (2)主要考查对动态存储分配的掌握,根据前面的定义,a是一个指针类型的变量,指向一个对象,但是并没有被初始化,此时a中的数据无任何意义,应该使用动态存储分配new生成一个新的对象,并将返回的指针赋值给a;

  (3)主要考查对象指针与对象在调用成员函数时格式的不同,b是一个对象变量,使用b调用成员函数应该用"."运算符。

  修改后代码:

  #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();

  }

  二、简单应用题

  编写函数fun(),它的功能是利用以下所示的简单迭代方法求方程cos(x)-x=0的一个实根。

  xn+1=cos(xn)

  迭代步骤如下:

  (1)取x1初值为0.0。

  (2)x0=x1,把x1的值赋给x0。

  (3)x1=cos(x0),求出一个新的x1。

  (4)若x0-x1的绝对值小于0.000001,则执行步骤(5),否则执行步骤(2)。

  (5)所求x1就是方程cos(x)-x=0的一个实根,做为函数值返回。

  程序输出结果Root=0.739085。

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

  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

  文件kt6_2的内容如下:

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

  }

  【试题解析】解答本题的关键之处在于看清题中所给的“迭代步骤”,同时要理解xn+1=cosxn通式的含义,要考虑到x1的初值为0.0。

  fabs(x0-x1)>=0.000001和fabs(x0-x1)>=1e-6的输出结果一致,

  但是如果没有fabs,最后输出结果会直接为Root=1而非Root=0.739085,

  ps:fabs(x)为对x求绝对值。说明:计算|x|,当x不为负时返回x,否则返回-x。abs和fabs是一对常用的数学函数,abs是整数取绝对值,而fabs主要用于求浮点数的绝对值。

  #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="<

  }

  三、综合应用题

  使用VC6打开考生文件夹下的工程kt6_3,此工程包含一个源程序文件kt6_3.cpp,其中定义了用于表示考生的类Student,请按要求完成下列操作,将程序补充完整。

  (1)定义私有数据成员code、english分别用于表示考生的编号、英语成绩、它们都是int型的数据。

  。请在注释“//**1**”之后添加适当的语句。

  (2)完成成员函数voidStudent::inputinformation()的定义,该函数用于用户输入一个考生对象的信息,输入格式如下所示:

  输入编号:

  英语成绩:

  计算机成绩:

  请在注释“//**2**”之后添加适当的.语句。

  (3)利用已实现的类Student的成员函数,完成函数voidfirstname(Student*A[],intnum)的定义,该函数根据考生信息A[],输出num个考生中总分最高者的编号及其相应的总分,在此不考虑总分相同的情况。请在注释“//**3**”之后添加适当的语句。

  注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。

  源程序文件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<<"计算机成绩:";

  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++程序设计的综合考查,类的成员及成员函数的定义与调用,数据的输入输出,for循环语句,if条件判断语句等多个知识点,其中(3)中为指针数组的使用,指针数组是一组指针,每一个成员都按照指针的操作规则,但是整个访问规则仍然使用数组下标方式,如A[0]指的是第一个指针,而* A[0]是取出第一个指针指向的内容。

  #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<<"计算机成绩:";

  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<<"总分最高者--"<

【2016计算机等级考试二级C++上机模拟试题】相关文章:

2017计算机等级考试二级C++考试试题06-23

2017计算机等级考试二级模拟试题07-28

全国计算机等级二级Access上机试题07-19

计算机二级考试C++试题07-21

计算机等级考试上机应试技巧10-17

2017年9月计算机二级C++考试模拟试题06-14

2017年计算机二级C++模拟试题06-05

2017计算机二级C++考试试题06-05

2016秘书资格二级考试模拟试题09-17

2017年全国计算机等级c++考试试题06-23