C语言|C语言【输入一些关于学生的数据然后按照学号从小到大排列】


简单的学生信息排序问题
【C语言|C语言【输入一些关于学生的数据然后按照学号从小到大排列】】

#include.h> #include.h> //define a struction. struct st_basic{ char name[35]; int num; int scls; }; //Output is arranged in order of number from small to large. int main(){ struct st_basic st[3]; int i; for(i=0; i<3; i++){ printf("Please input the %d st data:\n",i+1); scanf("%s%d%d",&st[i].name,&st[i].num,&st[i].scls); } /* Here we add a function sorted by student number. So our program can be sorted according to the number of students. BUT The range of ints in C language is -2147483648 to 2147483647. So if the number range exceeds this range, the wrong result will be output. */ int j; struct st_basic temp; for(i=0; i<2; i++){ for(j=1; j<=2; j++){ if(st[i].num>=st[j].num){ temp=st[i]; st[i]=st[j]; st[j]=temp; } } } //output these data... for(i=0; i<3; i++){ printf("%d>>> %d %s %d\n",i,st[i].num,st[i].name,st[i].scls); } return 0; }

    推荐阅读