求解答整行排序有关问题
求解答整行排序问题
405505108 胡小玲 89 84 93 266
305505035 陈龙 88 78 87 253
405505021 陈军 83 85 91 259
305505066 崔建基 97 70 90 257
系统功能实现到:如上,输出个人的信息(其中总分已放进数组里),现要求按总分大小从高分到低分排序,求大神们帮忙想想怎样做?最好有代码参考了
------解决方案--------------------
参考下面的代码中的“按总分排名”
405505108 胡小玲 89 84 93 266
305505035 陈龙 88 78 87 253
405505021 陈军 83 85 91 259
305505066 崔建基 97 70 90 257
系统功能实现到:如上,输出个人的信息(其中总分已放进数组里),现要求按总分大小从高分到低分排序,求大神们帮忙想想怎样做?最好有代码参考了
------解决方案--------------------
参考下面的代码中的“按总分排名”
- C/C++ code
#include <map> #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; // 学生类 class Student { private: int id; // 学号 string name; // 姓名 double chinese; // 语文成绩 double english; // 英语成绩 double math; // 数学成绩 double computer; // 计算机成绩 public: Student() { id = -1; name = ""; chinese = 0.0; english = 0.0; math = 0.0; computer = 0.0; } Student(int id, string name, double chinese, double english, double math, double computer) { this->id = id; this->name = name; this->chinese = chinese; this->english = english; this->math = math; this->computer = computer; } int get_id() const { return id; } string get_name() const { return name; } void set_name(string name) { this->name = name; } double get_chinese() const { return chinese; } void set_chinese(double chinese) { this->chinese = chinese; } double get_english() const { return english; } void set_english(double english) { this->english = english; } double get_math() const { return math; } void set_math(double math) { this->math = math; } double get_computer() const { return computer; } void set_computer(double computer) { this->computer = computer; } // 获取总分 double get_sum() const { return (chinese + english + math + computer); } void show_info() { cout << id << "\t" << name << "\t" << chinese << "\t" << english << "\t" << math << "\t" << computer << "\t" << get_sum() << endl; } }; // 按语文分数排序 bool compare_chinese(const Student& stu1, const Student& stu2) { return (stu1.get_chinese() < stu2.get_chinese()); } // 按英语分数排序 bool compare_english(const Student& stu1, const Student& stu2) { return (stu1.get_english() < stu2.get_english()); } // 按数学分数排序 bool compare_math(const Student& stu1, const Student& stu2) { return (stu1.get_math() < stu2.get_math()); } // 按计算机分数排序 bool compare_computer(const Student& stu1, const Student& stu2) { return (stu1.get_computer() < stu2.get_computer()); } // 按总分排序 bool compare_sum(const Student& stu1, const Student& stu2) { return (stu1.get_sum() < stu2.get_sum()); } // 数据库类。包含增、删、改、查等操作 class Database { private: // 模拟数据库。学号作为主键 map<int, Student> students; public: // 增加一个学生 void add_student(Student student) { students.insert(pair<int, Student>(student.get_id(), student)); } // 删除一个学生 void delete_student(int id) { map<int, Student>::iterator iter; iter = students.find(id); students.erase(iter); } // 更改一个学生 void update_student(Student student) { map<int, Student>::iterator iter; iter = students.find(student.get_id()); if(iter != students.end()) { iter->second.set_name(student.get_name()); iter->second.set_chinese(student.get_chinese()); iter->second.set_english(student.get_english()); iter->second.set_math(student.get_math()); iter->second.set_computer(student.get_computer()); } else { cout << "This student does not exist in database, update failed." << endl; } } // 根据学号查询学生 Student select_student(int id) { map<int, Student>::iterator iter; iter = students.find(id); return Student(iter->second.get_id(), iter->second.get_name(), iter->second.get_chinese(), iter->second.get_english(), iter->second.get_math(), iter->second.get_computer()); } // 计算某门课程的平均成绩 double average(string course) { double sum = 0.0; for(map<int, Student>::iterator iter = students.begin(); iter != students.end(); ++iter) { if(course == "chinese") { sum += iter->second.get_chinese(); } if(course == "english") { sum += iter->second.get_english(); } if(course == "math") { sum += iter->second.get_math(); } if(course == "computer") { sum += iter->second.get_computer(); } } return sum / students.size(); } vector<Student> sortBy(string course) { vector<Student> vec; for(map<int, Student>::iterator iter = students.begin(); iter != students.end(); ++iter) { vec.push_back(iter->second); } if(course == "chinese") sort(vec.begin(), vec.end(), compare_chinese); if(course == "english") sort(vec.begin(), vec.end(), compare_english); if(course == "math") sort(vec.begin(), vec.end(), compare_math); if(course == "computer") sort(vec.begin(), vec.end(), compare_computer); return vec; } // 获取数据库中的所有学生 vector<Student> select_all() { vector<Student> vec; map<int, Student>::iterator iter; for(iter = students.begin(); iter != students.end(); ++iter) { vec.push_back(iter->second); } return vec; } }; int main(int argc, char* argv[]) { Student student1(1, "Andy", 100.0, 100.0, 100.0, 100.0); Student student2(2, "Bob", 99.0, 99.0, 99.0, 99.0); Student student3(3, "Chris", 98.0, 98.0, 98.0, 98.0); Database db; // 增加数据 db.add_student(student1); db.add_student(student2); db.add_student(student3); // 显示增加数据后,数据库中的所有数据记录 vector<Student> vec = db.select_all(); for(vector<Student>::iterator iter = vec.begin(); iter != vec.end(); ++iter) { iter->show_info(); } cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; // 删除student2 db.delete_student(student2.get_id()); // 显示删除数据后,数据库中的所有数据记录 vec = db.select_all(); for(vector<Student>::iterator iter = vec.begin(); iter != vec.end(); ++iter) { iter->show_info(); } cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; // 更改数据 Student update_student(1, "Andrew", 120.0, 120.0, 120.0, 120.0); db.update_student(update_student); // 显示更改数据后,数据库中的所有数据记录 vec = db.select_all(); for(vector<Student>::iterator iter = vec.begin(); iter != vec.end(); ++iter) { iter->show_info(); } cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; // 查询数据 Student student = db.select_student(1); // 显示查询结果 student.show_info(); cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; // 平均成绩 cout << "average of chinese:\t" << db.average("chinese") << endl; // 语文 cout << "average of english:\t" << db.average("english") << endl; // 英语 cout << "average of maths:\t" << db.average("math") << endl; // 数学 cout << "average of computer:\t" << db.average("computer") << endl; // 计算机 cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; // 俺总分排名 vector<Student> sorted_vec = db.sortBy(""); for(vector<Student>::iterator iter = sorted_vec.begin(); iter != sorted_vec.end(); ++iter) { iter->show_info(); } return 0; }