【问题描述】建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数,首部为:void max(Student *arr),用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号和成绩。请用构造函数实现初始化。
【输入形式】根据系统提示,分别输入5位学生的学号、成绩,然后用构造函数实现初始化。
【输出形式】输出5个学生中成绩最高者的学号和成绩。
【样例输入】
10001 66.3
10003 77.5
10008 69.6
10011 80
10016 89
【样例输出】
Please enter the number and score for each of the 5 students:
10016 89
【样例说明】根据系统提示,分别输入5位学生的学号、成绩,然后用构造函数实现初始化。输出5个学生中成绩最高者的学号和成绩。
【评分标准】 结果完全正确得20分,每个测试点10分。提交程序名为:xt9-5.c或xt9-5.cpp
要是你们的答案用这个提交通过了,记得点个赞加个关注再走!!!以后会继续更新C++练习的!
#include<iostream>
#include<string>
using namespace std;
int num;
double score;
class Student
{
public:
int s_num;
double s_score;
Student()
{
s_num=0;
s_score=0;
}
public:
void setStudent()
{
cin>>num>>score;
s_num=num;
s_score=score;
}
};
int main()
{
int i=0;
double j=0;
cout<<"Please enter the number and score for each of the 5 students:";
cout<<endl;
Student s1;
s1.setStudent();
Student s2;
s2.setStudent();
if(s1.s_score>=s2.s_score)
{
i=s1.s_num;j=s1.s_score;}
else i=s2.s_num;j=s2.s_score;
Student s3;
s3.setStudent();
if(s3.s_score>=j)
{
i=s3.s_num;j=s3.s_score;}
Student s4;
s4.setStudent();
if(s4.s_score>=j)
{
i=s4.s_num;j=s4.s_score;}
//cout<<j<<endl;
Student s5;
s5.setStudent();
if(s5.s_score>=j)
{
i=s5.s_num;j=s5.s_score;}
cout<<i<<" "<<j;
return 0;
}
我不知道void max(Student *arr)怎么写,就用这个凑合下吧,交作业最重要,以后想到了再来改~