学生成绩分析系统C++
时间: 2024-01-25 09:47:50 浏览: 76
Student_score_management_system.zip_C++_Score!_stick2k4_学生成绩管理系统
一个学生成绩分析系统可以有以下功能:
1. 输入学生信息和成绩数据
2. 统计每个学生的总分和平均分
3. 统计每个科目的平均分和总分
4. 输出每个学生的成绩单,包括每个科目的成绩、总分和平均分
5. 输出所有学生的平均分和总分
6. 根据学生的总分或平均分进行排序并输出排名
下面是一个简单的C++程序示例:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生信息结构体
struct Student {
string name; // 姓名
int id; // 学号
vector<int> scores; // 分数
int total_score; // 总分
double avg_score; // 平均分
};
// 计算学生总分和平均分
void calc_student_score(Student& s) {
s.total_score = 0;
for (int score : s.scores) {
s.total_score += score;
}
s.avg_score = static_cast<double>(s.total_score) / s.scores.size();
}
// 计算每个科目的平均分和总分
void calc_subject_score(const vector<Student>& students, vector<int>& subject_total_scores, vector<double>& subject_avg_scores) {
subject_total_scores.clear();
subject_avg_scores.clear();
int num_subjects = students[0].scores.size();
subject_total_scores.resize(num_subjects, 0);
subject_avg_scores.resize(num_subjects, 0.0);
for (const Student& s : students) {
for (int i = 0; i < num_subjects; i++) {
subject_total_scores[i] += s.scores[i];
}
}
for (int i = 0; i < num_subjects; i++) {
subject_avg_scores[i] = static_cast<double>(subject_total_scores[i]) / students.size();
}
}
// 输出学生信息和成绩
void print_student(const Student& s) {
cout << "姓名: " << s.name << endl;
cout << "学号: " << s.id << endl;
cout << "成绩: ";
for (int score : s.scores) {
cout << score << " ";
}
cout << endl;
cout << "总分: " << s.total_score << endl;
cout << "平均分: " << s.avg_score << endl;
}
// 输出所有学生的平均分和总分
void print_summary(const vector<Student>& students) {
int total_score = 0;
double avg_score = 0.0;
for (const Student& s : students) {
total_score += s.total_score;
avg_score += s.avg_score;
}
avg_score /= students.size();
cout << "所有学生总分: " << total_score << endl;
cout << "所有学生平均分: " << avg_score << endl;
}
// 按总分排序比较函数
bool compare_total_score(const Student& s1, const Student& s2) {
return s1.total_score > s2.total_score;
}
// 按平均分排序比较函数
bool compare_avg_score(const Student& s1, const Student& s2) {
return s1.avg_score > s2.avg_score;
}
int main() {
vector<Student> students;
vector<int> subject_total_scores;
vector<double> subject_avg_scores;
// 输入学生信息和成绩数据
int num_students, num_subjects;
cout << "请输入学生人数: ";
cin >> num_students;
cout << "请输入科目数: ";
cin >> num_subjects;
students.resize(num_students);
for (int i = 0; i < num_students; i++) {
cout << "请输入第" << i + 1 << "个学生的姓名和学号: ";
cin >> students[i].name >> students[i].id;
students[i].scores.resize(num_subjects);
cout << "请输入第" << i + 1 << "个学生的成绩: ";
for (int j = 0; j < num_subjects; j++) {
cin >> students[i].scores[j];
}
calc_student_score(students[i]);
}
// 统计每个科目的平均分和总分
calc_subject_score(students, subject_total_scores, subject_avg_scores);
// 输出每个科目的平均分和总分
cout << "每个科目的总分和平均分: " << endl;
for (int i = 0; i < num_subjects; i++) {
cout << "科目" << i + 1 << "总分: " << subject_total_scores[i] << endl;
cout << "科目" << i + 1 << "平均分: " << subject_avg_scores[i] << endl;
}
// 输出每个学生的成绩单
cout << "每个学生的成绩单: " << endl;
for (const Student& s : students) {
print_student(s);
}
// 输出所有学生的平均分和总分
print_summary(students);
// 按总分排序并输出排名
sort(students.begin(), students.end(), compare_total_score);
cout << "按总分排名: " << endl;
for (int i = 0; i < num_students; i++) {
cout << "第" << i + 1 << "名: " << students[i].name << " 总分: " << students[i].total_score << endl;
}
// 按平均分排序并输出排名
sort(students.begin(), students.end(), compare_avg_score);
cout << "按平均分排名: " << endl;
for (int i = 0; i < num_students; i++) {
cout << "第" << i + 1 << "名: " << students[i].name << " 平均分: " << students[i].avg_score << endl;
}
return 0;
}
```
阅读全文