<1>在某系的成绩登记册中,每个班最多有40个学生,每份成绩表中的成绩信息包括:学号(9位字符),姓名(8位字符),成绩(百分制),备注(20位字符) 计程序以处理一个班级的成绩信息,包括输入、输出、查询(给定分数以上或以下的学生信息)、按分数排序等。用c++
时间: 2024-12-17 15:50:49 浏览: 1
在C++中设计一个用于处理班级成绩信息的程序,可以采用结构体来存储学生的详细信息,如`Student`结构:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// 定义学生结构体
struct Student {
std::string id;
std::string name;
int score;
std::string remark;
};
// 存储班级成绩的动态数组或容器
class GradeBook {
private:
std::vector<Student> students;
int max_students; // 每班最大人数
public:
GradeBook(int capacity) : max_students(capacity) {}
// 添加学生
void addStudent(const Student& student) {
if (students.size() < max_students)
students.push_back(student);
else
std::cout << "班级已满,无法添加更多学生。\n";
}
// 输入学生信息
void inputStudent(Student& student) {
std::cin >> student.id >> student.name >> student.score >> student.remark;
}
// 输出所有学生信息
void displayStudents() const {
for (const auto& student : students)
std::cout << "学号:" << student.id << ", 姓名:" << student.name << ", 成绩:" << student.score << "%, 备注:" << student.remark << "\n";
}
// 查询指定分数范围的学生
void searchByScore(int minScore, int maxScore) const {
std::vector<Student> filteredStudents;
for (const auto& student : students)
if (minScore <= student.score && student.score <= maxScore)
filteredStudents.push_back(student);
if (!filteredStudents.empty())
std::cout << "找到分数在[" << minScore << ", " << maxScore << "]之间的学生:\n";
else
std::cout << "没有找到符合条件的学生。\n";
for (const auto& student : filteredStudents)
std::cout << student.id << ", " << student.name << ", " << student.score << "%, " << student.remark << "\n";
}
// 按分数排序
void sortStudentsByScore() {
std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) { return a.score > b.score; });
std::cout << "按照分数从高到低排序的结果:\n";
displayStudents();
}
};
int main() {
GradeBook gradeBook(40); // 创建一个容纳40人的年级簿
int choice;
while (true) {
std::cout << "请选择操作:\n"
<< "1. 添加学生\n"
<< "2. 输入学生信息\n"
<< "3. 显示所有学生\n"
<< "4. 搜索成绩范围\n"
<< "5. 按分数排序\n"
<< "6. 退出\n"
<< "> ";
std::cin >> choice;
switch (choice) {
case 1:
Student newStudent;
gradeBook.inputStudent(newStudent);
gradeBook.addStudent(newStudent);
break;
case 2:
Student newStudent;
gradeBook.inputStudent(newStudent);
break;
case 3:
gradeBook.displayStudents();
break;
case 4:
int minScore, maxScore;
std::cout << "请输入最低分数和最高分数:";
std::cin >> minScore >> maxScore;
gradeBook.searchByScore(minScore, maxScore);
break;
case 5:
gradeBook.sortStudentsByScore();
break;
case 6:
return 0;
default:
std::cout << "无效选择,请重试。\n";
break;
}
}
}
阅读全文