建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
#include<bits/stdc++.h>
using namespace std;
class Student
{
public:
Student(){}
Student(string i,double s):id(i),score(s){}
string id;
double score;
};
void max(Student *p)
{
Student *t,*s;
double max;
for (t = p , s = t, max = t->score; t < (p + 5); t++)
{
if (t->score > max)
{
max = t->score;
s = t;
}
}
cout << "成绩最高者id为:" <<s->id<<" "<<max<<"分"<< endl;
}
int main()
{
Student s[5];
for(int i=0;i<5;i++)
{
cin>>s[i].id>>s[i].score;
}
max(s);
return 0;
}