Advertisement
Guest User

Untitled

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