设计一个对学生成绩进行分析的程序,实现一个班级的学生成绩进行排序、求平均分, 求最高分、最低分以及分数段的人数进行统计等功能。并实现输出分析结果。(C++)
时间: 2024-04-22 10:21:41 浏览: 22
好的,下面是一个对学生成绩进行分析的程序的 C++ 实现,可以实现学生成绩排序、求平均分、求最高分、最低分以及分数段的人数进行统计等功能:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
struct Student {
std::string name;
int score;
};
bool compareStudent(const Student& a, const Student& b) {
return a.score > b.score;
}
double calculateAverageScore(const std::vector<Student>& students) {
double totalScore = 0;
for (const auto& student : students) {
totalScore += student.score;
}
return totalScore / students.size();
}
int findHighestScore(const std::vector<Student>& students) {
int highestScore = students[0].score;
for (const auto& student : students) {
if (student.score > highestScore) {
highestScore = student.score;
}
}
return highestScore;
}
int findLowestScore(const std::vector<Student>& students) {
int lowestScore = students[0].score;
for (const auto& student : students) {
if (student.score < lowestScore) {
lowestScore = student.score;
}
}
return lowestScore;
}
int countStudentsInScoreRange(const std::vector<Student>& students, int start, int end) {
int count = 0;
for (const auto& student : students) {
if (student.score >= start && student.score <= end) {
count++;
}
}
return count;
}
void analyzeScores(std::vector<Student>& students) {
std::sort(students.begin(), students.end(), compareStudent);
double averageScore = calculateAverageScore(students);
int highestScore = findHighestScore(students);
int lowestScore = findLowestScore(students);
std::vector<std::pair<int, int>> scoreRanges = {{90, 100}, {80, 89}, {70, 79}, {60, 69}, {0, 59}};
std::vector<int> scoreCounts;
for (const auto& scoreRange : scoreRanges) {
int count = countStudentsInScoreRange(students, scoreRange.first, scoreRange.second);
scoreCounts.push_back(count);
}
std::cout << "成绩分析结果:" << std::endl;
std::cout << "学生成绩排名:" << std::endl;
for (const auto& student : students) {
std::cout << student.name << ": " << student.score << std::endl;
}
std::cout << "平均分:" << averageScore << std::endl;
std::cout << "最高分:" << highestScore << std::endl;
std::cout << "最低分:" << lowestScore << std::endl;
for (int i = 0; i < scoreRanges.size(); i++) {
std::cout << scoreRanges[i].first << "分到" << scoreRanges[i].second << "分的人数:" << scoreCounts[i] << std::endl;
}
}
int main() {
std::vector<Student> students = {{"小明", 90}, {"小红", 85}, {"小华", 72}, {"小李", 96}, {"小张", 64}, {"小刘", 78}, {"小陈", 88}, {"小王", 94}, {"小赵", 80}, {"小钱", 58}};
analyzeScores(students);
return 0;
}
```
这个程序使用了一个名为 `Student` 的结构体来表示一个学生的信息,包括姓名和分数。`compareStudent` 函数用于排序,`calculateAverageScore` 函数计算学生的平均分,`findHighestScore` 和 `findLowestScore` 函数分别找到学生的最高分和最低分,`countStudentsInScoreRange` 函数统计各个分数段内的学生人数。`analyzeScores` 函数将所有结果打印出来。
您可以根据自己的需求对这个程序进行修改和扩展。