C语言程序设计-笔记8-结构
C语言程序设计-笔记8-结构
例9-1 输出平均分最高的学生信息。根据学生的基本信息包括学号、姓名、三门课程成绩以及个人平均成绩。输入n个学生的成绩信息,计算并输出平均分最高的学生信息。
#include
struct student{
int num;
char name[10];
int computer,english,math;
double average;
};
int main(void)
int i,n;
struct student max,stu;
printf("Input n:");
scanf("%d",&n);
printf("Input the student's number,name and course scores\n");
for(i=1;i<=n;i++)
{
printf("No.%d:",i);
scanf("%d%s%d%d%d",&stu.num,stu.name,&stu.math,&stu.english,
&stu.computer);
stu.average=(stu.math+stu.english+stu.computer)/3.0;
if(i==1)
{
max=stu;
}
else if(max.average { max=stu; } } printf("num:%d,name:%s,average:%.2f\n",max.num,max.name,max.average); return 0; 例9-2 学生成绩排序。输入n(n〈50)个学生的成绩信息,按照学生的个人平均成绩从高到低输出他们的信息。 #include struct student{ int num; char name[10]; int computer,english,math; double average; }; int main(void) int i,index,j,n; struct student students[50],temp; printf("Input n:"); scanf("%d",&n); for(i=0;i { printf("Input the info of No.%d:\n",i+1); printf("number:"); scanf("%d",&students[i].num); printf("name:"); scanf("%s",students[i].name); printf("math score:"); scanf("%d",&students[i].math); printf("english score:"); scanf("%d",&students[i].english); printf("computer score:"); scanf("%d",&students[i].computer); students[i].average=(students[i].math+students[i].english+ students[i].computer)/3.0; } for(i=0;i { index=i; for(j=i+1;j { if(students[j].average>students[index].average) { index=j; } } temp=students[index]; students[index]=students[i]; students[i]=temp; } printf("num\t name\t average\n"); for(i=0;i { printf("%d \t%s \t%.2f\n",students[i].num,students[i].name, students[i].average); } return 0; 例9-3 修改学生成绩。输入n(n<50)个学生的成绩信息,再输入一个学生的学号、课程以及成绩,在自定义函数中修改该学生指定课程的成绩。 #include struct student{ int num; char name[10]; int computer,english,math; double average; }; int update_score(struct student *p,int n,int num,int course,int score); int main(void) int course,i,n,num,pos,score; struct student students[50]; printf("Input n:"); scanf("%d",&n); for(i=0;i { printf("Input the info of No.%d:\n",i+1); printf("number:"); scanf("%d",&students[i].num); printf("name:"); scanf("%s",students[i].name); printf("math score:"); scanf("%d",&students[i].math); printf("english score:"); scanf("%d",&students[i].english); printf("computer score:"); scanf("%d",&students[i].computer); } printf("Input the number of the students to be updated:"); scanf("%d",&num); printf("Choice the course:1.math 2.english 3.computer:"); scanf("%d",&course); printf("Input the new score:"); scanf("%d",&score); pos=update_score(students,n,num,course,score); if(pos==-1) { printf("Not found!\n"); } else { printf("After update:\n"); printf("num\t math\t english\t computer\n"); printf("%d\t %d\t %d\t %d\n",students[pos].num,students[pos].math, students[pos].english,students[pos].computer); } return 0; int update_score(struct student *p,int n,int num,int course,int score) int i,pos; for(i=0;i { if(p->num==num) { break; } } if(i { switch(course) { case 1:p->math=score;break; case 2:p->english=score;break; case 3:p->computer;break; } pos=i; } else { pos=-1; } return pos; C语言程序设计/何钦铭,颜晖主编.---4版.---北京:高等教育出版社,2020.9参考资料