代码如下,如有帮助,请帮忙采纳一下,谢谢。
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int id;
string name;
int score[3];
float avg;
};
void input(struct Student st[],int n)
{
int i;
for (i=0;i<n;i++)
{
cout << "请输入学生"<<i+1<<"的学号、姓名、三门课成绩:";
cin >> st[i].id >> st[i].name >> st[i].score[0] >> st[i].score[1] >> st[i].score[2] ;
}
}
void average1(struct Student st[],int n)
{
for (int i = 0;i<n;i++)
{
st[i].avg = (st[i].score[0] + st[i].score[1]+st[i].score[2])/3.0;
}
}
void average2(struct Student st[],int n,float avg[])
{
for (int i = 0;i<3;i++)
{
avg[i] = 0;
for (int j = 0;j<n;j++)
{
avg[i] += st[j].score[i];
}
avg[i] = avg[i]/n;
}
}
void sort(struct Student st[],int n)
{
struct Student tt;
for (int i = 0;i<n-1;i++)
{
for (int j=0;j<n-1-i;j++)
{
if (st[j].avg > st[j+1].avg)
{
tt = st[j];
st[j] = st[j+1];
st[j+1] = tt;
}
}
}
}
void output(struct Student st[],int n,float avg[])
{
//显示每门课的成绩
cout << "学生成绩:"<< endl;
for (int i = 0;i<n;i++)
{
cout << st[i].score[0] <<" "<< st[i].score[1] << " " << st[i].score[2] << " " << st[i].avg <<endl;
}
cout << "每门课的平均分:";
cout << avg[0] << " " << avg[1] << " " << avg[2] <<endl;
}
int main()
{
struct Student st[10];
float avg[3];
input(st,10);
average1(st,10);
average2(st,10,avg);
sort(st,10);
output(st,10,avg);
return 0;
}