首页 新闻 会员 周边

76行为什么有错误?帮我找找其他错误

0
悬赏园豆:20 [待解决问题]

include<bits/stdc++.h>

using namespace std;

// 学生类
class Student {
public:
string name;
vector<int> scores;

Student(string n) {
    name = n;
    scores.resize(10, 0); // 初始化成绩数组,包括10门科目
}

void SetScore(int index, int score) {
    scores[index] = score;
}

int GetScore(int index) {
    return scores[index];
}

double GetAverageScore() {
    int sum = accumulate(scores.begin(), scores.end(), 0);
    return static_cast<double>(sum) / scores.size();
}

double GetMedianScore() {
    vector<int> sortedScores = scores;
    sort(sortedScores.begin(), sortedScores.end());

    int size = sortedScores.size();
    if (size % 2 == 0) {
        return (sortedScores[size / 2 - 1] + sortedScores[size / 2]) / 2.0;
    } else {
        return sortedScores[size / 2];
    }
}

int GetTotalScore() {
    return accumulate(scores.begin(), scores.end(), 0);
}

};

// 学生成绩管理系统类
class GradeManager {
private:
vector<Student> students;
string dataFile;

public:
GradeManager(string file) {
dataFile = file;
LoadData();
}

void AddStudent() {
    string name;
    cout << "请输入学生姓名: ";
    cin >> name;

    students.push_back(Student(name));
    cout << "添加成功!" << endl;
}

void DeleteStudent() {
    if (students.empty()) {
        cout << "暂无学生信息" << endl;
        return;
    }

    string name;
    cout << "请输入要删除的学生姓名: ";
    cin >> name;

    auto  it = find_if(students.begin(), students.end(), [&](const Student& student) {
        return student.name == name;
    });

    if (it != students.end()) {
        students.erase(it);
        cout << "删除成功!" << endl;
    } else {
        cout << "未找到该学生" << endl;
    }
}

void SetStudentScore() {
    if (students.empty()) {
        cout << "暂无学生信息" << endl;
        return;
    }

    string name;
    cout << "请输入学生姓名: ";
    cin >> name;

    auto it = find_if(students.begin(), students.end(), [&](const Student& student) {
        return student.name == name;
    });

    if (it != students.end()) {
        int score;
        for (int i = 0; i < 10; ++i) {
            cout << "请输入" << name << "的第" << i + 1 << "门科目的成绩: ";
            cin >> score;
            it->SetScore(i, score);
        }
        cout << "成绩设置成功!" << endl;
    } else {
        cout << "未找到该学生" << endl;
    }
}

void ShowGrades() {
    if (students.empty()) {
        cout << "暂无学生信息" << endl;
        return;
    }

    cout << "学生成绩如下:" << endl;
    for (const auto& student : students) {
        cout << "姓名: " << student.name << endl;
        for (int i = 0; i < 10; ++i) {
            cout << "第" << i + 1 << "门科目: " << student.GetScore(i) << endl;
        }
        cout << "平均分: " << student.GetAverageScore() << endl;
        cout << "中位数: " << student.GetMedianScore() << endl;
        cout << "总分: " << student.GetTotalScore() << endl;
        cout << endl;
    }
}

void SaveData() {
    ofstream outFile(dataFile);

    if (!outFile) {
        cout << "保存数据失败" << endl;
        return;
    }

    for (const auto& student : students) {
        outFile << student.name << " ";
        for (int i = 0; i < 10; ++i) {
            outFile << student.GetScore(i) << " ";
        }
        outFile << student.GetAverageScore() << " ";
        outFile << student.GetMedianScore() << " ";
        outFile << student.GetTotalScore() << endl;
    }

    outFile.close();
    cout << "数据保存成功" << endl;
}

void LoadData() {
    ifstream inFile(dataFile);

    if (!inFile) {
        cout << "加载数据失败" << endl;
        return;
    }

    string name;
    int score;

    while (inFile >> name) {
        students.push_back(Student(name));
        for (int i = 0; i < 10; ++i) {
            inFile >> score;
            students.back().SetScore(i, score);
        }
    }

    inFile.close();
    cout << "数据加载成功" << endl;
}

};

int main() {
GradeManager manager("data.txt");

while (true) {
    cout << "1. 添加学生" << endl;
    cout << "2. 删除学生" << endl;
    cout << "3. 设置学生成绩" << endl;
    cout << "4. 显示成绩" << endl;
    cout << "5. 保存数据" << endl;
    cout << "6. 退出程序" << endl;

    int choice;
    cout << "请输入选项: ";
    cin >> choice;

    switch (choice) {
        case 1:
            manager.AddStudent();
            break;
        case 2:
            manager.DeleteStudent();
            break;
        case 3:
            manager.SetStudentScore();
            break;
        case 4:
            manager.ShowGrades();
            break;
        case 5:
            manager.SaveData();
            break;
        case 6:
            manager.SaveData();
            cout << "程序已退出" << endl;
            return 0;
        default:
            cout << "无效选项,请重新输入" << endl;
            break;
    }
}

return 0;

}

C++
曙光科技的主页 曙光科技 | 初学一级 | 园豆:52
提问于:2024-03-03 12:39
< >
分享
所有回答(1)
0

可以尝试给class students里面的函数使用const this指针

int GetScore(int index)const {...}
double GetAverageScore()const {...}
double GetMedianScore()const {...}
int GetTotalScore()const {...}

你在调用时使用了const auto& student,创造了const量,
但是const在载入this指针时出现了constthis->this非法操作

随缘XD | 园豆:316 (菜鸟二级) | 2024-03-04 18:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册