版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Fiverya/article/details/88863124
编写一个程序,功能是对若干个同学的某门课程成绩进行排序。
要求定义一个学生类,类中私有成员有学生的编号及成绩;成员函数有建立数据、输入数据函数;编写一个友元函数能完成对成绩由高到低排序。
从键盘输入学生的个数、学号及一门课程的成绩,调用排序程序后,将排序后的结果输出。输入、输出格式如下:
输入:
3
1001 90
1002 80
1003 85
输出:
学号:1001 成绩:90
学号:1003 成绩:85
扫描二维码关注公众号,回复:
5689601 查看本文章
学号:1002 成绩:80
#include<iostream>
using namespace std;
class Student
{
public:
int a,b[10][2];
void getscore();
void putscore();
friend Student paixu(Student student);
};
void Student::getscore()
{
cin>>a;
for(int i=0;i<a;i++)
{
cin>>b[i][0]>>b[i][1];
}
}
void Student::putscore()
{
for(int i=0;i<a;i++)
{
cout<<"学号:"<<b[i][0]<<" 成绩:"<<b[i][1]<<endl;
}
}
Student paixu(Student student)
{ int c[1][2];
for(int i=0;i<student.a;i++)
{
for(int j=0;j<student.a-1;j++)
{
if(student.b[j][1]<student.b[j+1][1])
{c[0][0]=student.b[j][0];
c[0][1]=student.b[j][1];
student.b[j][0]=student.b[j+1][0];
student.b[j][1]=student.b[j+1][1];
student.b[j+1][0]=c[0][0];
student.b[j+1][1]=c[0][1];
}
}
}
return student;
}
int main()
{
Student student,s2;
student.getscore();
s2=paixu(student);
s2.putscore();
return 0;
}