Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct book {
  6. char *title;
  7. char *author;
  8. char *subject;
  9. } book;
  10.  
  11. // linked list
  12. typedef struct library {
  13. struct book collection;
  14. int num_books;
  15. struct library *next;
  16. } library;
  17.  
  18. void copybook(book *dest, book *source);
  19. library *addbook(library *list, book* Book, FILE *file);
  20. void writeToOutput(char *);
  21. library *addToLibraryList(library **, book* );
  22. int searchForBook(library *list, char *detail);
  23. library *deleteFromLibraryList(library *, char *);
  24. library *deleteBook(library *, book*, FILE *);
  25.  
  26. // temp function
  27. void searchAll(library *);
  28.  
  29. #define CHAR_LIMIT 49
  30. #define LIBRARY_FILE "library.txt"
  31. #define OUTPUT_FILE "output.txt"
  32.  
  33. int main () {
  34. int instructions, valid_input, i;
  35. unsigned int updates = 0;
  36.  
  37. // buffers
  38. // char st[1024], title_input[100], author_input[100], subject_input[100], out_msg[1024];
  39. char out_msg[1024];
  40.  
  41. book *Book;
  42. library *head = (library *)malloc(sizeof(library));
  43. head = NULL;
  44.  
  45. FILE *fp;
  46. if ((fp = fopen(LIBRARY_FILE, "a+")) == NULL)
  47. {
  48. fprintf(stderr, "Error: %s cannot be opened.\n", LIBRARY_FILE);
  49. return 0;
  50. }
  51.  
  52. fscanf(fp, "%d", &updates);
  53.  
  54. for(i = 0; i < updates; i++) {
  55. fscanf(fp, "%d", &instructions);
  56. switch(instructions) {
  57. case 1:
  58. Book = (book *)malloc(sizeof(book));
  59. Book->title = malloc(sizeof(CHAR_LIMIT));
  60. Book->author = malloc(sizeof(CHAR_LIMIT));
  61. Book->subject = malloc(sizeof(CHAR_LIMIT));
  62. head = addbook(head, Book, fp);
  63. free(Book);
  64. break;
  65.  
  66. case 2:
  67. Book = (book *)malloc(sizeof(book));
  68. Book->title = malloc(sizeof(CHAR_LIMIT));
  69. Book->author = malloc(sizeof(CHAR_LIMIT));
  70. Book->subject = malloc(sizeof(CHAR_LIMIT));
  71. head = deleteBook(head, Book, fp);
  72. free(Book);
  73. break;
  74.  
  75. case 3:
  76. Book = (book *)malloc(sizeof(book));
  77. Book->title = malloc(sizeof(CHAR_LIMIT));
  78. Book->author = malloc(sizeof(CHAR_LIMIT));
  79. Book->subject = malloc(sizeof(CHAR_LIMIT));
  80. fscanf(fp, "%s", Book->title);
  81. if ((searchForBook(head, Book->title)) != 0) {
  82. snprintf(out_msg, sizeof(out_msg), "The book %s is currently in the library.", Book->title);
  83. } else {
  84. snprintf(out_msg, sizeof(out_msg), "The book %s is NOT currently in the library.", Book->title);
  85. }
  86. puts(out_msg);
  87. writeToOutput(out_msg);
  88. free(Book);
  89. break;
  90.  
  91. // case 4:
  92. // case 5:
  93. }
  94. }
  95.  
  96.  
  97. fclose(fp);
  98.  
  99. return 0;
  100. }
  101.  
  102. /* function declaration */
  103.  
  104. void copybook(book *dest, book *source)
  105. {
  106. strcpy(dest->author, source->author);
  107. strcpy(dest->subject, source->subject);
  108. strcpy(dest->title, source->title);
  109. }
  110.  
  111. int searchForBook(library *list, char *detail)
  112. {
  113. library *offset = list;
  114. while (offset != NULL)
  115. {
  116. if (strcmp(offset->collection.title, detail) == 0)
  117. {
  118. return 1;
  119. } else {
  120. offset = offset->next;
  121. }
  122. }
  123. return 0;
  124. }
  125.  
  126. void writeToOutput(char *msg)
  127. {
  128. FILE *fp;
  129. if ((fp = fopen(OUTPUT_FILE, "a+")) == NULL)
  130. {
  131. fprintf(stderr, "Error: %s cannot be opened.\n", OUTPUT_FILE);
  132. }
  133. else
  134. {
  135. fprintf(fp, "%s\n", msg);
  136. fprintf(fp, "\n");
  137. fclose(fp);
  138. }
  139. }
  140.  
  141. library *addbook(library *list, book *Book, FILE *file) {
  142. //TODO: do character length checking if possible
  143. char out_msg[1024];
  144.  
  145. fscanf(file, "%s%s%s", Book->title, Book->author, Book->subject);
  146.  
  147. // check for duplicate first
  148. if ((searchForBook(list, Book->title)) == 0) {
  149. list = addToLibraryList(&list, Book);
  150. snprintf(out_msg, sizeof(out_msg), "The book %s has been added to the library.", Book->title);
  151. puts(out_msg);
  152. writeToOutput(out_msg);
  153. return list;
  154. } else {
  155. fprintf(stderr, "Book %s already exists!\n", Book->title);
  156. return NULL;
  157. }
  158. }
  159.  
  160. library *deleteBook(library *list, book *Book, FILE *file) {
  161. char out_msg[1024];
  162.  
  163. fscanf(file, "%s", Book->title);
  164. if ((list = (deleteFromLibraryList(list, Book->title))) != NULL)
  165. {
  166. snprintf(out_msg, sizeof(out_msg), "The book %s has been removed from the library.", Book->title);
  167. puts(out_msg);
  168. writeToOutput(out_msg);
  169. return list;
  170. } else {
  171. fprintf(stderr, "Book %s not found!", Book->title);
  172. return list;
  173. }
  174. }
  175.  
  176. /* linked-list functions */
  177. library *addToLibraryList(library **list, book *book_details)
  178. {
  179. library *new_book = (library *) malloc(sizeof(library));
  180. // new_book;
  181. printf("%ld", sizeof(library));
  182. // printf("%p\n", new_book);
  183.  
  184. if(new_book == NULL) {
  185. fprintf(stderr, "Memory allocation failed!\n");
  186. exit(EXIT_FAILURE);
  187. }
  188.  
  189. // new book details
  190. new_book->collection = *book_details;
  191. new_book->next = NULL; // prevent next from pointing to somewhere random
  192.  
  193. // first insertion when no books exist
  194. if(*list == NULL) {
  195. *list = new_book;
  196. (*list)->num_books++;
  197. printf("new linked list started\n");
  198.  
  199. return *list;
  200.  
  201. } else {
  202. library *offset = *list;
  203. for(; offset->next != NULL; offset = offset->next)
  204. ;
  205. (*list)->num_books++;
  206. offset->next = new_book;
  207. printf("added to the end of the list\n");
  208. }
  209. return *list;
  210. }
  211.  
  212. library *deleteFromLibraryList(library *list, char *details)
  213. {
  214. // 1. search for last and search for book to be deleted
  215. // 2. copy last book into first book
  216. // 3. free the last
  217. // 4. return smth to show sucess or failure
  218. library *search = NULL, *prev, *last;
  219.  
  220. for(last = list, prev = NULL;
  221. last->next != NULL;
  222. prev = last, last = last->next)
  223. ;
  224.  
  225.  
  226. search = list;
  227. // if first node
  228. if (strcmp(details, search->collection.title) == 0) {
  229. copybook(&(search->collection), &(last->collection));
  230. free(last);
  231. printf("first node detected for deletion\n");
  232. return list;
  233. }
  234.  
  235. // loop through the nodes
  236. while(search != NULL) {
  237. if(strcmp(search->collection.title, details) == 0) {
  238. printf("other nodes detected for deletion\n");
  239. break;
  240. }
  241. search = search->next;
  242. }
  243.  
  244. copybook(&(search->collection), &(last->collection));
  245. // prev->next = NULL;
  246. free(last);
  247. return list;
  248. }
  249.  
  250.  
  251. void searchAll(library *list)
  252. {
  253. library *offset = list;
  254. while(offset!= NULL) {
  255. // if (offset->next == NULL)
  256. // return;
  257. printf("%s\n", offset->collection.title);
  258. offset = offset->next;
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement