c++程序设计第3版谭浩强课后答案
C++编程:建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
以下是此题的【c++源代码】
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
int num;
float score;
};
void main()
{Student stud[5]={
Student(101,78.5),Student(102,85.5),Student(103,98.5),
Student(104,100.0),Student(105,95.5)};
void max(Student* );
Student *p=&stud[0];
max(p);
}
void max(Student *arr)
{float max_score=arr[0].score;
int k=0;
for(int i=1;i<5;i++)
if(arr[i].score>max_score) {max_score=arr[i].score;k=i;}
cout<<arr[k].num<<" "<<max_score<<endl;
}
本文链接:http://www.wb98.com/cjia/post/cjia_9.5.html
本站文章搜索: