Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include "headers/InputChecker.h"
  6.  
  7. /*the input function, takes in input from the keyboard, and goes through a series of functions tests,
  8. If they all pass, then the input is valid. Once they do pass, the names are put to lowercase, and the score
  9. is converted into an int for storage later. Then it's checked if this is the first time running to program,
  10. if so, then the nalloc variable becomes a 1, which lets the array have an initial size.
  11. Then adds the data into the array from the validated input. and nused is increased by 1.
  12.  
  13. When it loops through again, it checks to see if the nalloc is the same increment as the nused. If it is,
  14. then it takes the size of the array, and stores it into a buffer (temp), and increases the size of it by double.
  15. Then that value is used to overwrite the size of the current array. Effectively increasing the array everytime
  16. it's needed.
  17.  
  18. At the end, when the EOF key is pressed, a for loop is called, which loops through the amount of nused, and
  19. free's up the allocated memory.*/
  20. void input(record_list *recordList){
  21. char lineChecker[LINESIZE];
  22. char id[LINESIZE];
  23. char first[LINESIZE];
  24. char last[LINESIZE];
  25. /*the score is at first a string, because I needed to check if there was letter or
  26. decimals in it. Which I couldn't think of how to do if it was just an int.
  27. Later it'll be converted over to an int using the atoi() function, only if it passes the
  28. validation tests. A classmate told me about in order to overcome this issue.*/
  29. char tempScore[LINESIZE];
  30. /*a buffer for the record used for copying the array over*/
  31. record tempRec;
  32.  
  33. (*recordList).nused = (*recordList).nalloc = 0;
  34.  
  35. /*As long as we can read a line, pressing the OEF key will be the only way to break
  36. out of this loop.*/
  37. while(fgets(lineChecker, LINESIZE, stdin)){
  38. /*pass in the inputs from lineChecker (which is the stdin) to id, first, last, and score*/
  39. if(sscanf(lineChecker, "%s %s %s %s", id, first, last, tempScore) >= 4){
  40. /*check inputs for validation*/
  41. if( check_sizes(id, first, last) == 1 &&
  42. check_id_format(id) == 1 &&
  43. score_digits(tempScore) == 1){
  44. /*converts the string input of score to an int for storage*/
  45. int score = atoi(tempScore);
  46.  
  47. /*if passed, then convert names to loweercase*/
  48. name_tolower(first);
  49. name_tolower(last);
  50.  
  51. /* This is just left in so I can remember how I debugged an issue.
  52. fprintf(stderr, "nalloc before adding: %lu\n", (*recordList).nalloc);
  53. fprintf(stderr, "nused before adding: %lu\n", (*recordList).nused);*/
  54.  
  55. /*checks if array size is zero*/
  56. check_nalloc_equal_zero(recordList);
  57. /*checks if array needs to be expanded, and then expands it*/
  58. check_and_expand(recordList);
  59.  
  60. /*copies over the data into the array's variables*/
  61. strcpy(tempRec.id, id);
  62. strcpy(tempRec.name.last, last);
  63. strcpy(tempRec.name.first, first);
  64. tempRec.score = score;
  65.  
  66. /*store new data into the array from the tempRec*/
  67. (*recordList).data[(*recordList).nused] = tempRec;
  68.  
  69. /*increase the amount of nused count*/
  70. (*recordList).nused++;
  71.  
  72. /* Once again, left in so I remember later how to debug for future C assignments.
  73. fprintf(stderr, "nused: %lu\n", (*recordList).nused);
  74.  
  75. fprintf(stderr, "nalloc: %lu", (*recordList).nalloc);
  76. fprintf(stderr, "\n");*/
  77. }
  78. }
  79. }
  80. }
  81.  
  82. /*If the nalloc is zero, then start up the nalloc amount.*/
  83. void check_nalloc_equal_zero(record_list *recordList){
  84. /*checks if the storage amount is zero*/
  85. if((*recordList).nalloc == 0){
  86. /*creates a temp record*/
  87. record *temp;
  88. /*increases the amount by 1 (so 0 -> 1)*/
  89. (*recordList).nalloc++;
  90. /*reallocs the temp array to become 1*/
  91. temp = realloc((*recordList).data, ((*recordList).nalloc + sizeof(record)));
  92. /*the dynamic array now becomes the size of the temp array*/
  93. (*recordList).data = temp;
  94.  
  95. #ifdef DEBUG
  96. fprintf(stderr, "#");
  97. #endif
  98. }
  99. }
  100.  
  101. /*checks if the array needs to be expanded or not.*/
  102. void check_and_expand(record_list *recordList){
  103. /*if the amount used is the same as the amount allocated*/
  104. if((*recordList).nused == (*recordList).nalloc){
  105. /*created a temp record*/
  106. record *temp;
  107. /*increase the size of the nalloc by 2 times the current amount*/
  108. (*recordList).nalloc *= 2;
  109. /*assign the space of the current nalloc, to a temp*/
  110. temp = realloc((*recordList).data, ((*recordList).nalloc * sizeof(record)));
  111.  
  112. /*remake the size of the data to the size of the temp*/
  113. (*recordList).data = temp;
  114.  
  115. #ifdef DEBUG
  116. fprintf(stderr, "#");
  117. #endif
  118. }
  119. }
  120.  
  121. /*If the the student ID isn't the required size,
  122. or the first name is bigger than the allocated namesize
  123. or the last name is bigger than the allocated namesize
  124. then return -1, and fail*/
  125. int check_sizes(char id[], char first[], char last[]){
  126. if(strlen(id) != (IDSIZE-1) || strlen(first) >= NAMESIZE
  127. || strlen(last) >= NAMESIZE){
  128. return -1;
  129. }
  130. /*return 0 if correct*/
  131. return 1;
  132. }
  133.  
  134. /*check if the first spot in the array for id is not an 'a'
  135. if it's not then loop through, and check if the remaining characters
  136. are not digits, if they aren't, then return -1
  137. if the first character isn't an 'a', return -1*/
  138. int check_id_format(char id[]){
  139. size_t i;
  140. /*if the first character isn't an a*/
  141. if(id[0] != 'a'){
  142. /*then loop through the rest of the array, and check
  143. if they aren't digits*/
  144. for(i = 1; id[i] != '\0'; i++){
  145. if(!isdigit(id[i])){
  146. /*if they aren't digits, return a false*/
  147. return -1;
  148. }
  149. }
  150. /*if the first one isn't an 'a', return a false*/
  151. return -1;
  152. }
  153. /*return 0 if correct*/
  154. return 1;
  155. }
  156.  
  157. /*if the score isn't above 0, or below 100,
  158. then return -1*/
  159. int score_digits(char scoreCheck[]){
  160. int score = atoi(scoreCheck);
  161. size_t i;
  162. for(i = 0; scoreCheck[i] != '\0'; i++){
  163. if(i == '.' || i == '#' || !isdigit(scoreCheck[i])){
  164. return -1;
  165. }
  166. }
  167. if(score < 0 || score > 100){
  168. return -1;
  169. }
  170. /*return 1 if correct*/
  171. return 1;
  172. }
  173.  
  174. /*loops through the first and last name, and converts the characters in the array to lowercase*/
  175. void name_tolower(char name[]){
  176. size_t i;
  177. /*loop through the array of first name, and set all the characters
  178. to lowercase*/
  179. for(i = 0; name[i] != '\0'; i++){
  180. name[i] = tolower(name[i]);
  181. }
  182. }
  183.  
  184. /*Also taken from my lab6*/
  185. /*loops through the data, and prints out the array data*/
  186. void print_record(const record_list *recordList){
  187. size_t i;
  188. for(i = 0; i < (*recordList).nused; i++){
  189. printf("%s : %s, %s : %d\n",
  190. (*recordList).data[i].id,
  191. (*recordList).data[i].name.last,
  192. (*recordList).data[i].name.first,
  193. (*recordList).data[i].score);
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement