Encapsulates a structure that contains a private array to store students' grades and a private variable to record the number of students.
Provide a public member function, void setNum(int num) is used to set the number of students
Provide a public member function: void input(), used to input the scores of all students
Provide a public member function: void sort(), which is used to sort the stored student scores in descending order.
Provide a public member function: void show(), used to display the scores of all students
In the main program, complete the call of relevant functions
#include <iostream>
using namespace std;
struct Stu
{
private:
int *p = new int[128];
private:
int num;
public:
void setNum(int var)//设置学生人数
{
num = var;
}
public:
void intput()//插入函数
{
for(int i=0;i<num;i++)
{
cin>>p[i];
}
}
public:
void show()//输出函数
{
for(int i=0;i<num;i++)
{
cout<<p[i]<<" ";
}
}
public:
void sort()//排序函数(降序)
{
int temp;
for(int i=1;i<num;i++)
{
for(int j=0;j<num-i;j++)
{
if(p[j]<p[j+1])
{
temp = p[j+1];
p[j+1] = p[j];
p[j]=temp;
}
}
}
}
};
int main()
{
Stu s;
int num;
cout<<"请输入学生个数:";
cin>>num;
s.setNum(num);
s.intput();
s.sort();
s.show();
return 0;
}