#include #include #include #include

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() const {
    return id;
}

string getName() const {
    return name;
}

int getMath() const {
    return math;
}

int getChinese() const {
    return chinese;
}

int getEnglish() const {
    return english;
}

int getTotalScore() const {
    return math + chinese + english;
}

double getAverageScore() const {
    return static_cast<double>(getTotalScore()) / 3;
}

};

class System { private: vector students;

bool isIdDuplicate(string id) const {
    for (const Student& student : students) {
        if (student.getId() == id) {
            return true;
        }
    }
    return false;
}

void displayStudent(const Student& student) const {
    cout << "学号: " << student.getId() << endl;
    cout << "姓名: " << student.getName() << endl;
    cout << "数学: " << student.getMath() << endl;
    cout << "语文: " << student.getChinese() << endl;
    cout << "英语: " << student.getEnglish() << endl;
    cout << "总分: " << student.getTotalScore() << endl;
    cout << "平均分: " << student.getAverageScore() << endl;
}

public: void create() { int n; cout << "请输入学生数量: "; cin >> n;

    for (int i = 0; i < n; i++) {
        string id, name;
        int math, chinese, english;
        
        cout << "请输入学号: ";
        cin >> id;
        
        if (isIdDuplicate(id)) {
            cout << "学号重复,请重新输入" << endl;
            i--;
            continue;
        }
        
        cout << "请输入姓名: ";
        cin >> name;
        
        cout << "请输入数学成绩: ";
        cin >> math;
        
        cout << "请输入语文成绩: ";
        cin >> chinese;
        
        cout << "请输入英语成绩: ";
        cin >> english;
        
        students.push_back(Student(id, name, math, chinese, english));
    }
}

void add() {
    string id, name;
    int math, chinese, english;
    
    cout << "请输入学号: ";
    cin >> id;
    
    if (isIdDuplicate(id)) {
        cout << "学号重复,请重新输入" << endl;
        return;
    }
    
    cout << "请输入姓名: ";
    cin >> name;
    
    cout << "请输入数学成绩: ";
    cin >> math;
    
    cout << "请输入语文成绩: ";
    cin >> chinese;
    
    cout << "请输入英语成绩: ";
    cin >> english;
    
    students.push_back(Student(id, name, math, chinese, english));
}

void summary() {
    int totalScore = 0;
    double averageScore;
    
    for (const Student& student : students) {
        totalScore += student.getTotalScore();
    }
    
    averageScore = static_cast<double>(totalScore) / students.size();
    
    cout << "总分: " << totalScore << endl;
    cout << "平均分: " << averageScore << endl;
}

void sort() {
    std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
        return a.getTotalScore() > b.getTotalScore();
    });
    
    cout << "排序成功" << endl;
}

void query() {
    string id;
    cout << "请输入学号: ";
    cin >> id;
    
    for (const Student& student : students) {
        if (student.getId() == id) {
            displayStudent(student);
            return;
        }
    }
    
    cout << "未找到该学生" << endl;
}

void display() {
    for (const Student& student : students) {
        displayStudent(student);
        cout << endl;
    }
}

void importData() {
    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) {
        if (isIdDuplicate(id)) {
            cout << "学号重复,跳过该学生信息" << endl;
            continue;
        }
        
        students.push_back(Student(id, name, math, chinese, english));
    }
    
    file.close();
    
    cout << "导入成功" << endl;
}

void exportData() {
    string filename;
    cout << "请输入文件名: ";
    cin >> filename;
    
    ofstream file(filename);
    
    if (!file) {
        cout << "文件打开失败" << endl;
        return;
    }
    
    for (const Student& student : students) {
        file << student.getId() << " "
             << student.getName() << " "
             << student.getMath() << " "
             << student.getChinese() << " "
             << student.getEnglish() << endl;
    }
    
    file.close();
    
    cout << "导出成功" << endl;
}

};

int main() { System system; int choice;

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 << "请选择:";
    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.query();
            break;
        case 6:
            system.display();
            break;
        case 7:
            system.importData();
            break;
        case 8:
            system.exportData();
            break;
        case 9:
            return 0;
        default:
            cout << "无效选择,请重新输入" << endl;
            break;
    }
    
    cout << endl;
}
写c++程序:2 学生成绩管理程序:设计一个菜单驱动的学生成绩管理程序。输入学生的基本信息包括学生的学号、姓名、数学、语文、英语等信息实现如下管理功能:1 能输入并显示n 个学生的基本信息。2 计算学生的平均分和总分。3 按总分降序进行排序。4 任意输入一个学号能显示该学生的基本信息。5 可根据需要添加学生信息到指定位置亦可在最后追加一批学生信息。6文件的导入和导出从文件中读取若干条学生信息或者将

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

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