//Sucessfully tested in Dev-C++ under ISO C++11 #include #include #include #include #include #include #include #include //functions as parameters using namespace std; struct StudentInfo { int id, schoolYear; string name, sex, birthday, birthplace; }; struct CourseInfo { int id, score; double credits; string course; }; set studentIds; map studentMap; multimap courseMap; void LoadStudentInfo(const string& filename) //get student information { ifstream file(filename); if(!file.is_open()) { cerr<<"Error opening file: "<>info.id>>info.name>>info.sex>>info.birthday>>info.schoolYear>>info.birthplace; studentMap[info.id] = info; } file.close(); } void LoadCourseInfo(const string& filename) //get course information { ifstream file(filename); if(!file.is_open()) { cerr<<"Error opening file: "<>info.id; string courseName; while(ss>>courseName) { if(!isalpha(courseName[0])) break; info.course += " " + courseName; //course name includes space } info.course.erase(0, 1); info.credits = stod(courseName); ss>>info.score; courseMap.insert({info.id, info}); } file.close(); } void PrintStudentCourses(int studentId, ostream& os) //"os" can be "cout" or "fout" { if(studentMap.find(studentId) != studentMap.end()) { const auto& student=studentMap[studentId]; os<<"Student ID: "<second.course<second.credits<< endl<<"\tScore: "<second.score<> students(studentMap.begin(), studentMap.end()); sort(students.begin(), students.end(), [](const pair& a, const pair& b) {return a.second.name < b.second.name;}); for(const auto& student: students) PrintStudentCourses(student.first, os); } void SortByTotalScore(ostream& os) { vector> students; //contain students' information for(const auto& pair1: studentMap) { int allScore=0; for(const auto& pair2: courseMap) //calculate all scores of each student for comparison if(pair2.first == pair1.first) allScore += pair2.second.score; students.push_back(make_pair(allScore, pair1.second)); } sort(students.begin(), students.end(), [](const pair& a, const pair& b) {return a.first > b.first;}); for(const auto& student: students) PrintStudentCourses(student.second.id, os); } void SortByScore(const string& courseName, ostream& os) //declare target course { vector> students; for(const auto& pair1: studentMap) { int oneScore=0; for(const auto& pair2: courseMap) if(pair2.first == pair1.first && pair2.second.course == courseName) oneScore += pair2.second.score; students.push_back(make_pair(oneScore, pair1.second)); } sort(students.begin(), students.end(), [](const pair& a, const pair& b) {return a.first > b.first;}); for(const auto& student: students) PrintStudentCourses(student.second.id, os); } void QueryByScoreRange(const string& courseName, int low, int high, ostream& os, int judge=0) //declare lower-bound and upper-bound { studentIds.clear(); for(const auto& pair: courseMap) if(pair.second.course == courseName && pair.second.score >= low && pair.second.score <= high) studentIds.insert(pair.first); if(judge) return; for(int id: studentIds) if(studentMap[id].schoolYear == 2020) PrintStudentCourses(id, os); } void QueryBySex(const string& sex, ostream& os, int judge=0) { studentIds.clear(); for(const auto& pair: studentMap) if(pair.second.sex == sex) studentIds.insert(pair.first); if(judge) return; for(int id: studentIds) PrintStudentCourses(id, os); } void CalculateAverage(double low, ostream& os, int judge=0) { studentIds.clear(); for(const auto& pair1: studentMap) { double averageValue=0.0; int averageCount=0; for(const auto& pair2: courseMap) { if(pair2.first == pair1.first) { averageValue += pair2.second.score * 1.0; ++averageCount; } } if(averageValue / averageCount >= low) studentIds.insert(pair1.first); } if(judge) return; if(!studentIds.empty()) for(int id: studentIds) PrintStudentCourses(id, os); else os<<"No Student Qualified."< studentIds; for(const auto& pair1: studentMap) { int allCredit=0; for(const auto& pair2: courseMap) if(pair2.first == pair1.first) allCredit += pair2.second.credits; if(allCredit >= low) studentIds.insert(pair1.first); } if(!studentIds.empty()) for(int id: studentIds) PrintStudentCourses(id, os); else os<<"No Student Qualified."< condition, ostream& os) //customized condition function { vector toDelete; for(const auto& pair: courseMap) { const auto& student=studentMap[pair.first]; const auto& course=pair.second; if(condition(student, course)) toDelete.push_back(pair.first); } for(int id: toDelete) { studentMap.erase(id); courseMap.erase(id); } if(!studentMap.empty()) for(const auto& pair: studentMap) PrintStudentCourses(pair.first, os); } void Test() //write in file { ofstream resultFile("StudenInfo_StudentCourse_result.txt"); cout< StudentInfo_StudentCourse_result.txt"<= 80.0)"<= 20)"<