下面是一个可能的实现:

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

using namespace std;

class Student {
private:
    string id;
    string name;
    int math;
    int chinese;
    int english;
public:
    Student(string id, string name, int math, int chinese, int english) {
        this->id = id;
        this->name = name;
        this->math = math;
        this->chinese = chinese;
        this->english = english;
    }
    
    string getId() {
        return id;
    }
    
    string getName() {
        return name;
    }
    
    int getMath() {
        return math;
    }
    
    int getChinese() {
        return chinese;
    }
    
    int getEnglish() {
        return english;
    }
    
    int getTotalScore() {
        return math + chinese + english;
    }
    
    float getAverageScore() {
        return getTotalScore() / 3.0;
    }
};

class System {
private:
    vector<Student> students;
public:
    void create() {
        int n;
        cout << "请输入学生数量:";
        cin >> n;
        
        for (int i = 0; i < n; i++) {
            string id, name;
            int math, chinese, english;
            
            cout << "请输入第" << i+1 << "个学生的学号:";
            cin >> id;
            
            // 检查学号是否已存在
            for (int j = 0; j < students.size(); j++) {
                if (students[j].getId() == id) {
                    cout << "该学号已存在,请重新输入学号:" << endl;
                    cin >> id;
                    j = -1; // 重新检查
                }
            }
            
            cout << "请输入第" << i+1 << "个学生的姓名:";
            cin >> name;
            
            cout << "请输入第" << i+1 << "个学生的数学成绩:";
            cin >> math;
            
            cout << "请输入第" << i+1 << "个学生的语文成绩:";
            cin >> chinese;
            
            cout << "请输入第" << i+1 << "个学生的英语成绩:";
            cin >> english;
            
            students.push_back(Student(id, name, math, chinese, english));
        }
        
        cout << "学生信息创建成功!" << endl;
    }
    
    void add() {
        string id, name;
        int math, chinese, english;
        
        cout << "请输入学生的学号:";
        cin >> id;
        
        // 检查学号是否已存在
        for (int j = 0; j < students.size(); j++) {
            if (students[j].getId() == id) {
                cout << "该学号已存在,请重新输入学号:" << endl;
                cin >> id;
                j = -1; // 重新检查
            }
        }
        
        cout << "请输入学生的姓名:";
        cin >> name;
        
        cout << "请输入学生的数学成绩:";
        cin >> math;
        
        cout << "请输入学生的语文成绩:";
        cin >> chinese;
        
        cout << "请输入学生的英语成绩:";
        cin >> english;
        
        students.push_back(Student(id, name, math, chinese, english));
        
        cout << "学生信息添加成功!" << endl;
    }
    
    void summary() {
        int totalScore = 0;
        float averageScore = 0;
        
        for (int i = 0; i < students.size(); i++) {
            totalScore += students[i].getTotalScore();
        }
        
        averageScore = totalScore / (float)students.size();
        
        cout << "总分:" << totalScore << endl;
        cout << "平均分:" << averageScore << endl;
    }
    
    void sort() {
        std::sort(students.begin(), students.end(), [](const Student& s1, const Student& s2) {
            return s1.getTotalScore() > s2.getTotalScore();
        });
        
        cout << "学生信息已按总分降序排序!" << endl;
    }
    
    void search() {
        string id;
        cout << "请输入要查询的学生学号:";
        cin >> id;
        
        for (int i = 0; i < students.size(); i++) {
            if (students[i].getId() == id) {
                cout << "学号:" << students[i].getId() << endl;
                cout << "姓名:" << students[i].getName() << endl;
                cout << "数学成绩:" << students[i].getMath() << endl;
                cout << "语文成绩:" << students[i].getChinese() << endl;
                cout << "英语成绩:" << students[i].getEnglish() << endl;
                cout << "总分:" << students[i].getTotalScore() << endl;
                cout << "平均分:" << students[i].getAverageScore() << endl;
                return;
            }
        }
        
        cout << "未找到该学生的信息!" << endl;
    }
    
    void display() {
        for (int i = 0; i < students.size(); i++) {
            cout << "学号:" << students[i].getId() << endl;
            cout << "姓名:" << students[i].getName() << endl;
            cout << "数学成绩:" << students[i].getMath() << endl;
            cout << "语文成绩:" << students[i].getChinese() << endl;
            cout << "英语成绩:" << students[i].getEnglish() << endl;
            cout << "总分:" << students[i].getTotalScore() << endl;
            cout << "平均分:" << students[i].getAverageScore() << endl;
            cout << endl;
        }
    }
    
    void importFromFile() {
        string filename;
        cout << "请输入要导入的文件名:";
        cin >> filename;
        
        ifstream file(filename);
        if (!file) {
            cout << "文件打开失败!" << endl;
            return;
        }
        
        string id, name;
        int math, chinese, english;
        
        while (file >> id >> name >> math >> chinese >> english) {
            students.push_back(Student(id, name, math, chinese, english));
        }
        
        file.close();
        
        cout << "学生信息导入成功!" << endl;
    }
    
    void exportToFile() {
        string filename;
        cout << "请输入要导出的文件名:";
        cin >> filename;
        
        ofstream file(filename);
        if (!file) {
            cout << "文件打开失败!" << endl;
            return;
        }
        
        for (int i = 0; i < students.size(); i++) {
            file << students[i].getId() << " "
                 << students[i].getName() << " "
                 << students[i].getMath() << " "
                 << students[i].getChinese() << " "
                 << students[i].getEnglish() << endl;
        }
        
        file.close();
        
        cout << "学生信息导出成功!" << endl;
    }
};

int main() {
    System system;
    
    while (true) {
        cout << "学生成绩管理系统:" << endl;
        cout << "1 创建" << endl;
        cout << "2 添加" << endl;
        cout << "3 汇总" << endl;
        cout << "4 排序" << endl;
        cout << "5 查询" << endl;
        cout << "6 显示" << endl;
        cout << "7 文件导入" << endl;
        cout << "8 文件导出" << endl;
        cout << "9 退出" << endl;
        cout << "请选择:";
        
        int choice;
        cin >> choice;
        
        switch (choice) {
            case 1:
                system.create();
                break;
            case 2:
                system.add();
                break;
            case 3:
                system.summary();
                break;
            case 4:
                system.sort();
                break;
            case 5:
                system.search();
                break;
            case 6:
                system.display();
                break;
            case 7:
                system.importFromFile();
                break;
            case 8:
                system.exportToFile();
                break;
            case 9:
                return 0;
            default:
                cout << "无效的选择,请重新选择!" << endl;
                break;
        }
        
        cout << endl;
    }
    
    return 0;
}

这个程序使用了两个类:StudentSystemStudent类表示学生,包含学号、姓名、数学、语文和英语成绩等属性,并提供了获取总分和平均分的方法。System类表示学生成绩管理系统,包含了用于管理学生信息的各种功能,如创建、添加、汇总、排序、查询、显示、文件导入和文件导出等。在main函数中,通过一个循环来驱动菜单,根据用户的选择调用相应的功能

编写一个C++代码学生成绩管理程序:设计一个菜单驱动的学生成绩管理程序。输入学生的基本信息包括学生的学号、姓名、数学、语文、英语等信息实现如下管理功能:1 能输入并显示n 个学生的基本信息。2 计算学生的平均分和总分。3 按总分降序进行排序。4 任意输入一个学号能显示该学生的基本信息。5 可根据需要添加学生信息到指定位置亦可在最后追加一批学生信息。6文件的导入和导出从文件中读取若干条学生信息或者将

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

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