answer06.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "pa06.h"
  5. #define MAXIMUM_LENGTH 80
  6. int countStudent(char * filename, int * numberStudent)
  7. /*
  8. * This function opens the file whose name is given as the first
  9. * argument. If the file cannot be opened, the function returns
  10. * EXIT_FAILURE.
  11. *
  12. * If the file is opened successfully, the function reads and counts
  13. * the number of students in the file.
  14. *
  15. * Each student has an ID (integer) and a name (string). For
  16. * simplicity, each student's name is only one string, not two for
  17. * first name and last name.
  18. *
  19. * Each student's record uses one line only. One student's record (ID
  20. * and name) will not span in two or more lines.
  21. *
  22. * Two students' records use two differne lines. The input file does
  23. * not contain any single line that has two (or more) students'
  24. * records.
  25. *
  26. * The number of students is stored in numberStudent (notice that this
  27. * is a pointer).
  28. *
  29. * Please be careful not to count empty lines or the end of the file
  30. * as another student. Otherwise, your program will print empty lines
  31. * in the output and this is incorrect.
  32. *
  33. * An empty line may include space. Thus, you cannot assume that an
  34. * empty line has only the newline character '\n'.
  35. *
  36. * The function closes the file and returns EXIT_SUCCESS.
  37. *
  38. * Hint: You can use the return value of fscanf to determine whether
  39. * a line contains an integer. For example,
  40. *
  41. * int val;
  42. * char str[MAXIMUM_LENGTH];
  43. *
  44. * fscanf(fptr, "%d", & val);
  45. *
  46. * return 1 if an integer is successfully read.
  47. * If a line has no integer, fscanf returns zero.
  48. *
  49. * fscanf(fptr, "%d %s", & val, str);
  50. *
  51. * return 2 if an integer and a string are successfully read.
  52. *
  53. */
  54. {
  55. return EXIT_SUCCESS;
  56. }
  57. /*
  58. * ================================================
  59. */
  60. Student * allocateStudent (int numberStudent)
  61. /*
  62. * allocate enough memory space to store students' records for
  63. * numberStudent students.
  64. *
  65. * If memory allocation fails, return NULL.
  66. * If memory allocation succeeds, return the memory address;
  67. */
  68. {
  69. return NULL;
  70. }
  71. /*
  72. * ================================================
  73. */
  74. int readStudent(char * filename, Student * studentArray,
  75. int numberStudent)
  76. /*
  77. * This function opens the file whose name is given as the first
  78. * argument. If the file cannot be opened, the function returns
  79. * EXIT_FAILURE.
  80. *
  81. * If the file is opened successfully, the function reads the
  82. * students' records and stores the data in the second argument.
  83. *
  84. * Please be careful not to count empty lines or the end of the file
  85. * as another student. Otherwise, your program will print empty lines
  86. * in the output and this is incorrect.
  87. *
  88. * The function closes the file and returns EXIT_SUCCESS.
  89. */
  90. {
  91. return EXIT_SUCCESS;
  92. }
  93. /*
  94. * ================================================
  95. */
  96. int writeStudent(char * filename, Student * studentArray,
  97. int numberStudent)
  98. /*
  99. * This function opens the file whose name is given as the first
  100. * argument. If the file cannot be opened, the function returns
  101. * EXIT_FAILURE.
  102. *
  103. * If the file is opened successfully, the function writes the
  104. * students' records to the file, one student per line. The line for
  105. * the last student should end with a newline character '\n'.
  106. *
  107. * This function should not write any empty line.
  108. *
  109. * The function closes the file and returns EXIT_SUCCESS.
  110. */
  111. {
  112. return EXIT_SUCCESS;
  113. }
  114. void swapStudent(Student * student1, Student * student2)
  115. /*
  116. * swap the IDs and the names of the two students
  117. *
  118. * Allocate only necessary memory. You cannot assume that every
  119. * student's name has MAXIMUM_LENGTH characters.
  120. *
  121. * Hint: Please be careful that the two students' names
  122. * may have different lengths.
  123. */
  124. {
  125. }
  126. /*
  127. * ================================================
  128. */
  129. void freeStudent(Student * studentArray, int numberStudent)
  130. /*
  131. * This function releases the memory allocated for the Student array.
  132. * Please remember that everything allocated using malloc must be
  133. * released.
  134. *
  135. * Hint: Each student's name requires malloc earlier so they
  136. * should be released now.
  137. *
  138. */
  139. {
  140. }
  141. /*
  142. * ====== Do not modify anything below ===================
  143. */
  144. void sortStudentbyID(Student * studentArray, int numberStudent)
  145. /* This function sorts the students by their IDs. */
  146. {
  147. /* use selection sort */
  148. int ind1, ind2;
  149. for (ind1 = 0; ind1 < numberStudent; ind1 ++)
  150. {
  151. int minInd = ind1;
  152. for (ind2 = ind1 + 1; ind2 < numberStudent; ind2 ++)
  153. {
  154. if (studentArray[minInd].ID > studentArray[ind2].ID)
  155. {
  156. minInd = ind2;
  157. }
  158. }
  159. if (minInd != ind1)
  160. {
  161. swapStudent(& (studentArray[minInd]), & (studentArray[ind1]));
  162. }
  163. }
  164. }
  165. /*
  166. * ================================================
  167. */
  168. void sortStudentbyName(Student * studentArray, int numberStudent)
  169. /* This function sorts the students by their names. */
  170. {
  171. int ind1, ind2;
  172. for (ind1 = 0; ind1 < numberStudent; ind1 ++)
  173. {
  174. int minInd = ind1;
  175. for (ind2 = ind1 + 1; ind2 < numberStudent; ind2 ++)
  176. {
  177. if (strcmp(studentArray[minInd].name,
  178. studentArray[ind2].name) > 0)
  179. {
  180. minInd = ind2;
  181. }
  182. }
  183. if (minInd != ind1)
  184. {
  185. swapStudent(& (studentArray[minInd]), & (studentArray[ind1]));
  186. }
  187. }
  188. }