/*建立一个对象数组,内放n(n<10)个学生的数据(学号、成绩),
用指针指向数组首元素,在max函数中找出n个学生中成绩最高者,并输出其学号。
*/
#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
public:
void input();
int get_num()
{
return num;
}
double get_score()
{
return score;
}
void max(Student*,int);
private:
int num;
double score;
};
void Student::input()
{
cin>>num>>score;
}
void max(Student *p,int n)
{
Student *a;
a=p;
int i;
for(i=0; i<n; i++)
{
if(p->get_score()<a->get_score())
p=a;
a++;
}
cout<<p->get_num()<<" "<<p->get_score()<<endl;
}
int main()
{
void max(Student* ,int);
const int NUM=10;
Student stud[NUM];
int n,i;
cin>>n;
for(i=0; i<n; i++)
stud[i].input();
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Student *p=&stud[0];
max(p,n);
return 0;
}
感悟:指针是我无法愈合的伤!!!!!