* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.* 文件名称:
* 作 者: 于宸
* 完成日期:2012 年 3月21日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述: 设计一个学生类,包括学号(num)和成绩(score)。建立一个对象数组,内放5个学生的数据,要求:(1) 用指针指向数组首元素,输出第1、3、5个学生的信息;(2) 设计一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
* 问题描述:
* 程序输出:
* 程序头部的注释结束*/
#include<iostream>
using namespace std;
class Student
{
public:
Student(int n,float s):num(n),score(s){}
void display();
int num;
float score;
};
void Student::display()
{
cout<<num<<" "<<score<<endl;
}
void max(Student *a);
int main()
{
Student s[5]={
Student(001,90),
Student(002,98.5),
Student(003,98),
Student(004,92),
Student(005,100)
};
Student *p=s;
for(int i=0;i<5;i=i+2)
{
cout<<"第"<<i+1<<"个学生的学号和成绩";
s[i].display();
}
cout<<"最高成绩的学号成绩为:";
Student *q=&s[0];
max(q);
cout<<endl;
return 0;
}
void max(Student *a)
{
float max_score=a[0].score;
int k;
for(int i=0;i<5;i++)
{
if(a[i].score>max_score)
max_score=a[i].score;
k=i;
}
cout<<a[k].num<<" "<<max_score;
return;
}