学生学籍管理系统统计各专业成绩以及性别还有籍贯时输出结果不符合实际情况

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include<map>
#include<sstream>
using namespace std;

// 学生类
class Student {
public:
    string id; // 学号
    string name; // 姓名 
    string gender; // 性别
    string major; // 专业
    string status; // 学籍状态
    string birthplace; // 籍贯

    // 构造函数
    Student(string id, string name, string gender, string major, string status, string birthplace) {
        this->id = id;
        this->name = name;
        this->gender = gender;
        this->major = major;
        this->status = status;
        this->birthplace = birthplace;
    }

    // 输出学生信息
    void print() {
        cout << "学号:" << id << "\t姓名:" << name << "\t性别:" << gender << "\t专业:" << major << "\t学籍状态:" << status << "\t籍贯:" << birthplace << endl;
    }
};

// 成绩类
class Score {
public:
    string id; // 学号
    string course; // 课程名称
    int score; // 成绩

    // 构造函数
    Score(string id, string course, int score) {
        this->id = id;
        this->course = course;
        this->score = score;
    }

    // 输出成绩信息
    void print() {
        cout << "学号:" << id << "\t课程名称:" << course << "\t成绩:" << score << endl;
    }
};

// 学籍管理系统类
class StudentManagementSystem {
private:
    vector<Student> students; // 存储学生信息
    vector<Score> scores; // 存储成绩信息
public:
    // 构造函数
    StudentManagementSystem() {
        load(); // 加载数据
    }

    // 增加学生信息
    void addStudent(string id,string name, string gender, string major, string status, string birthplace) {
        Student student(id, name, gender, major, status, birthplace);
        students.push_back(student);
        cout << "添加成功!" << endl;
    }

    // 删除学生信息
    void deleteStudent(string id) {
        for (auto it = students.begin(); it != students.end(); it++) {
            if (it->id == id) {
                students.erase(it); // 删除找到的学生
                cout << "删除成功!" << endl;
                return;
            }
        }
        cout << "学号为" << id << "的学生不存在!" << endl;
    }

    // 修改学生信息
    void updateStudent(string id, string name, string gender, string major, string status, string birthplace) {
        bool found = false;
        for (auto& student : students) {
            if (student.id == id) { // 找到对应学生
                if (name != "") {
                    student.name = name;
                }
                if (gender != "") {
                    student.gender = gender;
                }
                if (major != "") {
                    student.major = major;
                }
                if (status != "") {
                    student.status = status;
                }
                if (birthplace != "") {
                    student.birthplace = birthplace;
                }
                cout << "修改成功!" << endl;
                found = true;
                break;
            }
        }
        if (!found) {
            cout << "学号为" << id << "的学生不存在!" << endl;
        }
    }

    // 按姓名排序
    void sortByName() {
        sort(students.begin(), students.end(), [&](const Student& a, const Student& b) {return a.name < b.name; });
        cout << "按姓名排序成功!" << endl;
    }

    // 按学号排序
    void sortByID() {
        sort(students.begin(), students.end(), [&](const Student& a, const Student& b) {return a.id < b.id; });
        cout << "按学号排序成功!" << endl;
    }

    // 显示学生信息
    void displayStudents() {
        for (auto& student : students) {
            student.print();
        }
    }

    // 显示成绩信息
    void displayScores() {
        for (auto& score : scores) {
            score.print();
        }
    }

    // 查询学生基本情况
    void showStudentInfo(string idOrNameOrMajor) {
        bool found = false;
        for (auto& student : students) {
            if (student.id == idOrNameOrMajor || student.name == idOrNameOrMajor || student.major == idOrNameOrMajor) {
                student.print();
                found = true;
            }
        }
        if (!found) {
            cout << "未找到符合条件的学生!" << endl;
        }
    }

    // 查询学生成绩信息
    void showScoreInfo(string id) {
        bool found = false;
        for (auto& score : scores) {
            if (score.id == id) {
                score.print();
                found = true;
            }
        }
        if (!found) {
            cout << "未找到学号为" << id << "的学生成绩信息!" << endl;
        }
    }

    // 查询全部学生信息
    void showAllStudentInfo(string major) {
        bool found = false;
        for (auto& student : students) {
            if (major == "" || student.major == major) {
                student.print();
                found = true;
            }
        }
        if (!found) {
            cout << "未找到符合条件的学生!" << endl;
        }
    }

    // 统计各专业的成绩
    void totalScoreByMajor() {
        map<string, int> scoreByMajor;
        for (const auto& student : students) {
            if (student.status == "再读") {
                for (const auto& score : scores) {
                    if (score.id == student.id) {
                        scoreByMajor[score.course]++; // 统计每个课程的成绩人数
                    }
                }
            }
        }
        cout << "各专业的课程平均成绩如下:" << endl;
        for (const auto& it : scoreByMajor) {
            cout << it.first << ":" << it.second << endl;
        }
    }

    // 统计性别比例
    void genderRatio() {
        int maleCount = 0;
        int femaleCount = 0;
        for (const auto& student : students) {
            if (student.status == "再读") {
                if (student.gender == "男") {
                    maleCount++;
                }
                else {
                    femaleCount++;
                }
            }
        }
        cout << "男性人数:" << maleCount << "\t女性人数:" << femaleCount << endl;
    }

    // 统计籍贯
    void birthplaceRatio() {
        map<string, int> birthplaceCount;
        for (const auto& student : students) {
            if (student.status == "再读") {
                birthplaceCount[student.birthplace]++; // 统计籍贯人数
            }
        }
        cout << "各籍贯人数如下:" << endl;
        for (const auto& it : birthplaceCount) {
            cout << it.first << ":" << it.second << endl;
        }
    }

    // 保存数据
    void save() {
        ofstream studentOut("studentFile");
        for (auto& student : students) {
            studentOut << student.id << " " << student.name << " " << student.gender << " " << student.major << " " << student.status << " " << student.birthplace << endl;
        }
        studentOut.close();

        ofstream scoreOut("scoreFile");
        for (auto& score : scores) {
            scoreOut << score.id << " " << score.course << " " << score.score << endl;
        }
        scoreOut.close();
    }

    // 加载数据
    void load() {
        ifstream studentIn("studentFile");
        if (studentIn) {
            string line;
            while (getline(studentIn, line)) {
                string id, name, gender, major, status, birthplace;
                istringstream ss(line);
                ss >> id >> name >> gender >> major >> status >> birthplace;
                Student student(id, name, gender, major, status, birthplace);
                students.push_back(student);
            }
        }
        else {
            cout << "文件" << "studentFile" << "不存在!" << endl;
        }
        studentIn.close();

        ifstream scoreIn("scoreFile");
        if (scoreIn) {
            string line;
            while (getline(scoreIn, line)) {
                string id, course;
                int score;
                istringstream ss(line);
                ss >> id >> course >> score;
                Score scoreInfo(id, course, score);
                scores.push_back(scoreInfo);
            }
        }
        else {
            cout << "文件" << "scoreFile" << "不存在!" << endl;
        }
        scoreIn.close();
    }

    // 析构函数
    ~StudentManagementSystem() {
        save(); // 保存数据
    }
};

