用c++写学生成绩管理系统
时间: 2023-12-31 12:54:16 浏览: 30
下面是一个简单的学生成绩管理系统的 C++ 代码示例:
```c++
#include <iostream>
#include <string>
using namespace std;
const int MAX_STUDENT = 100; // 最大学生数
const int MAX_SUBJECT = 5; // 最大科目数
struct Student {
string name;
int score[MAX_SUBJECT]; // 每个学生的各科成绩
};
int main() {
Student students[MAX_STUDENT]; // 存储所有学生信息
int numStudents = 0; // 学生总数
int numSubjects = 0; // 科目总数
// 输入学生和科目总数
cout << "请输入学生总数: ";
cin >> numStudents;
cout << "请输入科目总数: ";
cin >> numSubjects;
// 输入每个学生的信息
for (int i = 0; i < numStudents; i++) {
cout << "请输入第" << i + 1 << "个学生的姓名: ";
cin >> students[i].name;
for (int j = 0; j < numSubjects; j++) {
cout << "请输入" << students[i].name << "的第" << j + 1 << "门成绩: ";
cin >> students[i].score[j];
}
}
// 输出每个学生的信息和平均分
for (int i = 0; i < numStudents; i++) {
cout << students[i].name << "的成绩为: ";
int sum = 0;
for (int j = 0; j < numSubjects; j++) {
cout << students[i].score[j] << " ";
sum += students[i].score[j];
}
double avg = (double)sum / numSubjects;
cout << "平均分为: " << avg << endl;
}
return 0;
}
```
这个例子中,使用了结构体 `Student` 存储每个学生的信息,包括姓名和各科成绩。在 `main` 函数中,首先输入学生和科目总数,然后循环输入每个学生的信息,最后输出每个学生的信息和平均分。