/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:acm 对象数组求最大值
*作者:刘中林
*完成日期:2014 年 6 月 23 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*问题描述:建立一个对象数组,内放n(<10)个学生的数据(学号、成绩),
设立一个函数max,用指向对象的指针作函数参数,
在max函数中找出n个学生中成绩最高者,并输出其学号。
*程序输入:
*程序输出:
*问题分析:
*算法设计:
*/
#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
public:
Student() {}
void input()
{
cin>>num>>score;
}
void display()
{
cout<<num<<" "<<score<<endl;
}
int get_num()
{
return num;
}
double get_score()
{
return score;
}
private:
int num;
double score;
};
void max(Student*p ,int n)
{
Student t;
for(int j=0; j<n-1; j++)
for(int i=0; i<n-1-j; i++)
{
if((p+i)->get_score()>(p+i+1)->get_score())
{
t=*(p+i);
*(p+i)=*(p+i+1);
*(p+i+1)=t;
}
}
(p+n-1)->display();
}
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;
}
*样例输出:
*心得体会:特色。。