C语言基础:求结构体数组成员列表中的成绩最大值并按序输出
1. 用结构体数组进行传参
例1:
#include"stdafx.h"#include<stdio.h>#definemember5//成员数#defineNAME_LEN20//姓名最大长度structst{intid;charname[NAME_LEN];doublescore;};intmax_score_id(structstst[],intsize)//结构体数组作为形参{inti,j;structsttemp;//定义结构体类型变量tempfor(i=0;i<=size;i++){for(j=i+1;j<=size;j++){if(st[i].score<st[j].score){temp=st[i];st[i]=st[j];st[j]=temp;}}}return0;}intmain(void){intindex;structststudents[]={{101,"zhang3",88.8},{102,"li4",99.9},{103,"wang5",66.6},{104,"shun6",66.8},{105,"yang7",88.6}};index=max_score_id(students,member);//结构体数组进行传参printf("Sno\tSname\tScore\n");for(index=0;index<member;index++)printf("%d\t%s\t%.2f\n",students[index].id,students[index].name,students[index].score);return0;}2. 用结构体指针进行传参
例2:
#include"stdafx.h"#include<stdio.h>#definemember5//成员数#defineNAME_LEN20//姓名最大长度structst{intid;charname[NAME_LEN];doublescore;}*p;intmax_score_id(structst*p,intsize){//结构体指针作为形参structstmax;//定义结构体类型变量maxfor(inti=0;i<size;i++){for(intj=i+1;j<size;j++){if((p+i)->score<(p+j)->score){max=*(p+i);*(p+i)=*(p+j);*(p+j)=max;}}}return0;}intmain(void){intindex;structststudents[]={{101,"zhang3",88.8},{102,"li4",99.9},{103,"wang5",66.6},{104,"shun6",66.8},{105,"yang7",88.6}};p=students;index=max_score_id(p,member);//结构体指针进行传参printf("Sno\tSname\tScore\n");for(index=0;index<member;index++){printf("%d\t%s\t%.2f\n",p->id,p->name,p->score);p++;}return0;}运行结果:
Sno Sname Score
102 li4 99.90
101 zhang3 88.80
105 yang7 88.60
104 shun6 66.80
103 wang5 66.60
——>以上内容是关于如何用C语言求结构体数组成员列表中的成绩最大值并按序输出,希望对初学者或再次学习者有所帮助,基础打扎实,不怕风吹雨打! 如果以上内容有错误或者内容不全,望大家提出!我也会继续写好每一篇博文!
待续未完
——文优
欢迎观看和提问!!!
下一篇:C语言基础:求两个数的最大公约数与最小公倍数