编程:某班有最多不超过30人(具体人数由键盘输入)参加期末考试,最多不超过6门(具体门数由键盘输入)定义结构体类型描述学生信息,每个学生信息包括:学号、姓名、多门课的成绩、总成绩和平均成绩。用结构体数组作为函数参数,录入每个学生的学号、姓名和各科考试成绩。
时间: 2024-02-05 07:02:38 浏览: 25
好的,以下是C++代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义结构体类型描述学生信息
struct Student {
string id; // 学号
string name; // 姓名
int scores[6]; // 多门课的成绩
int totalScore; // 总成绩
float avgScore; // 平均成绩
};
// 录入每个学生的学号、姓名和各科考试成绩
void inputStudents(Student students[], int n, int m) {
for (int i = 0; i < n; i++) {
cout << "请输入第" << i+1 << "个学生的学号: ";
cin >> students[i].id;
cout << "请输入第" << i+1 << "个学生的姓名: ";
cin >> students[i].name;
cout << "请输入第" << i+1 << "个学生的" << m << "门课的成绩:" << endl;
int totalScore = 0;
for (int j = 0; j < m; j++) {
cout << "第" << j+1 << "门课的成绩: ";
cin >> students[i].scores[j];
totalScore += students[i].scores[j];
}
students[i].totalScore = totalScore;
students[i].avgScore = (float)totalScore / m;
}
}
int main() {
int n, m;
cout << "请输入学生人数和课程数: ";
cin >> n >> m;
Student students[n];
inputStudents(students, n, m);
return 0;
}
```
这段代码可以先让用户输入学生人数和课程数,然后根据输入的人数和课程数定义一个结构体数组,最后调用 `inputStudents` 函数录入每个学生的学号、姓名和各科考试成绩。在 `inputStudents` 函数中,我们先让用户输入学号和姓名,然后再让用户输入每门课的成绩,计算总成绩和平均成绩,并赋值给相应的结构体成员。