Guest User

Untitled

a guest
Mar 20th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. /*
  2. Program : Assignment 7
  3. Student Name : YeEum Lee (Kate)
  4. Student Number : A00966565
  5. Date : March 6, 2018
  6. Version : 1.7
  7. comment: read_record - size_t n removed for now
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <string.h>
  14. #define IDSIZE 10
  15. #define NAMESIZE 20
  16. #define BLOCK 2
  17. #define LINESIZE 1024
  18.  
  19. typedef struct{
  20. char last[NAMESIZE];
  21. char first[NAMESIZE];
  22. } name;
  23.  
  24. typedef struct{
  25. char id[IDSIZE];
  26. name name;
  27. int score;
  28. } record;
  29.  
  30. int read_record(record *prec);
  31. int scoreDescIdAsc(const void *a, const void *b);
  32. int scoreDescNameAsc(const void *a, const void *b);
  33. int scoreDescNameAscIdAsc(const void *a, const void *b);
  34. void sort_score_desc_id_asc(record a[], size_t n);
  35. void sort_score_desc_name_asc(record a[], size_t n);
  36. void sort_score_desc_name_asc_id_asc(record a[], size_t n);
  37. void print_record(const record *prec);
  38.  
  39. int sortBy = 0; //global variable
  40.  
  41. int main(void){
  42. int i;
  43. record r;
  44.  
  45. for (i = 0; i < 3; i++) {
  46. if (read_record(&r)!= 1) {
  47. printf("function error\n");
  48. return 1;
  49. }
  50. }
  51.  
  52. sort_score_desc_name_asc_id_asc(&r, 3);
  53.  
  54. for (i = 0; i < 3; i++) {
  55. print_record(&r);
  56. }
  57.  
  58. return 0;
  59. }
  60.  
  61. int read_record(record *prec) {
  62.  
  63. char line[LINESIZE];
  64. char studentnumber_temp[LINESIZE];
  65. char fname_temp[LINESIZE];
  66. char lname_temp[LINESIZE];
  67. int score_temp[LINESIZE];
  68. int index;
  69. printf("Enter the id, first name, last name and score with space\n");
  70. if(!fgets(line, LINESIZE, stdin)){
  71. clearerr(stdin);
  72. return 0;
  73. }
  74.  
  75. if((sscanf(line, "%s %s %s %d", studentnumber_temp, fname_temp, lname_temp, score_temp )) >=4){
  76. /* id - consists of an 'a' followed by 8 digits */
  77. if(strlen(studentnumber_temp) == 9 && studentnumber_temp[0] == 'a'){
  78. /* score - must be an integer between 0 and 100 */
  79. if(*score_temp >= 0 && *score_temp <= 100){
  80. /* name - both first name and last name should have length less than NAMESIZE */
  81. if(strlen(fname_temp) < NAMESIZE && strlen(lname_temp) < NAMESIZE){
  82. /* store the first & last names in all lowercase in the record */
  83. for(index = 0; fname_temp[index] != '\0'; index++){
  84. fname_temp[index] = tolower(fname_temp[index]);
  85. }
  86. for(index = 0; lname_temp[index] != '\0'; index++){
  87. lname_temp[index] = tolower(lname_temp[index]);
  88. }
  89. /* copy to the record */
  90. strcpy(prec->id, studentnumber_temp);
  91. strcpy(prec->name.first, fname_temp);
  92. strcpy(prec->name.last, lname_temp);
  93. prec->score = *score_temp;
  94. return 1;
  95. }
  96. }
  97. }
  98. }
  99. return 0;
  100. }
  101.  
  102. int scoreDescIdAsc(const void *a, const void *b){
  103. const record *p = a;
  104. const record *q = b;
  105. int n = q->score - p->score;
  106. if(n != 0){
  107. return n;
  108. }
  109. return p->id - q->id;
  110. }
  111.  
  112. int scoreDescNameAsc(const void *a, const void *b){
  113. const record *p = a;
  114. const record *q = b;
  115. int m;
  116. int n = q->score - p->score;
  117. if(n != 0){
  118. return n;
  119. }
  120. m = strcmp(p->name.last,q->name.last);
  121. if( m != 0){
  122. return m;
  123. }
  124. return strcmp(p->name.first, q->name.first);
  125. }
  126.  
  127. int scoreDescNameAscIdAsc(const void *a, const void *b){
  128. const record *p = a;
  129. const record *q = b;
  130. int n = q->score - p->score;
  131. int m,s;
  132.  
  133. if(n != 0){
  134. return n;
  135. }
  136. m = strcmp(p->name.last, q->name.last);
  137. if(m != 0){
  138. return m;
  139. }
  140. s = strcmp(p->name.first, q->name.first);
  141. if(s != 0){
  142. return s;
  143. }
  144. return strcmp(p->id, q->id);
  145. }
  146.  
  147.  
  148. /*
  149. sorts the array in descending order of scores. If two or more records have the same score,
  150. they are then sorted in ascending order of their IDs.
  151. */
  152. void sort_score_desc_id_asc(record a[], size_t n){
  153. sortBy = 1;
  154. qsort(a, n, sizeof(a[0]), scoreDescIdAsc);
  155. }
  156.  
  157. /*
  158. sorts the array in descending order of scores. If two or more records have the same score,
  159. they are then sorted in ascending order of their names.
  160. */
  161. void sort_score_desc_name_asc(record a[], size_t n){
  162. sortBy = 2;
  163. qsort(a, n, sizeof(a[0]), scoreDescNameAsc);
  164. }
  165.  
  166. /*
  167. sorts the array in descending order of scroes. If two or more records have the same score,
  168. they are then sorted in ascending order of their names.
  169. If furthermore, several records have the same score & the same name (first , last), they are then
  170. sorted in ascending order of their IDs.
  171. */
  172. void sort_score_desc_name_asc_id_asc(record a[], size_t n){
  173. sortBy = 0;
  174. qsort(a, n, sizeof(a[0]), scoreDescNameAscIdAsc);
  175. }
  176.  
  177. /*
  178. Prints the record pointed to by prec.
  179. */
  180. void print_record(const record *prec){
  181. //Need to organize each case
  182. switch(sortBy){
  183. case 0:
  184. printf("%d : %s, %s : %s\n", prec->score, prec->name.last, prec->name.first, prec->id);
  185. break;
  186. case 1:
  187. printf("%d : %s, %s : %s\n", prec->id, prec->name.last, prec->name.first, prec->score );
  188. break;
  189. case 2:
  190.  
  191. printf("%d : %s, %s : %s\n", prec->score, prec->name.last, prec->name.first, prec->id);
  192. break;
  193. default:
  194. break;
  195.  
  196. }
  197.  
  198. }
Add Comment
Please, Sign In to add comment