ZBLOG

修改这一段 #include #include #include #include #include #include using namespace std; struct Student { string student_id; string name; string major; int math_score; int chinese_score; int...

#include #include #include #include #include #include

using namespace std;

struct Student {

  1. string student_id;
  2. string name;
  3. string major;
  4. int math_score;
  5. int chinese_score;
  6. int english_score;
  7. int total_score;

};

// 解析数据文件中的一行并创建一个学生对象 Student parseStudent(const string& line) {

  1. istringstream ss(line);
  2. string student_id, name, major;
  3. int math_score, chinese_score, english_score;
  4. ss >> student_id >> name >> major >> math_score >> chinese_score >> english_score;
  5. int total_score = math_score + chinese_score + english_score;
  6. return { student_id, name, major, math_score, chinese_score, english_score, total_score };

}

// 从文件中读取数据并将其存储在学生对象的向量中 vector readDataFromFile(const string& filename) {

  1. vector<Student> students;
  2. ifstream file(filename);
  3. if (file.is_open()) {
  4. string line;
  5. while (getline(file, line)) {
  6. students.push_back(parseStudent(line));
  7. }
  8. file.close();
  9. }
  10. else {
  11. cerr << "错误:无法打开文件 " << filename << endl;
  12. }
  13. return students;

}

// 用于按总分排序的比较函数(降序) bool compareByTotalScore(const Student& a, const Student& b) {

  1. return a.total_score > b.total_score;

}

void outputStudentsByMajor(const vector& students) {

  1. map<string, vector<Student>> studentsByMajor;
  2. map<string, int> totalScoreByMajor;
  3. // 初始化每个专业的总评分为0
  4. for (const auto& student : students) {
  5. totalScoreByMajor[student.major] = 0;
  6. }
  7. // 将学生按专业分类,并计算每个专业的总评分
  8. for (const auto& student : students) {
  9. studentsByMajor[student.major].push_back(student);
  10. totalScoreByMajor[student.major] += student.total_score;
  11. }
  12. // 输出每个专业的学生总评信息
  13. for (auto& pair : studentsByMajor) {
  14. sort(pair.second.begin(), pair.second.end(), compareByTotalScore);
  15. cout << "专业:" << pair.first << endl;
  16. cout << "总评:" << totalScoreByMajor[pair.first] << " 分" << endl;
  17. for (size_t i = 0; i < pair.second.size(); ++i) {
  18. cout << "排名 " << i + 1 << ":" << pair.second[i].name << "(" << pair.second[i].total_score << " 分)" << endl;
  19. }
  20. cout << endl;
  21. }

}

// 按姓名查找学生信息 void searchStudentByName(const vector& students, const string& name) {

  1. bool found = false;
  2. for (const auto& student : students) {
  3. if (student.name == name) {
  4. cout << "找到学生:" << endl;
  5. cout << "姓名:" << student.name << endl;
  6. cout << "学号:" << student.student_id<< endl;
  7. cout<< "专业:" student.major<<endl;
  8. cout<<"数学成绩:"<<student.math_score<<endl
  9. cout<<"语文成绩:"<<student.chinese_score<<endl
  10. cout<<"英语成绩:"<<student.english_score<<endl
  11. cout<<"总分:"<<student.total_score<<endl
  12. found = true;
  13. break;
  14. }
  15. }
  16. if (!found) {
  17. cout << "未找到姓名为 '" << name << "' 的学生。" << endl;
  18. }

}

int main() {

  1. string filename = "data.txt";
  2. vector<Student> students = readDataFromFile(filename);
  3. cout << "按专业输出学生总评信息:" << endl;
  4. outputStudentsByMajor(students);
  5. // 按姓名查找学生信息
  6. string nameToSearch = "张家辉"; // 修改此处为你想要查找的姓名
  7. cout << "正在查找姓名为 '" << nameToSearch<< "' 的学生:" << endl;
  8. searchStudentByName(students, nameToSearch);
  9. return 0;

}


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=15299

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?