Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 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->title);
  64. free(Book->author);
  65. free(Book->subject);
  66. free(Book);
  67. break;
  68.  
  69. case 2:
  70. Book = (book *)malloc(sizeof(book));
  71. Book->title = malloc(sizeof(CHAR_LIMIT));
  72. Book->author = malloc(sizeof(CHAR_LIMIT));
  73. Book->subject = malloc(sizeof(CHAR_LIMIT));
  74. head = deleteBook(head, Book, fp);
  75. free(Book->title);
  76. free(Book->author);
  77. free(Book->subject);
  78. free(Book);
  79. break;
  80.  
  81. case 3:
  82. Book = (book *)malloc(sizeof(book));
  83. Book->title = malloc(sizeof(CHAR_LIMIT));
  84. Book->author = malloc(sizeof(CHAR_LIMIT));
  85. Book->subject = malloc(sizeof(CHAR_LIMIT));
  86. fscanf(fp, "%s", Book->title);
  87. if ((searchForBook(head, Book->title)) != 0) {
  88. snprintf(out_msg, sizeof(out_msg), "The book %s is currently in the library.", Book->title);
  89. } else {
  90. snprintf(out_msg, sizeof(out_msg), "The book %s is NOT currently in the library.", Book->title);
  91. }
  92. puts(out_msg);
  93. writeToOutput(out_msg);
  94. free(Book->title);
  95. free(Book->author);
  96. free(Book->subject);
  97. free(Book);
  98. break;
  99.  
  100. // case 4:
  101. // case 5:
  102. }
  103. }
  104.  
  105.  
  106. fclose(fp);
  107.  
  108. return 0;
  109. }
  110.  
  111. /* function declaration */
  112.  
  113. void copybook(book *dest, book *source)
  114. {
  115. strcpy(dest->author, source->author);
  116. strcpy(dest->subject, source->subject);
  117. strcpy(dest->title, source->title);
  118. }
  119.  
  120. int searchForBook(library *list, char *detail)
  121. {
  122. library *offset = list;
  123. while (offset != NULL)
  124. {
  125. if (strcmp(offset->collection.title, detail) == 0)
  126. {
  127. return 1;
  128. } else {
  129. offset = offset->next;
  130. }
  131. }
  132. return 0;
  133. }
  134.  
  135. void writeToOutput(char *msg)
  136. {
  137. FILE *fp;
  138. if ((fp = fopen(OUTPUT_FILE, "a+")) == NULL)
  139. {
  140. fprintf(stderr, "Error: %s cannot be opened.\n", OUTPUT_FILE);
  141. }
  142. else
  143. {
  144. fprintf(fp, "%s\n", msg);
  145. fprintf(fp, "\n");
  146. fclose(fp);
  147. }
  148. }
  149.  
  150. library *addbook(library *list, book *Book, FILE *file) {
  151. //TODO: do character length checking if possible
  152. char out_msg[1024];
  153.  
  154. fscanf(file, "%s%s%s", Book->title, Book->author, Book->subject);
  155.  
  156. // check for duplicate first
  157. if ((searchForBook(list, Book->title)) == 0) {
  158. list = addToLibraryList(&list, Book);
  159. snprintf(out_msg, sizeof(out_msg), "The book %s has been added to the library.", Book->title);
  160. puts(out_msg);
  161. writeToOutput(out_msg);
  162. return list;
  163. } else {
  164. fprintf(stderr, "Book %s already exists!\n", Book->title);
  165. return NULL;
  166. }
  167. }
  168.  
  169. library *deleteBook(library *list, book *Book, FILE *file) {
  170. char out_msg[1024];
  171.  
  172. fscanf(file, "%s", Book->title);
  173. if ((list = (deleteFromLibraryList(list, Book->title))) != NULL)
  174. {
  175. snprintf(out_msg, sizeof(out_msg), "The book %s has been removed from the library.", Book->title);
  176. puts(out_msg);
  177. writeToOutput(out_msg);
  178. return list;
  179. } else {
  180. fprintf(stderr, "Book %s not found!", Book->title);
  181. return list;
  182. }
  183. }
  184.  
  185. /* linked-list functions */
  186. library *addToLibraryList(library **list, book *book_details)
  187. {
  188. library *new_book = (library *) malloc(sizeof(library));
  189. // new_book;
  190. // printf("%ld", sizeof(library));
  191. printf("%p\n", new_book);
  192.  
  193. if(new_book == NULL) {
  194. fprintf(stderr, "Memory allocation failed!\n");
  195. exit(EXIT_FAILURE);
  196. }
  197.  
  198. // new book details
  199. new_book->collection = *book_details;
  200. new_book->next = NULL; // prevent next from pointing to somewhere random
  201.  
  202. // first insertion when no books exist
  203. if(*list == NULL) {
  204. *list = new_book;
  205. (*list)->num_books++;
  206. printf("new linked list started\n");
  207.  
  208. return *list;
  209.  
  210. } else {
  211. library *offset = *list;
  212. for(; offset->next != NULL; offset = offset->next)
  213. ;
  214. (*list)->num_books++;
  215. offset->next = new_book;
  216. printf("added to the end of the list\n");
  217. }
  218. return *list;
  219. }
  220.  
  221. library *deleteFromLibraryList(library *list, char *details)
  222. {
  223. // 1. search for last and search for book to be deleted
  224. // 2. copy last book into first book
  225. // 3. free the last
  226. // 4. return smth to show sucess or failure
  227. library *search = NULL, *prev, *last;
  228.  
  229. for(last = list, prev = NULL;
  230. last->next != NULL;
  231. prev = last, last = last->next)
  232. ;
  233.  
  234.  
  235. search = list;
  236. // if first node
  237. if (strcmp(details, search->collection.title) == 0) {
  238. copybook(&(search->collection), &(last->collection));
  239. free(last);
  240. printf("first node detected for deletion\n");
  241. return list;
  242. }
  243.  
  244. // loop through the nodes
  245. while(search != NULL) {
  246. if(strcmp(search->collection.title, details) == 0) {
  247. printf("other nodes detected for deletion\n");
  248. break;
  249. }
  250. search = search->next;
  251. }
  252.  
  253. copybook(&(search->collection), &(last->collection));
  254. // prev->next = NULL;
  255. free(last);
  256. return list;
  257. }
  258.  
  259.  
  260. void searchAll(library *list)
  261. {
  262. library *offset = list;
  263. while(offset!= NULL) {
  264. // if (offset->next == NULL)
  265. // return;
  266. printf("%s\n", offset->collection.title);
  267. offset = offset->next;
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement