发布网友 发布时间:2022-04-27 07:51
共2个回答
热心网友 时间:2023-09-14 01:36
排序你可以用最简单的冒泡或者选择排序,以平均分为标准
#include<stdio.h>
typedef struct student
{
int num;
char name[20];
int Score1;
int Score2;
int Score3;
float average;
}student;
student st[3];
void CreateStudent(student st[])
{
int i,j;
float Average;
for(i=0;i<3;i++)
{
printf("请输入学生%d的资料:\n",i+1);
printf("学号为:");
scanf("%d",&st[i].num);
printf("姓名是:");
scanf("%s",&st[i].name);
printf("第1门成绩是:");
scanf("%d",&st[i].Score1);
printf("第2门成绩是:");
scanf("%d",&st[i].Score2);
printf("第3门成绩是:");
scanf("%d",&st[i].Score3);
printf("\n");
st[i].average = (st[i].Score1+st[i].Score2+st[i].Score3)/3;
}
}
void SortStudent(student st[],int nLength)
{
int i, j, max;
student temp;
for(i=0; i<nLength;i++)
{
max = i;
for(j=i+1; j<nLength; j++)
{
if(st[j].average > st[max].average)
max = j;
}
if(max != i)
{
temp = st[i];
st[i] = st[max];
st[max] = temp;
}
}
}
int main()
{
int j;
CreateStudent(st);
SortStudent(st,3);
printf("学号\t姓名\t语文\t数学\t英语\t平均分\n");
for(j=0;j<3;j++)
{
printf("%d",st[j].num);
printf("\t%s",st[j].name);
printf("\t%d",st[j].Score1);
printf("\t%d",st[j].Score2);
printf("\t%d",st[j].Score3);
printf("\t%2f",st[j].average);
printf("\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
}
}
热心网友 时间:2023-09-14 01:37
#include<stdio.h>
struct student
{
int num;
char name[20];
int Score1;
int Score2;
int Score3;
}student[2];
int main()
{
int i,j;
int Average;
for(i=0;i<3;i++)
{
printf("请输入学生%d的资料:\n",i+1);
printf("学号为:");
scanf("%d",&student[i].num);
printf("姓名是:");
scanf("%s",&student[i].name);
printf("第1门成绩是:");
scanf("%d",&student[i].Score1);
printf("第2门成绩是:");
scanf("%d",&student[i].Score2);
printf("第3门成绩是:");
scanf("%d",&student[i].Score3);
printf("\n\n");
}
printf("学号\t姓名\t语文\t数学\t英语\t平均分\n");
for(j=0;j<3;j++)
{
Average=(student[j].Score1+student[j].Score2+student[j].Score3)/3;
printf("%d",student[j].num);
printf("\t%s",student[j].name);
printf("\t%d",student[j].Score1);
printf("\t%d",student[j].Score2);
printf("\t%d",student[j].Score3);
printf("\t%d",Average);
printf("\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
}
}