Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define _XOPEN_SOURCE
  4. #include <time.h>
  5. #include <math.h>
  6. #include <string.h>
  7.  
  8. typedef struct person
  9. {
  10. char name[25];
  11. char surname[50];
  12. struct person* next;
  13. }author_t, borrower_t;
  14.  
  15. typedef struct book
  16. {
  17. char title[50];
  18. int pages;
  19. int yearOfRelease;
  20. char date[12];
  21. struct person * borrower;
  22. struct person * author;
  23. struct book* next;
  24. }book_t;
  25.  
  26. void loadLine(book_t * head, char line[], int iterator)
  27. {
  28. if(iterator==0)
  29. {
  30. strcpy(head->title, strtok(line, ";"));
  31. head->pages = atoi(strtok(NULL, ";"));
  32. head->yearOfRelease = atoi(strtok(NULL, ";")) ;
  33. strcpy(head->date, strtok(NULL, ";"));
  34.  
  35. head->borrower = (borrower_t*)malloc(sizeof(borrower_t));
  36.  
  37. strcpy(head->borrower->name, strtok(NULL, ";"));
  38. strcpy(head->borrower->surname, strtok(NULL, ";"));
  39.  
  40. head->author = (author_t*)malloc(sizeof(author_t));
  41.  
  42. strcpy(head->author->name, strtok(NULL, ";"));
  43. strcpy(head->author->surname, strtok(NULL, ";"));
  44.  
  45. head->next = NULL;
  46. }
  47. else
  48. {
  49. book_t * current = head;
  50.  
  51. while (current->next != NULL){
  52. current = current->next;
  53. }
  54. current->next = (book_t*)malloc(sizeof(book_t));
  55. strcpy(current->next->title, strtok(line, ";"));
  56. current->next->pages = atoi (strtok (NULL, ";"));
  57. current->next->yearOfRelease = atoi (strtok (NULL, ";"));
  58. strcpy(current->next->date, strtok(NULL, ";"));
  59.  
  60. current->next->borrower = (borrower_t*)malloc(sizeof(borrower_t));
  61.  
  62. strcpy(current->next->borrower->name, strtok(NULL, ";"));
  63. strcpy(current->next->borrower->surname, strtok(NULL, ";"));
  64.  
  65. current->next->author = (author_t*)malloc(sizeof(author_t));
  66.  
  67. strcpy(current->next->author->name, strtok(NULL, ";"));
  68. strcpy(current->next->author->surname, strtok(NULL, ";"));
  69.  
  70. current->next->next = NULL;
  71.  
  72. }
  73. }
  74.  
  75.  
  76. void printList(book_t * head)
  77. {
  78.  
  79.  
  80. book_t * current = head;
  81. while(current != NULL)
  82. {
  83.  
  84.  
  85.  
  86. printBook(current);
  87.  
  88.  
  89. current = current->next;
  90. }}
  91.  
  92.  
  93.  
  94. int isBorrowed(book_t *current){
  95. if((strcasecmp(current->date, "0")!=0))
  96. {
  97. return 1;
  98. }
  99.  
  100. return 0;
  101.  
  102. }
  103.  
  104. void printBorrowedBooks(book_t * head)
  105. {
  106. book_t * current = head;
  107. while(current != NULL)
  108. {
  109.  
  110. if(isBorrowed(current)==1)
  111. {
  112. printBook(current);
  113. }
  114. current = current->next;
  115. }
  116. }
  117.  
  118.  
  119. void printBook(book_t *current){
  120. printf("Tytul: %s \n", current->title);
  121. printf("Autor/autorzy: %s ", current->author->name);
  122. printf("%s", current->author->surname);
  123. if(current->next == NULL)
  124. {
  125. printf("\n");
  126. }
  127. printf("Liczba stron: %d\n", current->pages);
  128. printf("Rok wydania: %d \n", current->yearOfRelease);
  129. printf("Data wypozyczenia (rrrr-mm-dd), jesli ksiazka nie jest wypozyczona to '0': %s\n", current->date);
  130. printf("Osoba wypozyczajaca, jesli ksiazka nie jest wypozyczona to '0': %s ", current->borrower->name);
  131. printf("%s ", current->borrower->surname);
  132. printf("\n");
  133. printf("---------------------------------------------------------------------------------------------");
  134. //printf("%d", moreThanHalfYear(current->date));
  135. printf("\n");
  136. }
  137.  
  138. void loadFile(book_t *head)
  139. {
  140. char line[150];
  141. FILE *fp;
  142. fp = fopen("Books.csv", "r");
  143.  
  144. if(fp==NULL)
  145. {
  146. printf("Nie ma takiego pliku.");
  147. exit(1);
  148. }
  149. else
  150. {
  151. int iterator = 0;
  152. while(fgets (line, 150, fp) != NULL)
  153. {
  154. loadLine(head, line, iterator);
  155. iterator++;
  156. //printf("%s", line);
  157.  
  158. }
  159. fclose(fp);
  160. }
  161.  
  162. }
  163.  
  164.  
  165. void addBook(book_t* head)
  166. {
  167. book_t *last = head;
  168.  
  169. if (head == NULL)
  170. {
  171. head = last->next;
  172. return;
  173. }
  174.  
  175.  
  176. while (last->next != NULL)
  177. {
  178. last = last->next;
  179. }
  180.  
  181. last->next = (book_t*)malloc(sizeof(book_t));
  182.  
  183. last->next->author = (author_t*)malloc(sizeof(author_t));
  184.  
  185. last->next->borrower = (borrower_t*)malloc(sizeof(borrower_t));
  186.  
  187. printf("Podaj tytul nowej ksiazki: \n");
  188. gets(last->next->title);
  189. printf("Podaj imie autora: \n");
  190. gets(last->next->author->name);
  191. printf("Podaj nazwisko autora: \n");
  192. gets(last->next->author->surname);
  193. printf("Podaj liczbe stron: \n");
  194. scanf("%d", &last->next->pages);
  195. printf("Podaj rok wydania: \n");
  196. scanf("%d", &last->next->yearOfRelease);
  197. printf("Podaj date wypozyczenia ksiazki (rrrr-mm-dd), jesli ksiazka nie jest wypozyczona, wpisz 0: \n");
  198. scanf("%s" , &last->next->date);
  199. printf("Podaj imie osoby wypozyczajacej, jesli brak to 0: \n");
  200. fflush(stdin);
  201. gets(last->next->borrower->name);
  202. printf("Podaj nazwisko osoby wypozyczajacej, jesli brak to 0: \n");
  203. gets(last->next->borrower->surname);
  204. last->next->next = NULL;
  205. return;
  206. }
  207.  
  208. void deleteBook(book_t *head)
  209. {
  210. char titleToDelete[50];
  211. printf("Podaj tytul ksiazki, ktora chcesz usunac. \n");
  212. gets(titleToDelete);
  213.  
  214.  
  215. book_t *previous, *current;
  216.  
  217. while(head!=NULL && strcasecmp(head->title, titleToDelete)==0)
  218. {
  219. previous = head;
  220. *head = *head->next;
  221. free(previous);
  222. }
  223.  
  224. previous = head;
  225. current = head->next;
  226. while(current!=NULL)
  227. {
  228. if(strcasecmp(current->title, titleToDelete)==0)
  229. {
  230. previous->next= current->next;
  231. free(current);
  232. current=previous->next;
  233. }else
  234. {
  235. previous=current;
  236. current = current->next;
  237. }
  238. }
  239.  
  240. }
  241.  
  242. void findBookToEdit(book_t *head)
  243. {
  244. char titleToFind[50];
  245. printf("Podaj tytul ksiazki, ktor¹ chcesz edytowac\n");
  246. gets(titleToFind);
  247.  
  248. book_t * current = head;
  249. while(current!=NULL && strcasecmp(current->title, titleToFind)!=0)
  250. {
  251. current = current->next;
  252. }
  253. int choice;
  254. printf("Wpisz:\n");
  255. printf("1 Jesli chcesz edytowac ogolne informacje o ksiazce.\n");
  256. printf("2 Jesli ktos zwraca ksiazke.\n");
  257. printf("3 Jesli ktos wypozycza ksiazke.\n");
  258. scanf("%d", &choice);
  259.  
  260. switch (choice)
  261. {
  262. case 1:
  263. editBook(current);
  264. break;
  265. case 2:
  266. returnBook(current);
  267. break;
  268.  
  269. case 3:
  270. wantToBorrow(current);
  271. break;
  272. }
  273.  
  274. //printf("%s", current->title);
  275. }
  276.  
  277.  
  278. void editBook(book_t *current)
  279. {
  280. char newName[50];
  281. printf("Jaka informacje chcesz edytowac?");
  282. int choice;
  283. printf("Wpisz: \n");
  284. printf("1 Jesli chcesz edytowac tytul.\n");
  285. printf("2 Jesli chcesz edytowac imie autora.\n");
  286. printf("3 Jesli chcesz edytowac nazwisko autora.\n");
  287. printf("4 Jesli chcesz edytowac liczbe stron.\n");
  288. printf("5 Jesli chcesz edytowac date wydania.\n");
  289.  
  290.  
  291. scanf("%d", &choice);
  292. fflush(stdin);
  293. switch(choice)
  294. {
  295. case 1:
  296.  
  297. printf("Podaj nowy tytul\n");
  298. gets(newName);
  299. strcpy(current->title, newName);
  300. break;
  301.  
  302. case 2:
  303. printf("Podaj nowe imie autoran");
  304. gets(newName);
  305. strcpy(current->author->name, newName);
  306. break;
  307.  
  308. case 3:
  309. printf("Podaj nowe nazwisko autora\n");
  310. gets(newName);
  311. strcpy(current->author->surname, newName);
  312. break;
  313.  
  314. case 4:
  315. printf("Podaj nowa liczbe stron\n");
  316. scanf("%d", &current->pages);
  317. fflush(stdin);
  318. break;
  319. case 5:
  320. printf("Podaj nowa rok");
  321. scanf("%d", &current->yearOfRelease);
  322. break;
  323.  
  324. }
  325.  
  326. }
  327.  
  328.  
  329. void returnBook(book_t *current)
  330. {
  331. strcpy(current->date, "0");
  332. strcpy(current->borrower->name, "0");
  333. strcpy(current->borrower->surname, "0");
  334. printf("Ksizka wrocila do biblioteczki");
  335.  
  336. }
  337.  
  338. void wantToBorrow(book_t *current)
  339. {
  340. fflush(stdin);
  341. printf("Podaj date wypozyczenia ksiazki (rrrr-mm-dd).\n");
  342. gets(current->date);
  343. printf("Podaj imie osoby wypozyczajacej ksiazke.\n");
  344. gets(current->borrower->name);
  345. printf("Podaj nazwisko osoby wypozyczajacej ksiazke.\n");
  346. gets(current->borrower->surname);
  347. printf("Ksiazka znajduje sie u %s %s", current->borrower->name, current->borrower->surname);
  348.  
  349. }
  350.  
  351. void findBookBy(book_t *head)
  352. {
  353. char title[50];
  354. char name[25];
  355. char surname[25];
  356. int choice;
  357. printf("Wedlug jakiej danej chcesz wyszukac ksiazke? Wpisz:\n");
  358. printf("1 Jesli chcesz znalezc ksiazke wedlug tytulu.\n");
  359. printf("2 jesli chcesz znalezc ksiazke wedlug autora.\n");
  360. scanf("%d", &choice);
  361. fflush(stdin);
  362.  
  363.  
  364. switch(choice)
  365. {
  366. case 1:
  367.  
  368. printf("Jakiego tytulu szukasz? \n");
  369. gets(name);
  370. book_t *current = head;
  371. while(current!=NULL)
  372. {
  373. while(current!=NULL && strcasecmp(current->title, name)!=0)
  374. {
  375. current = current->next;
  376. }
  377. if(current!=NULL)
  378. {
  379. printBook(current);
  380. current = current->next;
  381. }
  382. }
  383.  
  384. break;
  385.  
  386. case 2:
  387. printf("Podaj imie autora ksiazki.\n");
  388. gets(name);
  389. printf("Podaj nazwisko autora ksiazki.\n");
  390. gets(surname);
  391.  
  392. current = head;
  393. while(current!=NULL)
  394. {
  395. while(current!=NULL && strcasecmp(current->author->name, name)!=0 && strcasecmp(current->author->surname, surname)!=0)
  396. {
  397. current = current->next;
  398. }
  399.  
  400. if(current!=NULL)
  401. {
  402. printBook(current);
  403. current = current->next;
  404. }
  405. }
  406.  
  407. break;
  408. }
  409. }
  410.  
  411. void booksOfBorrower(book_t * head)
  412. {
  413. char name[25];
  414. char surname[25];
  415. int choice;
  416. printf("Wedlug jakiej danej pozyczajacego chcesz przeszukac liste.\n");
  417. printf("Wpisz:\n");
  418. printf("1 Jesli chcesz przeszukac liste wedlug imienie.\n");
  419. printf("2 Jesli chcesz przeszukac liste wedlug nazwiska.\n");
  420. printf("3 Jesli chcesz przeszukac liste wedlug imienia i nazwiska.\n");
  421. scanf("%d", &choice);
  422. fflush(stdin);
  423.  
  424.  
  425. switch(choice)
  426. {
  427. case 1:
  428. printf("Podaj imie wypozyczajacego.\n");
  429. gets(name);
  430. book_t * current = head;
  431. while(current!=NULL)
  432. {
  433. while(current != NULL && strcasecmp(current->borrower->name, name)!=0)
  434. {
  435.  
  436. current = current->next;
  437. }
  438. if(current!=NULL)
  439. {
  440. printBook(current);
  441. current = current->next;
  442. }
  443. }
  444. break;
  445.  
  446. case 2:
  447. printf("Podaj nazwisko wypozyczajacego.\n");
  448. gets(surname);
  449. current = head;
  450.  
  451. while(current!=NULL)
  452. {
  453. while(current != NULL && strcasecmp(current->borrower->surname, surname)!=0)
  454. {
  455.  
  456. current = current->next;
  457. }
  458. if(current!=NULL)
  459. {
  460. printBook(current);
  461. current = current->next;
  462. }
  463. }
  464.  
  465.  
  466. break;
  467. case 3:
  468. printf("Podaj imie wypozyczajacego.\n");
  469. gets(name);
  470. printf("Podaj nazwisko wypozyczajacego.\n");
  471. gets(surname);
  472. current = head;
  473.  
  474. while(current!=NULL)
  475. {
  476. while(current != NULL && strcasecmp(current->borrower->name, name)!=0 && strcasecmp(current->borrower->surname, surname)!=0)
  477. {
  478.  
  479. current = current->next;
  480. }
  481. if(current!=NULL)
  482. {
  483. printBook(current);
  484. current = current->next;
  485. }
  486. }
  487. break;
  488. }
  489. }
  490.  
  491.  
  492. void saveToFile (book_t * head){
  493. FILE *fp;
  494. fp = fopen ("Books.csv", "w");
  495. if(fp==NULL)
  496. {
  497. printf("Nie można zapisac zaaktualizowanej listy.\n");
  498. }
  499. book_t *current = head;
  500.  
  501. while (current != NULL){
  502.  
  503. fprintf (fp, "%s;", current->title);
  504. fprintf (fp, "%d;", current->pages);
  505. fprintf (fp, "%d;", current->yearOfRelease);
  506. fprintf (fp, "%s;", current->date);
  507. fprintf (fp, "%s;", current->borrower->name);
  508. fprintf (fp, "%s;", current->borrower->surname);
  509. author_t *curr_author = current->author;
  510. while (curr_author != NULL){
  511. fprintf (fp, "%s;", curr_author->name);
  512. fprintf (fp, "%s", curr_author->surname);
  513. if (curr_author->next != NULL){
  514. fprintf (fp, ";");
  515. }
  516. curr_author = curr_author->next;
  517. }
  518. current = current->next;
  519. }
  520.  
  521.  
  522. fclose (fp);
  523. }
  524.  
  525.  
  526. int main()
  527. {
  528. book_t *head = NULL;
  529. head = (book_t*)malloc(sizeof(book_t));
  530. int choice;
  531. loadFile(head);
  532.  
  533. printf("***********************************DOMOWA BIBLIOTECZKA***********************************\n");
  534. printf("\n\n");
  535.  
  536. while(1)
  537. {
  538. printf("Menu Glowne:\n");
  539. printf("Wybierz 1 jesli chcesz wyswietlic liste ksiazek.\n");
  540. printf("wybierz 2 jesli chcesz wyszukac danej ksiazki.\n");
  541. printf("Wybierz 3 jesli chcesz wyswietlic liste pozyczonych ksiazek.\n");
  542. printf("wybierz 4 jesli chcesz wyswietlic liste ksiazek pozyczonych poszczegolnym osobom.\n");
  543. printf("wybierz 5 jesli chcesz dodac nowa ksiazke do listy.\n");
  544. printf("wybierz 6 jesli chcesz usunac ksiazke z listy.\n");
  545. printf("wybierz 7 jesli chcesz edytowac informacje o ksiazce.\n");
  546. printf("wybierz 8 jesli chcesz zakonczyc korzystanie z programu\n");
  547. scanf("%d", &choice);
  548. fflush(stdin);
  549.  
  550.  
  551. switch(choice)
  552. {
  553. case 1:
  554. printList(head);
  555. break;
  556.  
  557. case 2:
  558. findBookBy(head);
  559. break;
  560. case 3:
  561. printBorrowedBooks(head);
  562. break;
  563. case 4:
  564. booksOfBorrower(head);
  565. break;
  566. case 5:
  567. addBook(head);
  568. break;
  569. case 6:
  570. deleteBook(head);
  571. break;
  572. case 7:
  573. findBookToEdit(head);
  574. break;
  575. case 8:
  576. saveToFile(head);
  577. exit(1);
  578. }
  579. }
  580.  
  581.  
  582. return 0;
  583. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement