Advertisement
Guest User

Untitled

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