您的位置:首页 > 其它

OJ对象数组求最大值

2015-06-15 14:33 323 查看


代码:

#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
public:
Student(int n=0,double g=0);
void input();
friend void max(Student*p,int n);
private:
int  num;
double grade;
};
Student::Student(int n,double g)
{
num=n;
grade=g;
}
void Student::input()
{
cin>>num>>grade;
}
void max(Student*p,int n)
{
int i,max,value;
max=p[0].grade;
for(i=1;i<n;i++)
{
if(max<p[i].grade)
{
max=p[i].grade;
value=i;
}
}
cout<<p[value].num<<" "<<p[value].grade<<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;

}


运行结果:



知识点总结:

友元函数的应用

学习心得:

开始写的时候忘记要用友元函数了,按照自己的想法写完之后,一运行出现好几个错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: