C言語【学生に関するデータを入力し、学号に従って小さいものから大きいものに並べる】
簡単な学生情報ソートの問題
#include<stdio.h>
#include<string.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:
",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
",i,st[i].num,st[i].name,st[i].scls);
}
return 0;
}