C语言编写程序:

1)创建一个名称stu的结构体,该结构体包含3个成员分别为:stuID(学号)、stuName(姓名)、stuScore(成绩);
2)创建一个名称为scoreList\长度为5、类型为struct stu的数组;
3)从键盘为scoreArray数组的各元素赋值;
4)使用冒泡排序法法把scoreArray中的元素按照成绩从高到低进行排序;
5)在屏幕上打印排序好的数组。

#include<stdio.h>
struct stu
{char stuID[20];
 char stuName[20];
 int stuScore;
};
int main( )
{struct stu scoreArray[5],t;
 int i,j;
 for(i=0;i<5;i++)
   scanf("%s%s%d",scoreArray[i].stuID,scoreArray[i].stuName,
     &scoreArray[i].stuScore);
 for(i=0;i<4;i++)
   for(j=0;j<4-i;j++)
     if(scoreArray[j].stuScore<scoreArray[j+1].stuScore)
     {t=scoreArray[j];
      scoreArray[j]=scoreArray[j+1];
      scoreArray[j+1]=t;
 }
 for(i=0;i<5;i++)
   printf("%s\t%s\t%d\n",scoreArray[i].stuID,scoreArray[i].stuName,
     scoreArray[i].stuScore);
 return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-26
#include "stdio.h"
struct stu{
int stuID,stuScore;
char stuName[20];
};
int main(int argv,char *argc[]){
struct stu scorList[5],t;
int i,j;
printf("Please enter the data(ID NAME SCORE(separated by spaces)...\n");
for(i=0;i<5;i++)
if(scanf("%d%s%d",&scorList[i].stuID,&scorList[i].stuName,&scorList[i].stuScore)!=3
|| scorList[i].stuScore>100 || scorList[i].stuScore<0){
printf("Input error, redo:\n");
i--;
fflush(stdin);
}
for(i=0;i<5;i++)
for(j=i+1;j<5;j++)
if(scorList[j].stuScore>scorList[j-1].stuScore)
t=scorList[j],scorList[j]=scorList[j-1],scorList[j-1]=t;
printf("\nSTUID\tSTUNAME\tSTUSCORE\n========================\n");
for(i=0;i<5;i++)
printf("%d\t%s\t%d\n",scorList[i].stuID,scorList[i].stuName,scorList[i].stuScore);
return 0;
}

运行样例:

相似回答