以下是一个简单的C++成绩管理系统的示例代码:

#include <iostream>
#include <vector>
#include <algorithm>

struct Student {
    std::string name;
    int score;
};

bool compareByScore(const Student& a, const Student& b) {
    return a.score > b.score;
}

int main() {
    std::vector<Student> students;
    int choice;
    
    while (true) {
        std::cout << "1. 添加学生" << std::endl;
        std::cout << "2. 显示所有学生" << std::endl;
        std::cout << "3. 按成绩排序并显示" << std::endl;
        std::cout << "4. 退出" << std::endl;
        std::cout << "请输入选项: ";
        std::cin >> choice;
        
        if (choice == 1) {
            std::string name;
            int score;
            
            std::cout << "请输入学生姓名: ";
            std::cin >> name;
            std::cout << "请输入学生成绩: ";
            std::cin >> score;
            
            Student student;
            student.name = name;
            student.score = score;
            
            students.push_back(student);
        } else if (choice == 2) {
            std::cout << "所有学生的信息:" << std::endl;
            for (const auto& student : students) {
                std::cout << "姓名: " << student.name << ", 成绩: " << student.score << std::endl;
            }
        } else if (choice == 3) {
            std::sort(students.begin(), students.end(), compareByScore);
            
            std::cout << "按成绩排序后的学生信息:" << std::endl;
            for (const auto& student : students) {
                std::cout << "姓名: " << student.name << ", 成绩: " << student.score << std::endl;
            }
        } else if (choice == 4) {
            break;
        } else {
            std::cout << "无效的选项,请重试。" << std::endl;
        }
        
        std::cout << std::endl;
    }
    
    return 0;
}

这个简单的成绩管理系统允许用户选择以下操作:

  1. 添加学生:用户可以输入学生的姓名和成绩,然后将学生信息添加到学生列表中。
  2. 显示所有学生:显示当前学生列表中的所有学生信息。
  3. 按成绩排序并显示:将学生列表按照成绩从高到低排序,并显示排序后的学生信息。
  4. 退出:退出程序。

这个示例代码使用了std::vector来存储学生信息,std::sort函数来对学生列表进行排序。可以根据自己的需求进行修改和扩展


原文地址: https://www.cveoy.top/t/topic/hYJP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录