C++编程习题 在线=

来源:百度知道 编辑:UC知道 时间:2024/06/13 22:47:06
建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号.(见谭浩强-《C++面向对象程序设计》P117习题5.)
要求1.请用C++标准格式写.<#include<iostream>....
2.按照要求用对象数组及用对象指针作参数.
3.调试成功.
(谢谢各位关注,满意答案可追加分数.)

#include <iostream>
using namespace std;

class student
{
public:
student(int n,int s):num(n),score(s){}
void max(student*);
private:
int num;
int score;
};

void student::max(student* p)
{
int max=p[0].score;
int temp;
int n;
for(int i=1;i<5;i++)
{
if((p[i].score)>max)
{
temp=p[i].score;p[i].score=max;max=temp;
n=i;
}
}
cout<<"成绩最高者"<<max<<endl;
cout<<"其学号为:"<<p[n].num<<endl;
}

int main()
{
student A(0,0);
student stu[5]={student(1,88),
student(2,89),
student(3,99),
student(4,86),
student(5,100)
};//初始化对象数组
A.max(stu);
return 0;
}

#include "stdio.h"
typedef struct
{
int score;
int num;
}Student;
int max(Student S[])
{