Operation
1> Encapsulate a structure. The structure 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 stu_count;
public:
//设置学生个数函数
void setNum(int num)
{
stu_count=num;
cout<<"学生人数是:"<<stu_count<<endl;
}
public:
//输入学生成绩函数
void input()
{
for(int i=0;i<stu_count;i++)
{
cout<<"请输入学生成绩:"<<endl;
cin>>p[i];
}
}
public:
//学生成绩降序排序函数
void sort()
{
int temp;
for(int i=1;i<stu_count;i++)
{
for(int j=0;j<stu_count-i;j++)
{
if(p[j]<p[j+1])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
public:
//输出学生成绩函数
void show()
{
cout<<"学生成绩是:";
for(int i=0;i<stu_count;i++)
{
cout<<p[i]<<" ";
}
cout<<endl;
}
};
int main()
{
struct Stu stu;
int num;
cout<<"请输入学生个数:";
cin>>num;
stu.setNum(num);
stu.input();
stu.sort();
stu.show();
return 0;
}
2> Mind map