您的位置:首页 > 其它

oj刷题第一批E 对象数组求最大值

2015-06-17 20:43 267 查看
#include<iostream>
#include<iomanip>
using namespace std;
class Student
{
public:
Student(int num=0,double sco=0):number(num),score(sco){}
void input();
friend void max(Student*p,int n);
private:
int number;
double score;
};
void Student::input()
{
cin>>number>>score;
}
void max(Student*p,int n)
{
int i,k=0;
double max_score;
max_score=p[0].score;
for(i=0;i<10;i++)
{
if(p[i].score>max_score)
{
max_score=p[i].score;
k=i;
}
}
cout<<p[k].number<<" "<<p[k].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;
}
测试结果:
<img src="https://img-blog.csdn.net/20150617204800975?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFpZHVfMjE2OTg4MTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</pre><pre name="code" class="cpp">总结:真是经过了太长时间的脱轨才把c++的学习调整到正轨上来,一定要保持下去。也正因为进过了太长时间的“放羊”,我在做这个题目时完全不知道还要用友元函数来声明max函数,使它作为一个完全属于类外的函数能够调用类内的私有数据。明白了这一点之后,debug时出现的"number is private"就顺理成章了;我是参考了一个同学的博客才能完成这个题目的,由此对这种展示学习成果的形式产生了很多感激之情,不仅有助于自己的学习,还大大方便了同学的学习,尤其是不太努力的同学想要努力的时候,更是获益匪浅。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: