21xrx.com
2024-06-03 06:16:49 Monday
登录
文章检索 我的文章 写文章
【题解】东南大学C++期末考试题及答案
2023-07-12 10:40:43 深夜i     --     --
东南大学 C++ 期末考试 题解 答案

近日,有关东南大学C++期末考试题目及答案的信息在网络上广泛流传。据了解,该考试试卷难度较大,考查了学生们对于C++基础知识、指针、文件操作、继承与多态等方面的理解。以下是具体考试内容及答案分析。

第一题:求二维数组右上角的值

题目描述:给定一个由n*m个整数组成的二维数组,求出它的右上角的值。

答案分析:对于二维数组,我们可以定义一个指针,通过移动指针来访问数组的每一个元素。根据数组的定义,右上角元素的坐标为arr[0][m-1],因此只需将指针指向该元素即可。答案为:arr[0][m-1]。

第二题:求文件中数字的平均值

题目描述:给定一个文本文件,包含了若干个整数,求这些整数的平均值。

答案分析:首先需要读取文件中的数字,并计算它们的总和。可以利用文件输入流将数据从文件中读入,利用一个计数器来记录数字的个数,最后将总和除以数字个数即可得到平均值。代码如下:


#include <iostream>

#include <fstream>

using namespace std;

int main()

{

  int num, count = 0, sum = 0;

  double avg;

  ifstream fin("nums.txt");

  while (fin >> num)

  {

    sum += num;

    count++;

  }

  fin.close();

  avg = (double)sum / count;

  cout << "The average of the numbers is " << avg << endl;

  return 0;

}

第三题:设计一个图形类

题目描述:设计一个图形类,有计算面积和周长的方法,并对其进行继承,派生出矩形和圆形子类。

答案分析:这道题考查了面向对象编程的知识点,需要掌握类的设计、继承、虚函数等概念。首先创建一个包含面积和周长计算方法的图形类,由于不同的子类计算方法不同,可以将计算方法定义为虚函数,供子类进行重写。然后,定义矩形和圆形类,通过继承图形类并重写虚函数来实现面积和周长的计算。下面是代码实现:


#include <iostream>

using namespace std;

class Shape

{

public:

  virtual double getArea() {}

  virtual double getPerimeter() {}

};

class Rectangle : public Shape

{

private:

  double length, width;

public:

  Rectangle(double l, double w)

  

    length = l;

    width = w;

  

  double getArea()

  {

    return length * width;

  }

  double getPerimeter()

  {

    return 2 * (length + width);

  }

};

class Circle : public Shape

{

private:

  double radius;

  const double PI = 3.1415926535;

public:

  Circle(double r)

  

    radius = r;

  

  double getArea()

  {

    return PI * radius * radius;

  }

  double getPerimeter()

  {

    return 2 * PI * radius;

  }

};

int main()

{

  Shape* shape1 = new Rectangle(5, 3);

  cout << "The area of the rectangle is " << shape1->getArea() << endl;

  cout << "The perimeter of the rectangle is " << shape1->getPerimeter() << endl;

  Shape* shape2 = new Circle(5);

  cout << "The area of the circle is " << shape2->getArea() << endl;

  cout << "The perimeter of the circle is " << shape2->getPerimeter() << endl;

  delete shape1;

  delete shape2;

  return 0;

}

以上便是东南大学C++期末考试题目及答案的相关内容,希望对广大读者有所帮助,更多精彩内容敬请关注。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复