11、对象数组和对象指针

问题描述 :

要求:在给定的Student类的基础上建立一个对象数组,内放n个学生的数据(学号,分数)。请设计一个用户函数max,用指向对象的指针作为函数参数。max函数的功能是找出这n个学生中成绩最高者(假定无相同的成绩),并输出其学号。

 

Student类如下:

class Student{ 

   public:

      void Set_StuNum(int); //自行设计

      int Get_StudNum();  //自行设计

      void Set_Score(float); //自行设计

      float Get_Score();  //自行设计

   private:   

      int stu_num; //学号

      float score; //分数   

};

 

max函数原型为:

void max(Student *, int, int &, float &); //5个形参的含义分别为:对象指针,学生数,最高分的学生学号,最高分

 

要求使用以下main函数测试:

int main(){ 

  int n, stu_num, max_stu_num;

  float score, max_score;

  cin>>n;

 

  Student stu[n];

  for( int i = 0; i < n; i++ ){

    cin>>stu_num>>score; 

    stu[i].Set_StuNum(stu_num);

    stu[i].Set_Score(score);

  }

 

  max(stu, n, max_stu_num, max_score);

 

  cout<<max_stu_num<<" "<<max_score;  

  return 1;

}

 

输入说明 :

第一行:学生数目n。

第二行至第n+1行:每行输入:学号 分数。 二者之间以空格分隔,行与行之间无空行。

 

输出说明 :

最高分的学生学号 分数。二者之间以空格分隔

输入范例 :

5
1001 70.5
1002 78
1003 68
1004 82.5
1005 81

输出范例 :

1004 82.5

解题代码: 

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
class Student{
   public:
      Student(void);
      void Set_StuNum(int); //自行设计
      int Get_StudNum();  //自行设计
      void Set_Score(float); //自行设计
      float Get_Score();  //自行设计
   private:
      int stu_num; //学号
      float score; //分数
};
Student::Student(void)
{
    ;
}
void Student::Set_StuNum(int num)
{
    stu_num=num;
}
int Student::Get_StudNum()
{
    return stu_num;
}
void Student::Set_Score(float num)
{
    score=num;
}
float Student::Get_Score()
{
    return score;
}
void max(Student *star, int n, int &num, float &sroce)
{
    Student *i;
    float huge=-10.0;
    while(n--)
    {
        if((*star).Get_Score()>huge)
        {
            sroce=(*star).Get_Score();
            huge=(*star).Get_Score();
            num=(*star).Get_StudNum();
        }
        star++;
    }
    return ;
}
int main(){
  int n, stu_num, max_stu_num;
  float score, max_score;
  cin>>n;
  Student stu[n];
  for( int i = 0; i < n; i++ ){
    cin>>stu_num>>score;
    stu[i].Set_StuNum(stu_num);
    stu[i].Set_Score(score);
  }
  max(stu, n, max_stu_num, max_score);
  cout<<max_stu_num<<" "<<max_score;
  return 0;
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值