求5名学生的最高的分者的学号和姓名 指针做函数参数

#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
    string num;
    string name;
    float sc;
    Student()
    {
        num = "101";
        name = "张三";
        sc = 100.0;
    }
    Student(string n, string nam, float sc1) :num(n), name(nam), sc(sc1) {}
    //插入数据函数
    void update(int i);
    void display();
};
void Student::update(int i)
{
    cout << "请输入第" << i + 1 << "名学生的学号、姓名、成绩" << endl;
    cout << "学号:";
    cin >> num;
    cout << "姓名:";
    cin >> name;
    cout << "成绩:";
    cin >> sc;
    cout << "\n";
}

void Student::display()
{
    cout << "成绩最好者的学号:" << num <<
        "\t" << "成绩最好者的姓名:" << name << endl;
}
//找到最高成绩的下标
int max1(const Student* p0)//常指针
{
    float max = p0->sc;
    int i;
    int ans;
    for (i = 0; i < 5; i++)
    {
        if (max < p0->sc)
        {
            max = p0->sc;
            ans = i;
        }
        p0++;
    }
    //ans= i;
    return ans;
}
int main()
{
    int i;
    //int best;
    Student stu[5];
    //输入
    for (i = 0; i < 5; i++)
    {
        stu[i].update(i);
    }
    //输出结果
    stu[max1(&stu[0])].display();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值