Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.97 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. void addbook(library *thislib);
  20. int writeToLibrary(int, book *);
  21. void writeToOutput(char *);
  22. void addToLibraryList(library **, book *);
  23. library *searchForBook(library*, char *);
  24. int deleteFromLibraryList(library *, book *);
  25. void updateCounter(unsigned int);
  26.  
  27. // temp function
  28. void searchAll(library *);
  29.  
  30. #define CHAR_LIMIT 49
  31. #define LIBRARY_FILE "library.txt"
  32. #define OUTPUT_FILE "output.txt"
  33.  
  34. int main () {
  35. int choice, valid_input;
  36. unsigned int updates = 0;
  37.  
  38. // buffers
  39. char st[1024], title_input[100], author_input[100], subject_input[100], out_msg[1024];
  40.  
  41. book *bookptr;
  42.  
  43. library *head = (library * ) malloc(sizeof(library));
  44. head = NULL;
  45.  
  46. do {
  47. printf("? ");
  48. fgets(st, sizeof(st), stdin);
  49. valid_input = sscanf(st, "%d", &choice);
  50.  
  51. switch(choice) {
  52. case 1:
  53. // memory allocation
  54. bookptr = (book *)malloc(sizeof(book));
  55. bookptr->author = malloc(CHAR_LIMIT);
  56. bookptr->subject = malloc(CHAR_LIMIT);
  57. bookptr->title = malloc(CHAR_LIMIT);
  58.  
  59. while (1) {
  60. printf("Enter title: ");
  61. fgets(title_input, sizeof(title_input), stdin);
  62.  
  63. if (strlen(title_input) >= CHAR_LIMIT)
  64. {
  65. printf("Book title too long! \n");
  66. continue;
  67. // TODO: Check for existing title with else {}
  68. // better to check here
  69. }
  70.  
  71. printf("Enter author: ");
  72. fgets(author_input, sizeof(author_input), stdin);
  73. if (strlen(author_input) >= CHAR_LIMIT)
  74. {
  75. printf("Book author too long!\n");
  76. continue;
  77. }
  78.  
  79. printf("Enter subject: ");
  80. fgets(subject_input, sizeof(subject_input), stdin);
  81. if (strlen(subject_input) >= CHAR_LIMIT ){
  82. printf("Subject name too long!\n");
  83. continue;
  84. }
  85.  
  86. sscanf(title_input, "%s", bookptr->title);
  87.  
  88. /* checkk for duplicate */
  89. if ((searchForBook(head, bookptr->title)) != NULL)
  90. {
  91. fprintf(stderr, "Book %s already exists!\n", bookptr->title);
  92. // printf("Book %s already exists!\n", title_input);
  93. continue;
  94. }
  95.  
  96. sscanf(author_input, "%s", bookptr->author);
  97. sscanf(subject_input, "%s", bookptr->subject);
  98.  
  99. addToLibraryList(&head, bookptr);
  100. updateCounter(++updates);
  101.  
  102. if (writeToLibrary(choice, bookptr) != 0) {
  103. snprintf(out_msg, sizeof(out_msg),"The book %s has been added to the library.", bookptr->title);
  104. puts(out_msg);
  105. writeToOutput(out_msg);
  106. }
  107.  
  108. break;
  109. }
  110. choice = 0;
  111. break;
  112.  
  113. case 2:
  114. printf("Enter book title to be deleted: " );
  115. fgets(title_input, sizeof(title_input), stdin);
  116. sscanf(title_input, "%s", bookptr->title);
  117.  
  118. if ( (deleteFromLibraryList(head, bookptr)) > 0) {
  119. writeToLibrary(choice, bookptr);
  120. updateCounter(++updates);
  121. snprintf(out_msg, sizeof(out_msg), "The book %s has been removed from the library.", bookptr->title);
  122. puts(out_msg);
  123. writeToOutput(out_msg);
  124. } else {
  125. printf("Book %s not found!\n");
  126. }
  127.  
  128. choice = 0;
  129. break;
  130.  
  131. case 3:
  132. printf("Enter book title to be searched: ");
  133. fgets(title_input, sizeof(title_input), stdin);
  134. sscanf(title_input, "%s", bookptr->title);
  135.  
  136. if ((searchForBook(head, bookptr->title)) != NULL) {
  137. snprintf(out_msg, sizeof(out_msg), "The book %s is currently in the library.", bookptr->title);
  138. } else {
  139. snprintf(out_msg, sizeof(out_msg), "The book %s is NOT currently in the library.", bookptr->title);
  140. }
  141.  
  142. writeToLibrary(choice, bookptr);
  143. updateCounter(++updates);
  144. puts(out_msg);
  145. writeToOutput(out_msg);
  146.  
  147. choice = 0;
  148. break;
  149. case 4:
  150. searchAll(head);
  151. choice = 0;
  152. break;
  153. case 5:
  154. printf("pass5\n");
  155. choice = 0;
  156. break;
  157. default:
  158. printf("Wrong input!\n");
  159. }
  160. } while (valid_input < 1 || choice > 5 || choice < 1);
  161.  
  162.  
  163. return 0;
  164. }
  165.  
  166. /* function declaration */
  167. int writeToLibrary(int choice, book *details) {
  168. // add more parameters for flag to choose what to write
  169. FILE *fp;
  170. if((fp = fopen(LIBRARY_FILE, "a+" )) == NULL) {
  171. fprintf(stderr, "Error: %s cannot be opened.\n", LIBRARY_FILE);
  172. return 0;
  173.  
  174. } else {
  175. switch (choice) {
  176. case 1:
  177. if (fprintf(fp, "%d %12s %12s %12s\n", choice, details->title, details->author, details->subject) > 0)
  178. {
  179. fclose(fp);
  180. return 1;
  181. }
  182. break;
  183.  
  184. case 2:
  185. case 3:
  186. if (fprintf(fp, "%d %12s\n", choice, details->title) > 0)
  187. {
  188. fclose(fp);
  189. return 1;
  190. }
  191. break;
  192. }
  193.  
  194. }
  195. }
  196.  
  197. void writeToOutput(char *msg) {
  198. FILE *fp;
  199. if ((fp = fopen(OUTPUT_FILE, "a+")) == NULL) {
  200. fprintf(stderr, "Error: %s cannot be opened.\n", OUTPUT_FILE);
  201. } else {
  202. fprintf(fp, "%s\n", msg);
  203. fprintf(fp, "\n");
  204. fclose(fp);
  205. }
  206. }
  207.  
  208. void addToLibraryList(library **head, book *book_details) {
  209. library *new_book;
  210. new_book = malloc(sizeof(library));
  211.  
  212. if(new_book == NULL) {
  213. fprintf(stderr, "Memory allocation failed!\n");
  214. exit(EXIT_FAILURE);
  215. }
  216.  
  217. // new book details
  218. new_book->collection = *book_details;
  219. new_book->next = NULL; // prevent next from pointing to somewhere random
  220.  
  221. // first insertion when no books exist
  222. if(*head == NULL) {
  223. *head = new_book;
  224. (*head)->num_books++;
  225. printf("new linked list started\n");
  226. return;
  227.  
  228. } else {
  229. library *offset = *head;
  230. for(; offset->next != NULL; offset = offset->next)
  231. ;
  232. (*head)->num_books++;
  233. offset->next = new_book;
  234. printf("added to the end of the list\n");
  235. }
  236. return;
  237. }
  238.  
  239. library *searchForBook(library *list, char *detail) {
  240. // for (; (strcmp(offset->collection.title, detail)) != 0 && offset != NULL; offset = offset->next)
  241. // ;
  242.  
  243. // library *offset = list;
  244. while(list != NULL ) {
  245. if(strcmp(list->collection.title, detail) == 0) {
  246. return list;
  247. } else {
  248. list = list->next;
  249. }
  250. // list = list->next;
  251. }
  252. return NULL;
  253. // if(offset == NULL)
  254. // return NULL;
  255.  
  256. // if (offset->collection.title == detail) {
  257. // return offset;
  258. // }
  259.  
  260. // return searchForBook(offset->next, detail);
  261. }
  262.  
  263.  
  264. void copybook(book *dest, book *source) {
  265. dest->author = source->author;
  266. dest->subject = source->subject;
  267. dest->title = source->title;
  268. }
  269.  
  270. int deleteFromLibraryList(library *list, book *details) {
  271. // 1. search for last and search for book to be deleted
  272. // 2. copy last book into first book
  273. // 3. free the last
  274. // 4. return smth to show sucess or failure
  275. library *search = NULL, *prev, *last;
  276.  
  277. // for(;last->next != NULL; last = last->next)
  278. // ;
  279. /* search for last book */
  280. for(last = list, prev = NULL;
  281. last->next != NULL;
  282. prev = last, last = last->next)
  283. ;
  284.  
  285. // if( (search = searchForBook(list, details->title)) == NULL) {
  286. // return 0;
  287. // }
  288.  
  289. search = list;
  290. // if first node
  291. if (strcmp(details->title, search->collection.title) == 0) {
  292. copybook(&(search->collection), &(last->collection));
  293. free(last);
  294. printf("first node detected for deletion\n");
  295. return 1;
  296. }
  297.  
  298. // loop through the nodes
  299. while(search != NULL) {
  300. if(strcmp(search->collection.title, details->title) == 0) {
  301. printf("other nodes detected for deletion\n");
  302. break;
  303. }
  304. search = search->next;
  305. }
  306.  
  307. copybook(&(search->collection), &(last->collection));
  308. // prev->next = NULL;
  309. free(last);
  310. return 1;
  311. }
  312.  
  313. // int deleteFromLibraryList(library *list, book *details) {
  314. // library *last = list;
  315. // library *search = list, *prev;
  316.  
  317. // // search for the given book
  318. // if ((search = searchForBook(list, details->title)) == NULL) {
  319. // return 0;
  320. // }
  321.  
  322. // // move *last to last book/node
  323. // for(;last->next != NULL; last = last->next)
  324. // ;
  325.  
  326. // copybook(&(search->collection), &(last->collection));
  327. // free(last);
  328. // return 1;
  329. // }
  330.  
  331.  
  332.  
  333. void updateCounter(unsigned int updates) {
  334. // find 1st integer
  335. // overwrite
  336. // skip others using scanset
  337. FILE *fp;
  338.  
  339. if( (fp = fopen(LIBRARY_FILE, "r+")) == NULL ) {
  340. fprintf(stderr, "Error: %s cannot be opened.\n", OUTPUT_FILE);
  341. } else {
  342. if ((fgetc(fp)) > 0) {
  343. rewind(fp);
  344. fprintf(fp, "%d\n", updates);
  345. } else {
  346. fprintf(fp, "%d\n", updates);
  347.  
  348. }
  349. // fprintf(fp, "%d\n", updates);
  350. }
  351. fclose(fp);
  352. }
  353.  
  354. void searchAll(library *list) {
  355. library *offset = list;
  356. while(offset!= NULL) {
  357. // if (offset->next == NULL)
  358. // return;
  359. printf("%s\n", offset->collection.title);
  360. offset = offset->next;
  361. }
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement