題目要求如下:
-----------------------------------------------------------------------------------------------------------------------------------------------
代碼如下:
運行結果:
/*
* Copyright (c) 2013, 煙臺大學計算機學院
* All rights reserved.
* 作 者: 沈遠宏
* 完成日期:2014 年06月18日
* 版 本 號:v1.0
* 問題描述:Description
建立一個對象數組,內放n(<10)個學生的數據(學號、成績),設立一個函數max,用指向對象的指針作函數參數,在max函數中找出n個學生中成績最高者,並輸出其學號。
Input
n和n個學生的學號、成績
Output
成績最高者的學號和成績
*/#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
using namespace std;
//主函數已給定如下,提交時不需要包含下述主函數
class Student
{
private:
string no;
double score;
public:
void input()
{
cin>>no>>score;
}
void display()
{
cout<<no<<" "<<score<<endl;
}
double get_score()
{
return score;
}
string get_no()
{
return no;
}
};
void max(Student* p,int n)
{
int index=0;
for(int j=0; j<n; ++j)
{
for(int i=0;i<n-j;++i)
{
if(p[i].get_score()>p[i+1].get_score())
{
index=i;
}
}
}
cout<<p[index].get_no()<<" "<<p[index].get_score()<<endl;
}
int main()
{
void max(Student* p,int);
const int NUM=10;
Student stud[NUM];
int n,i;
freopen("13.txt","r",stdin);
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要求結果輸出例樣: