Keep_OP
HELP'
2019-03-22 18:38
采纳率: 100%
浏览 5.8k

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

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

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

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

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

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

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

 跪求大佬指点!!!
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • caozhy
    已采纳

    采纳后可以帮你完善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 . . .

    点赞 评论

相关推荐