HELP' 2019-03-22 18:38 采纳率: 100%
浏览 8751
已采纳

建立一个学生类,存放5个学生的数据(学号、姓名、成绩)

1.通过构造函数实现5个学生数据的初始化

2.设立一个input函数实现学生数据的输入

3.设立一个函数sort实现学生按成绩降序排序

4.设立一个函数MAX,在MAX中找出5个学生中成绩最高的,并输出学号

5.设立一个函数search,实现按姓名查找学生(考虑可能存在同名学生)

6.写出主函数对上述函数的调用

 跪求大佬指点!!!
  • 写回答

1条回答 默认 最新

  • threenewbee 2019-03-22 18:59
    关注

    采纳后可以帮你完善sort和search的代码

    #include <iostream>
    #include <string>
    using namespace std;
    class Student
    {
    public:
        Student(string n,float s):number(n),score(s){}
        friend void max(Student *);  //声明友元函数
    private:
        string number; //将学号声明为字符串
        float score;
    };
    void max(Student *p)
    {
        int i;
        for(i=0;i<5;i++)
        {
            if(p->score<(p+i)->score)
            {
                p=(p+1);  //将指向较大值的指针赋给指向较小值的指针
            }
        }
        cout<<"最高成绩为:"<<p->score<<endl;
        cout<<"学生学号为:"<<p->number;
    }
    
    
    int main()
    {
        Student Stud[5]={
        Student("201024131101",99),
        Student("201024131102",92),
        Student("201024131103",99.5),
        Student("201024131104",95),
        Student("201024131105",93)
        };  //定义一个对象数组数组并初始化对象
        Student *p=Stud;   //定义一个指向对象的指针
        max(p);   //调用函数
        return 0;
    }
    

    https://blog.csdn.net/xiao_song_shu/article/details/69252661

    // Q753145.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    class Student
    {
    public:
        Student(string n, string na, float s):number(n),score(s),name(na) {} //构造函数
        void max(Student *);
        void sort(Student *, int);
        void search(Student *, string, int);
        void display()
        {
            cout << name << " " << number << " " << score << endl;
        }
    private:
        string number; //将学号声明为字符串
        string name;
        float score;
    };
    void Student::max(Student *p)
    {
        int i;
        for(i=0;i<5;i++)
        {
            if(p->score<(p+i)->score)
            {
                p=(p+1);  //将指向较大值的指针赋给指向较小值的指针
            }
        }
        cout<<"最高成绩为:"<<p->score<<endl;
        cout<<"学生姓名为:"<<p->name<<endl;
        cout<<"学生学号为:"<<p->number << endl;
    }
    
    void Student::sort(Student *p, int n)
    {
        for (int i = 0; i < n - 1; i++)
            for (int j = 0; j < n - i - 1; j++)
            {
                if (p[j].score < p[j + 1].score)
                {
                    string tn = p[j].number;
                    string tna = p[j].name;
                    float ts = p[j].score;
                    p[j].number = p[j + 1].number;
                    p[j].name = p[j + 1].name;
                    p[j].score = p[j + 1].score;
                    p[j + 1].number = tn;
                    p[j + 1].name = tna;
                    p[j + 1].score = ts;
                }
            }
    }
    
    void Student::search(Student *p, string name, int n)
    {
        int found = 0;
        for (int i = 0; i < n - 1; i++)
        {
            if (p[i].name == name) 
            {
                found = 1;
                p[i].display();
            }
        }
        if (!found) cout << "no result" << endl;
    }
    
    int main()
    {
        Student Stud[5]={
        Student("201024131101", "a",99),
        Student("201024131102", "b",92),
        Student("201024131103", "b",99.5),
        Student("201024131104", "c",95),
        Student("201024131105", "cc",93)
        };  //定义一个对象数组数组并初始化对象
        Student *p=Stud;   //定义一个指向对象的指针
        p->max(p);   //调用函数
        cout << "search b:" << endl;
        p->search(p, "b", 5);
        cout << "sort:" << endl;
        p->sort(p, 5);
        for (int i = 0; i < 5; i++)
            p[i].display();
        return 0;
    }
    
    

    最高成绩为:99.5
    学生姓名为:b
    学生学号为:201024131103
    search b:
    b 201024131102 92
    b 201024131103 99.5
    sort:
    b 201024131103 99.5
    a 201024131101 99
    c 201024131104 95
    cc 201024131105 93
    b 201024131102 92
    Press any key to continue . . .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误