c语言指针指向一个结构体指针数组,一个有关指向结构体数组的指针的问题

你的main()函数不够简洁,我增加了计算平均分并按成绩排序输出前5名的功能,

我修改后是:

#include

#define SIZE 5

struct stud_score

{

long num;

char name[10];

double score[3];

double average;

};

void input(struct stud_score *,int);

void sort(struct stud_score *,int);

void output(struct stud_score *);

void main()

{

struct stud_score student[SIZE];

input(student,SIZE);

sort(student,SIZE);

output(student);

}

void input(struct stud_score *student,int size)

{

int i;

printf("请按以下格式输入:\n\n"

"学号\t姓名\t分数1\t分数2\t分数3 \n");

for(i=0;i

{

printf("请输入第%d组数据\n>",i+1);

scanf(" %d %s %lf %lf %lf",

&student[i].num,&student[i].name,

&student[i].score[0],&student[i].score[1],&student[i].score[2]);

student[i].average=(student[i].score[0]+student[i].score[1]+student[i].score[2])/3;

}

}

void sort(struct stud_score *student,int size)

{

int i,j;

struct stud_score temp;

for(i=0;i

for(j=i+1;j

{

if(student[i].average

{

temp=student[i];

student[i]=student[j];

student[j]=temp;

}

}

}

void output(struct stud_score *student)

{

int i;

printf("成绩前五是:\n");

printf("学号\t姓名\t分数1\t分数2\t分数3\t平均成绩\n");

for(i=0;i<5;i++)

printf(" %d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n",

student[i].num, student[i].name,

student[i].score[0], student[i].score[1], student[i].score[2],

student[i].average);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值