fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define NUMBER 5
  5. #define NAME_LEN 64
  6.  
  7. typedef struct {
  8. char name[NAME_LEN];
  9. int height;
  10. float weight;
  11. long number;
  12. } Student;
  13.  
  14. void swap_Student(Student *x, Student *y) {
  15. Student temp = *x;
  16. *x = *y;
  17. *y = temp;
  18. }
  19.  
  20. void sort_by_weight(Student a[], int n) {
  21. int i, j;
  22. for (i = 0; i < n - 1; i++) {
  23. for (j = n - 1; j > i; j--) {
  24. if (a[j - 1].weight < a[j].weight) { // 体重の降順に変更
  25. swap_Student(&a[j - 1], &a[j]);
  26. }
  27. }
  28. }
  29. }
  30.  
  31. int main(void) {
  32. int i;
  33. Student std[] = {
  34. {"AYASE Eri", 162, 51.9, 1021},
  35. {"WATANABE You", 157, 47.5, 101},
  36. {"NAKASU Kasumi", 155, 44.6, 123},
  37. {"TUSHIMA Yoshiko", 156, 46.7, 713},
  38. {"SONODA Umi", 159, 46.8, 315},
  39. };
  40.  
  41. for (i = 0; i < NUMBER; i++)
  42. printf("%-14s %6d %6.1f %7ld\n", std[i].name, std[i].height, std[i].weight, std[i].number);
  43.  
  44. sort_by_weight(std, NUMBER);
  45.  
  46. puts("体重順にソートしました。");
  47. for (i = 0; i < NUMBER; i++)
  48. printf("%-14s %6d %6.1f %7ld\n", std[i].name, std[i].height, std[i].weight, std[i].number);
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
AYASE Eri         162   51.9    1021
WATANABE You      157   47.5     101
NAKASU Kasumi     155   44.6     123
TUSHIMA Yoshiko    156   46.7     713
SONODA Umi        159   46.8     315
体重順にソートしました。
AYASE Eri         162   51.9    1021
WATANABE You      157   47.5     101
SONODA Umi        159   46.8     315
TUSHIMA Yoshiko    156   46.7     713
NAKASU Kasumi     155   44.6     123