21xrx.com
2025-04-23 11:58:54 Wednesday
文章检索 我的文章 写文章
C++制作成绩表教程
2023-06-22 01:15:25 深夜i     14     0
C++ 制作 成绩表 教程 学习

C++是一种高级编程语言,通常用于开发大型软件和游戏等复杂的计算机程序。其强大的功能和可扩展性使其成为了大量开发人员的选择。

要制作一个简单的成绩表,我们可以使用C++来实现。在这篇文章中,我们将介绍如何使用C++编写一个成绩表,其中包括输入学生姓名、课程名称和成绩,计算平均成绩和总成绩,并输出结果。

首先,我们需要定义一些变量来存储学生姓名、课程名称和成绩。在这个例子中,我们将使用数组来存储学生的成绩信息。下面是定义变量的代码:

#include <iostream>
using namespace std;
const int MAX_STUDENTS = 50;
const int MAX_COURSES = 5;
string students[MAX_STUDENTS];
string courses[MAX_COURSES];
int grades[MAX_STUDENTS][MAX_COURSES];

在这个例子中,我们使用const关键字来定义了最大学生数和最大课程数。我们还使用了string类型来存储学生的姓名和课程的名称,以及一个二维数组来存储学生的成绩信息。

接下来,我们需要让用户输入学生姓名、课程名称和成绩。这里我们可以使用for循环来遍历学生和课程,然后使用cin来获取用户输入的成绩信息。下面是输入成绩信息的代码:

cout << "Enter student names:" << endl;
for (int i = 0; i < MAX_STUDENTS; i++) {
  cin >> students[i];
}
cout << "Enter course names:" << endl;
for (int i = 0; i < MAX_COURSES; i++) {
  cin >> courses[i];
}
cout << "Enter grades:" << endl;
for (int i = 0; i < MAX_STUDENTS; i++) {
  cout << "Enter grades for " << students[i] << ":" << endl;
  for (int j = 0; j < MAX_COURSES; j++) {
    cin >> grades[i][j];
  }
}

在这个例子中,我们首先通过for循环让用户输入学生和课程名称。然后,在嵌套的for循环中,我们使用cout输出学生姓名,并让用户输入该学生的每个课程的成绩。这些成绩将存储在二维数组grades中。

接下来,我们需要计算每个学生的平均成绩和总成绩。为了实现这个功能,我们可以再次使用for循环遍历学生和课程,并计算每个学生的平均成绩和总成绩。下面是计算平均成绩和总成绩的代码:

cout << "Grades table:" << endl;
cout << "Name\tTotal\tAverage" << endl;
for (int i = 0; i < MAX_STUDENTS; i++) {
  int total = 0;
  for (int j = 0; j < MAX_COURSES; j++) {
    total += grades[i][j];
  }
  int average = total / MAX_COURSES;
  cout << students[i] << "\t" << total << "\t" << average << endl;
}

在这个例子中,我们首先使用cout输出成绩表的标题。然后,在for循环中,我们遍历每个学生并计算该学生的总成绩和平均成绩。最后,我们使用cout输出学生的姓名、总成绩和平均成绩。

最后,我们可以使用return语句结束程序。下面是完整的C++代码:

#include <iostream>
using namespace std;
const int MAX_STUDENTS = 50;
const int MAX_COURSES = 5;
string students[MAX_STUDENTS];
string courses[MAX_COURSES];
int grades[MAX_STUDENTS][MAX_COURSES];
int main() {
  cout << "Enter student names:" << endl;
  for (int i = 0; i < MAX_STUDENTS; i++) {
    cin >> students[i];
  }
  cout << "Enter course names:" << endl;
  for (int i = 0; i < MAX_COURSES; i++) {
    cin >> courses[i];
  }
  cout << "Enter grades:" << endl;
  for (int i = 0; i < MAX_STUDENTS; i++) {
    cout << "Enter grades for " << students[i] << ":" << endl;
    for (int j = 0; j < MAX_COURSES; j++) {
      cin >> grades[i][j];
    }
  }
  cout << "Grades table:" << endl;
  cout << "Name\tTotal\tAverage" << endl;
  for (int i = 0; i < MAX_STUDENTS; i++) {
    int total = 0;
    for (int j = 0; j < MAX_COURSES; j++) {
      total += grades[i][j];
    }
    int average = total / MAX_COURSES;
    cout << students[i] << "\t" << total << "\t" << average << endl;
  }
  return 0;
}

以上就是用C++编写成绩表的全部内容,希望能够对您有所帮助!

  
  

评论区

请求出错了