fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. //dai6kai
  4. typedef struct {
  5. char name;
  6. int english;
  7. int math;
  8. } Student;
  9.  
  10. void swap_student(Student *data1, Student *data2) {
  11. Student temp = *data1;
  12. *data1 = *data2;
  13. *data2 = temp;
  14. }
  15.  
  16. void sort_english(Student data[]) {
  17. for (int i = 0; i < 5 ; i++) {
  18. for (int j = i + 1; j < 5; j++) {
  19. if (data[i].english > data[j].english) {
  20. swap_student(&data[i], &data[j]);
  21. }
  22. }
  23. }
  24. }
  25.  
  26. void sort_math(Student data[]) {
  27. for (int i = 0; i < 5 ; i++) {
  28. for (int j = i + 1; j < 5; j++) {
  29. if (data[i].math < data[j].math) {
  30. swap_student(&data[i], &data[j]);
  31. }
  32. }
  33. }
  34. }
  35.  
  36. void sort_name(Student data[]) {
  37. for (int i = 0; i < 5 ; i++) {
  38. for (int j = i + 1; j < 5; j++) {
  39. if (data[i].name > data[j].name) {
  40. swap_student(&data[i], &data[j]);
  41. }
  42. }
  43. }
  44. }
  45.  
  46. int main() {
  47.  
  48. Student data[5] = {
  49. {'e', 60, 70},
  50. {'b', 80, 75},
  51. {'a', 90, 72},
  52. {'c', 70, 60},
  53. {'d', 50, 56}
  54. };
  55.  
  56. printf("sort_english\n");
  57. sort_english(data);
  58. for (int i = 0; i < 5; i++) {
  59. printf("name: %c, english: %d, math: %d\n", data[i].name, data[i].english, data[i].math);
  60. }
  61.  
  62. printf("\nsort_math\n");
  63. sort_math(data);
  64. for (int i = 0; i < 5; i++) {
  65. printf("name: %c, english: %d, math: %d\n", data[i].name, data[i].english, data[i].math);
  66. }
  67.  
  68. printf("\nsort_name\n");
  69. sort_name(data);
  70. for (int i = 0; i < 5; i++) {
  71. printf("name: %c, english: %d, math: %d\n", data[i].name, data[i].english, data[i].math);
  72. }
  73.  
  74. return 0;
  75. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
sort_english
name: d, english: 50, math: 56
name: e, english: 60, math: 70
name: c, english: 70, math: 60
name: b, english: 80, math: 75
name: a, english: 90, math: 72

sort_math
name: b, english: 80, math: 75
name: a, english: 90, math: 72
name: e, english: 60, math: 70
name: c, english: 70, math: 60
name: d, english: 50, math: 56

sort_name
name: a, english: 90, math: 72
name: b, english: 80, math: 75
name: c, english: 70, math: 60
name: d, english: 50, math: 56
name: e, english: 60, math: 70