fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Seito
  6. {
  7. int num;
  8. char lname[20];
  9. char fname[20];
  10. int point;
  11. } Seito;
  12.  
  13. typedef struct Cell
  14. {
  15. void* car;
  16. struct Cell* cdr;
  17. } Cell;
  18.  
  19. typedef struct SortResult
  20. {
  21. int count;
  22. Cell* cell;
  23. } SortResult;
  24.  
  25. void destroy_cell(Cell* cell)
  26. {
  27. Cell* a;
  28. while (cell != NULL)
  29. {
  30. a = cell;
  31. cell = cell->cdr;
  32. free(a->car);
  33. free(a);
  34. }
  35. }
  36.  
  37. Cell* loadData_(FILE* f)
  38. {
  39. int num;
  40. char lname[20];
  41. char fname[20];
  42. int point;
  43. Cell* head = NULL;
  44. Cell* tail = NULL;
  45. Cell* cell = NULL;
  46. int success = 1;
  47. Seito* seito = NULL;
  48.  
  49. while (fscanf(f, "%d%s%s%d", &num, lname, fname, &point) != EOF)
  50. {
  51. success = 0;
  52.  
  53. seito = malloc(sizeof(Seito));
  54. if (seito != NULL)
  55. {
  56. seito->num = num;
  57. strcpy(seito->lname, lname);
  58. strcpy(seito->fname, fname);
  59. seito->point = point;
  60.  
  61. cell = malloc(sizeof(Cell));
  62. if (cell != NULL)
  63. {
  64. cell->car = seito;
  65. cell->cdr = NULL;
  66. if (tail == NULL)
  67. {
  68. tail = cell;
  69. head = cell;
  70. }
  71. else
  72. {
  73. tail->cdr = cell;
  74. tail = cell;
  75. }
  76. success = 1;
  77. }
  78. }
  79. if (!success)
  80. {
  81. break;
  82. }
  83. }
  84. if (success)
  85. {
  86. return head;
  87. }
  88. else
  89. {
  90. free(seito);
  91. destroy_cell(head);
  92. return NULL;
  93. }
  94. }
  95.  
  96. Cell* loadData()
  97. {
  98. Cell* cell = NULL;
  99. FILE* f = fopen("seito.dat", "r");
  100. if (f != NULL)
  101. {
  102. cell = loadData_(f);
  103. fclose(f);
  104. }
  105. return cell;
  106. }
  107.  
  108. SortResult* sort(Cell* cell, int (*cmp)(Cell*, Cell*))
  109. {
  110. SortResult* result = malloc(sizeof(SortResult));
  111. if (result != NULL)
  112. {
  113. Cell* h = cell;
  114. Cell* c = NULL;
  115. Cell* n = NULL;
  116. int count = 0;
  117.  
  118. if (h != NULL)
  119. {
  120. cell = h->cdr;
  121. h->cdr = NULL;
  122. while (cell != NULL)
  123. {
  124. n = cell->cdr;
  125. count++;
  126. if (cmp(cell, h) <= 0)
  127. {
  128. cell->cdr = h;
  129. h = cell;
  130. }
  131. else
  132. {
  133. c = h;
  134. while (c->cdr != NULL)
  135. {
  136. count++;
  137. if (cmp(cell, c->cdr) <= 0)
  138. {
  139. cell->cdr = c->cdr;
  140. c->cdr = cell;
  141. break;
  142. }
  143. c = c->cdr;
  144. }
  145. }
  146. cell = n;
  147. }
  148. }
  149. result->count = count;
  150. result->cell = h;
  151. }
  152. return result;
  153. }
  154.  
  155. int bigToSmall(Cell* x, Cell* y)
  156. {
  157. Seito* x1 = x->car;
  158. Seito* y1 = y->car;
  159. if (x1->point < y1->point)
  160. {
  161. return 1;
  162. }
  163. else if (x1->point > y1->point)
  164. {
  165. return -1;
  166. }
  167. else
  168. {
  169. return 0;
  170. }
  171. }
  172.  
  173. int main(void)
  174. {
  175. Seito* seito;
  176. Cell* cell;
  177. SortResult* result;
  178. Cell* head = loadData();
  179. if (head != NULL)
  180. {
  181. result = sort(head, bigToSmall);
  182. if (result != NULL)
  183. {
  184. head = result->cell;
  185. cell = head;
  186. printf("%4s %-20s %s\n", "番号", "氏名", "得点");
  187. while (cell != NULL)
  188. {
  189. seito = cell->car;
  190. printf("%d %-10s%-10s%3d\n", seito->num, seito->lname, seito->fname, seito->point);
  191. cell = cell->cdr;
  192. }
  193. printf("整列の為の比較回数=%d回\n", result->count);
  194. free(result);
  195. }
  196. destroy_cell(head);
  197. }
  198. return 0;
  199. }
  200.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty