您的位置:首页 > 其它

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

2014-11-17 23:10 274 查看
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
,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
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