#include <iostream>
#include <list>
#include <string>
using namespace std;
struct Student
{
string itsId;
string itsName;
double itsScore1;
double itsScore2;
double itsScore3;
double average;
double getAverage(){average=(itsScore1+itsScore2+itsScore3)/3;return average;};
bool operator < (const Student& itemToCompare)const
{
return (this->average>itemToCompare.average);
}
};
int main()
{
list<Student> listStudent(10);
cout<<"请输入10位学生的数据,学号,姓名,三科成绩"<<endl;
list<Student>::iterator iElement;
for (iElement=listStudent.begin();iElement!=listStudent.end();iElement++)
{
cin>>iElement->itsId>>iElement->itsName>>iElement->itsScore1>>iElement->itsScore2>>iElement->itsScore3;
}
cout<<endl<<endl;
for (iElement=listStudent.begin();iElement!=listStudent.end();iElement++)
{
cout<<"学生: "<<iElement->itsName<<"\t平均成绩: "<<iElement->getAverage();
cout<<endl;
}
cout<<endl<<endl;
listStudent.sort();
iElement=listStudent.begin();
cout<<"最高分学生: "<<iElement->itsId<<"
"<<iElement->itsName
<<"
"<<iElement->itsScore1<<"
"<<iElement->itsScore2
<<"
"<<iElement->itsScore3;
cout<<endl;
return 0;
}
感觉自己写的不太好,将就看吧,楼主