/*
Description
建立一个对象数组,内放n(<10)个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出n个学生中成绩最高者,并输出其学号。
Input
n和n个学生的学号、成绩
Output
成绩最高者的学号和成绩
*/
#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
public:
Student(){}
Student(double n,double g):NUM(n),grade(g) {}
void input();
void show();
double get1()
{
return NUM;
}
double get2()
{
return grade;
}
private:
int NUM;
double grade;
};
void Student::show()
{
cout<<NUM<<" "<<grade<<endl;
}
void Student::input()
{
cin>>NUM>>grade;
}
void max(Student*s,int n)
{
Student max=*s;
for(int i=0;i<n;i++)
{
if(max.get2()<(s+i)->get2())
{
max=*(s+i);
}
}
max.show();
}
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;
}
OJ 1.M
最新推荐文章于 2024-12-18 20:31:25 发布