// 主函数
int main() {
    StudentManagementSystem sms;
    int choice = 0;
    while (true) {
        cout << "========== 学籍管理系统 ==========" << endl;
        cout << "\t1. 录入学生信息" << endl;
        cout << "\t2. 显示学生信息" << endl;
        cout << "\t3. 删除学生信息" << endl;
        cout << "\t4. 修改学生信息" << endl;
        cout << "\t5. 学生基本情况查询" << endl;
        cout << "\t6. 学生成绩查询" << endl;
        cout << "\t7. 全部学生信息查询" << endl;
        cout << "\t8. 统计各专业的成绩,性别比例及籍贯" << endl;
        cout << "\t9. 退出系统" << endl;
        cout << "请输入操作序号(1-9):" << endl;
        cin >> choice;
        switch (choice) {
        case 1: {
            string id,name, gender, major, status, birthplace;
            cout << "请输入学生id:" << endl;
            cin >> id;
            cout << "请输入学生姓名:" << endl;
            cin >> name;
            cout << "请输入学生性别:" << endl;
            cin >> gender;
            cout << "请输入学生专业:" << endl;
            cin >> major;
            cout << "请输入学生学籍状态:" << endl;
            cin >> status;
            cout << "请输入学生籍贯:" << endl;
            cin >> birthplace;
            sms.addStudent(id,name, gender, major, status, birthplace);
            break;
        }
        case 2: {
            int subChoice = 0;
            cout << "========== 显示学生信息 ==========" << endl;
            cout << "\t1. 按姓名排序显示" << endl;
            cout << "\t2. 按学号排序显示" << endl;
            cout << "请输入操作序号(1-2):" << endl;
            cin >> subChoice;
            if (subChoice == 1) {
                sms.sortByName();
            }
            else if (subChoice == 2) {
                sms.sortByID();
            }
            else {
                cout << "无效操作序号!" << endl;
            }
            sms.displayStudents();
            break;
        }
        case 3: {
            string id;
            cout << "请输入要删除的学生的学号:" << endl;
            cin >> id;
            sms.deleteStudent(id);
            break;
        }
        case 4: {
            string id, name, gender, major, status, birthplace;
            cout << "请输入要修改的学生的学号:" << endl;
            cin >> id;
            cout << "请输入学生姓名:" << endl;
            cin >> name;
            cout << "请输入学生性别:" << endl;
            cin >> gender;
            cout << "请输入学生专业:" << endl;
            cin >> major;
            cout << "请输入学生学籍状态:" << endl;
            cin >> status;
            cout << "请输入学生籍贯:" << endl;
            cin >> birthplace;
            sms.updateStudent(id, name, gender, major, status, birthplace);
            break;
        }
        case 5: {
            string idOrNameOrMajor;
            cout << "请输入学号、姓名或专业:" << endl;
            cin >> idOrNameOrMajor;
            sms.showStudentInfo(idOrNameOrMajor);
            break;
        }
        case 6: {
            string id;
            cout << "请输入要查询的学生的学号:" << endl;
            cin >> id;
            sms.showScoreInfo(id);
            break;
        }
        case 7: {
            string major;
            cout << "请输入专业名称(留空则查询全部专业):" << endl;
            cin >> major;
            sms.showAllStudentInfo(major);
            break;
        }
        case 8: {
            sms.totalScoreByMajor();
            sms.genderRatio();
            sms.birthplaceRatio();
            break;
        }
        case 9: {
            sms.save();
            cout << "感谢使用学籍管理系统,再见!" << endl;
            return 0;
        }
        default: {
            cout << "无效操作序号!" << endl;
            break;
        }
        }
    }
    return 0;
}

这个系统运行成功之后使用成绩统计时可以输出但是输出结果不对。,帮帮忙帮我看一下哪的问题,代码可以直接运行,但是case8就是错的。

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7656624
  • 你也可以参考下这篇文章:【数据结构与算法 8】递归之迷宫问题
  • 除此之外, 这篇博客: 详细且图文并茂的单链表学习教程中的 8. 返回单链表中第一个与指定值相同的元素的位置 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • /**
     * 返回指定元素在链表中第一个出现的位置
     *
     * @param element
     * @return
     */
    public int locate(E element){
        Node<E> p = first.next;
    
        for (int i = 0; i < size && p != null; i++, p = p.next){
            if(p.element.equals(element)){
                return i;
            }
        }
        return -1;// 该元素在链表中不存在
    }