定义结构记录学生分数,并由高到低排列出来

Define a struct type that contains a student’s number, name and score of three courses. Write a program, to enter 5 students’ information(name, number & scores of the 3 courses), and calculate the total score of all three courses, then output the students' number & name sorted by the total score from high to low.

注意指针不能用%s读取会导致错误

char name【10】正确

但是char *name不正确用scanf读取时候

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 5
int main(void)
{
	struct studentRecord
	{
		int number;
		char name[10];
		float score[4];
		float sum;
	}student[N],studentTemp;
	
	int i;
	for(i=0;i<N;++i)
	{
		printf("ÊäÈëѧºÅ\tÐÕÃû\t¿ÆÄ¿1³É¼¨\t¿ÆÄ¿2³É¼¨\t¿ÆÄ¿3³É¼¨\n");
		scanf("%d %s %f %f %f",&student[i].number,student[i].name,&student[i].score[0],&student[i].score[1],&student[i].score[2]);
		//printf("1\n");
		student[i].sum=student[i].score[0]+student[i].score[1]+student[i].score[2];
	}
	
	int num[N];
	int j; 
	printf("ѧºÅ\tÐÕÃû\t¿ÆÄ¿1³É¼¨\t¿ÆÄ¿2³É¼¨\t¿ÆÄ¿3³É¼¨\t×Ü·Ö\n"); 
	for(i=0;i<N;i++){
		num[i]=i;
		for(j=i+1;j<N;j++){
			if(student[i].sum<student[j].sum){
				studentTemp.sum=student[i].sum;
				studentTemp.number=student[i].number;
				strcpy(studentTemp.name,student[i].name);
				studentTemp.score[0]=student[i].score[0];
				studentTemp.score[1]=student[i].score[1];
				studentTemp.score[2]=student[i].score[2];
				student[i].sum=student[j].sum;
				student[i].number=student[j].number;
				strcpy(student[i].name,student[j].name);
				student[i].score[0]=student[j].score[0];
				student[i].score[1]=student[j].score[1];
				student[i].score[2]=student[j].score[2];
				student[j].sum=studentTemp.sum;
				student[j].number=studentTemp.number;
				strcpy(student[j].name,studentTemp.name);
				student[j].score[0]=studentTemp.score[0];
				student[j].score[1]=studentTemp.score[1];
				student[j].score[2]=studentTemp.score[2];
				
			}
		}
		//printf("1\n");
		printf("%5d\t%s\t%4.1f\t%12.1f\t%13.1f\t%11.1f\n",student[i].number,student[i].name,student[i].score[0],student[i].score[1],student[i].score[2],student[i].sum);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值