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_name(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 (strcmp(a[j - 1].name, a[j].name) > 0) {
  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. printf("Before sorting:\n");
  42. for (i = 0; i < NUMBER; i++)
  43. printf("%-14s %6d %6.1f %7ld\n", std[i].name, std[i].height, std[i].weight, std[i].number);
  44.  
  45. sort_by_name(std, NUMBER);
  46.  
  47. puts("名前の辞書順にソートしました。");
  48. for (i = 0; i < NUMBER; i++)
  49. printf("%-14s %6d %6.1f %7ld\n", std[i].name, std[i].height, std[i].weight, std[i].number);
  50.  
  51. return 0;
  52. }
  53.  
  54.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Before sorting:
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
NAKASU Kasumi     155   44.6     123
SONODA Umi        159   46.8     315
TUSHIMA Yoshiko    156   46.7     713
WATANABE You      157   47.5     101