~cpp
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
#define SUBJECT_SIZE 4
struct Student_info {
string name;
vector<int> subjects;
int total;
double average;
};
double Sum(Student_info &s);
void totalSum(vector<Student_info> &students);
double average(Student_info &s);
void totalAverage(vector<Student_info> &students);
bool totalCompare(const Student_info &s1, const Student_info &s2);
void sortBySum(vector<Student_info> &students);
ostream& displayScores(ostream &out, const vector<Student_info> &students);
istream& readScores(istream &in, ifstream &fin, vector<Student_info> &students);
int main()
{
ifstream fin("score.txt");
vector<Student_info> students;
readScores(cin,fin,students);
totalSum(students);
totalAverage(students);
sortBySum(students);
displayScores(cout,students);
return 0;
}
double Sum(Student_info &s)
{
s.total=accumulate(s.subjects.begin()+(s.subjects.size()-4),s.subjects.end(),0);
return s.total;
}
void totalSum(vector<Student_info> &students)
{
vector<int> sum;
transform(students.begin(),students.end(),back_inserter(sum),Sum);
}
double average(Student_info &s)
{
return s.average=Sum(s)/SUBJECT_SIZE;
}
void totalAverage(vector<Student_info> &students)
{
vector<double> averageScore;
transform(students.begin(),students.end(),back_inserter(averageScore),average);
}
void sortBySum(vector<Student_info> &students)
{
sort(students.begin(),students.end(),totalCompare);
}
bool totalCompare(const Student_info &s1, const Student_info &s2)
{
return s1.total > s2.total;
}
ostream& displayScores(ostream &out, const vector<Student_info> &students)
{
for(vector<Student_info>::const_iterator it = students.begin() ; it!=students.end();++it)
out<<it->name<<" - totalSum : "<<it->total<<" average : "<<it->average<<"\n";
return out;
}
istream& readScores(istream &in, ifstream &fin, vector<Student_info> &students)
{
string name, temp;
int scoreTemp;
Student_info student;
vector<string> subject;
fin>> name;
while(fin>>temp && subject.size()!=SUBJECT_SIZE)
subject.push_back(temp);
do
{
student.name = temp;
for(vector<string>::const_iterator it = subject.begin() ;
it!=subject.end() && fin>>scoreTemp;++it)
student.subjects.push_back(scoreTemp);
students.push_back(student);
}while(fin>>temp);
return in;
}