Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 30.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <stdbool.h>
  6.  
  7. #define size 30
  8.  
  9. typedef struct //definicja struktury ksiazki
  10.     {
  11.     char* title;
  12.     char* author;
  13.     int year;
  14.     char* subject;
  15.     int book_id;
  16. } book;
  17.  
  18. book* createBook(char* title, char* author, int year, char* subject, int book_id) //tworzenie ksiazki do struktury
  19. {
  20.     book* b = malloc(sizeof(book)); // book *b = malloc(sizeof(struct book));
  21.     b->title = strdup(title);
  22.     b->author = strdup(author);
  23.     b->year = year;
  24.     b->subject = strdup(subject);
  25.     b->book_id = book_id;
  26.     printf("ID:%i wczytana\n", book_id);
  27.     return b;
  28. }
  29.  
  30. typedef struct list //struktura listy
  31.     {
  32.     book* b;
  33.     struct list* next;
  34. } list;
  35.  
  36. int toint(char* s) //zamiana char to int
  37. {
  38.     return atoi(s);
  39. }
  40.  
  41. void printAllBook(list* head) //wypisanie ksiazek
  42. {
  43.     while (head != NULL) {
  44.         printf("\nNazwa: %s\nAutor: %s\nRok: %i \nGatunek: %s\nId: %i\n", head->b->title, head->b->author, head->b->year, head->b->subject, head->b->book_id);
  45.         head = head->next;
  46.     }
  47. }
  48.  
  49. list* SortedMerge(list* a, list* b, int num);
  50. void FrontBackSplit(list* source,
  51.     list** frontRef, list** backRef);
  52.  
  53. // sortuje liste zmieniajac wskazniki(next)
  54. void MergeSort(list** headRef, int num)
  55. {
  56.     list* head = *headRef;
  57.     list* a;
  58.     list* b;
  59.  
  60.     //Rozmiar 0 lub 1
  61.     if ((head == NULL) || (head->next == NULL)) {
  62.         return;
  63.     }
  64.  
  65.     // Dzieli heada na podlisty a i b
  66.     FrontBackSplit(head, &a, &b);
  67.  
  68.     // Rekursywnie sortuje podlisty
  69.     MergeSort(&a, num);
  70.     MergeSort(&b, num);
  71.  
  72.     //laczy podlisty w jedna liste
  73.     *headRef = SortedMerge(a, b, num);
  74. }
  75.  
  76. list* SortedMerge(list* a, list* b, int num)
  77. {
  78.     list* result = NULL;
  79.  
  80.     // sprawdza czy podlisty nie sa puste
  81.     if (a == NULL)
  82.         return (b);
  83.     else if (b == NULL)
  84.         return (a);
  85.  
  86.     //wybiera a lub b i sortuje zaleznie od parametru num
  87.     if (num == 1) {
  88.         //if (a->b->title <= b->b->title)
  89.         if (strcmp(a->b->title, b->b->title) <= 0) {
  90.             result = a;
  91.             result->next = SortedMerge(a->next, b, num);
  92.         }
  93.         else {
  94.             result = b;
  95.             result->next = SortedMerge(a, b->next, num);
  96.         }
  97.         return (result);
  98.     }
  99.     else if (num == 2) {
  100.         if (strcmp(a->b->author, b->b->author) <= 0) {
  101.             result = a;
  102.             result->next = SortedMerge(a->next, b, num);
  103.         }
  104.         else {
  105.             result = b;
  106.             result->next = SortedMerge(a, b->next, num);
  107.         }
  108.         return (result);
  109.     }
  110.     else if (num == 3) {
  111.         if (a->b->year <= b->b->year) {
  112.             result = a;
  113.             result->next = SortedMerge(a->next, b, num);
  114.         }
  115.         else {
  116.             result = b;
  117.             result->next = SortedMerge(a, b->next, num);
  118.         }
  119.         return (result);
  120.     }
  121.     else if (num == 4) {
  122.         if (strcmp(a->b->subject, b->b->subject) <= 0) {
  123.             result = a;
  124.             result->next = SortedMerge(a->next, b, num);
  125.         }
  126.         else {
  127.             result = b;
  128.             result->next = SortedMerge(a, b->next, num);
  129.         }
  130.         return (result);
  131.     }
  132.     else if (num == 5) {
  133.         if (a->b->book_id <= b->b->book_id) {
  134.             result = a;
  135.             result->next = SortedMerge(a->next, b, num);
  136.         }
  137.         else {
  138.             result = b;
  139.             result->next = SortedMerge(a, b->next, num);
  140.         }
  141.         return (result);
  142.     }
  143. }
  144.  
  145. /* Dzieli lista na przednia i tylnia czesc,
  146.     zwraca dwie listy przy pomocy referencji.
  147.     Jezeli rozmiar listy jest nieparzysty, dodatkowy node trafi do przedniej listy.
  148.     fast/slow pointer strategy
  149.     */
  150.  
  151. void FrontBackSplit(list* source,
  152.     list** frontRef, list** backRef)
  153. {
  154.     list* fast;
  155.     list* slow;
  156.     slow = source;
  157.     fast = source->next;
  158.  
  159.     /* Bierze dwie "fast" nody i jedna "slow" node*/
  160.     while (fast != NULL) {
  161.         fast = fast->next;
  162.         if (fast != NULL) {
  163.             slow = slow->next;
  164.             fast = fast->next;
  165.         }
  166.     }
  167.     //Jezeli "slow" jest przed srodkiem listy, podziel ja na dwie w tym miejscu
  168.  
  169.     *frontRef = source;
  170.     *backRef = slow->next;
  171.     slow->next = NULL;
  172. }
  173.  
  174. int check_similarity(const char* s, const char* t) //algorytm Levenshtein'a do sprawdzania podobienstwa
  175. {
  176.     int ls = strlen(s), lt = strlen(t);
  177.     int d[ls + 1][lt + 1];
  178.  
  179.     for (int i = 0; i <= ls; i++)
  180.         for (int j = 0; j <= lt; j++)
  181.             d[i][j] = -1;
  182.  
  183.     int dist(int i, int j)
  184.     {
  185.         if (d[i][j] >= 0)
  186.             return d[i][j];
  187.  
  188.         int x;
  189.         if (i == ls)
  190.             x = lt - j;
  191.         else if (j == lt)
  192.             x = ls - i;
  193.         else if (s[i] == t[j])
  194.             x = dist(i + 1, j + 1);
  195.         else {
  196.             x = dist(i + 1, j + 1);
  197.  
  198.             int y;
  199.             if ((y = dist(i, j + 1)) < x)
  200.                 x = y;
  201.             if ((y = dist(i + 1, j)) < x)
  202.                 x = y;
  203.             x++;
  204.         }
  205.         return d[i][j] = x;
  206.     }
  207.     return dist(0, 0); //zwraca ilosc operacji jaka trzeba wykonac zeby string s doprowadzic do stringa l
  208. }
  209.  
  210. int check_unique_name(char* name, list* head) //sprawdza unikatowosc tytulu (0 unikatowy, 1 podobny)
  211. {
  212.  
  213.     while (head != NULL) {
  214.         // printf("%i",check_similarity(head->b->title,name));
  215.         //check_similarity(head->b->author,name);
  216.         if (check_similarity(head->b->title, name) < 3 && strlen(name) > 5) //szukana wartosc
  217.         {
  218.  
  219.             printf("Podobny rekord z id %i z tytulem %s\n", head->b->book_id, head->b->title);
  220.             return 1;
  221.         }
  222.         else if (check_similarity(head->b->title, name) <= 1 && strlen(name) > 3) //szukana wartosc
  223.         {
  224.             printf("Podobny rekord z id %i z tytulem %s\n", head->b->book_id, head->b->title);
  225.             return 1;
  226.         }
  227.         head = head->next;
  228.     }
  229.  
  230.     // printf("Unikatowy rekord\n");
  231.     return 0;
  232. }
  233.  
  234. int check_unique_id(int id, list* head) //sprawdza unikatowosc id
  235. {
  236.  
  237.     while (head != NULL) {
  238.  
  239.         if (head->b->book_id == id) //szukana wartosc
  240.         {
  241.  
  242.             printf("Juz istnieje rekord z id %i z tytulem %s\n", head->b->book_id, head->b->title);
  243.             return 1;
  244.         }
  245.  
  246.         head = head->next;
  247.     }
  248.  
  249.     // printf("Unikatowe id\n");
  250.     return 0;
  251. }
  252.  
  253. book* getBook(list* books, int book_id) //funkcja przeszukujaca liste
  254. {
  255.     list* l = books;
  256.  
  257.     while (l != NULL) {
  258.         if (l->b->book_id == book_id) //szukana wartosc
  259.         {
  260.             return l->b;
  261.         }
  262.         l = l->next;
  263.     }
  264.  
  265.     return NULL;
  266. }
  267.  
  268. void* editBook(list* head, int id) //funkcja edytujaca element listy
  269. {
  270.     list* books = head;
  271.     char value[size];
  272.     char edit[size];
  273.     printf("Ktore pole chcesz zedytowac:\n");
  274.     fgets(edit, size, stdin);
  275.  
  276.     if (strcmp(edit, "Nazwa\n") == 0) {
  277.         printf("Podaj nowa nazwe:\n");
  278.         fgets(value, size, stdin);
  279.         while (books != NULL) {
  280.             if (books->b->book_id == id) //szukana wartosc
  281.             {
  282.  
  283.                 books->b->title = strtok(value, "\n");
  284.             }
  285.             books = books->next;
  286.         }
  287.     }
  288.     else if (strcmp(edit, "Autor\n") == 0) {
  289.         printf("Podaj nowego autora:\n");
  290.         fgets(value, size, stdin);
  291.         while (books != NULL) {
  292.             if (books->b->book_id == id) //szukana wartosc
  293.             {
  294.                 books->b->author = strtok(value, "\n");
  295.             }
  296.             books = books->next;
  297.         }
  298.     }
  299.     else if (strcmp(edit, "Rok\n") == 0) {
  300.         printf("Podaj nowy rok:\n");
  301.         fgets(value, size, stdin);
  302.  
  303.         while (books != NULL) {
  304.             if (books->b->book_id == id) //szukana wartosc
  305.             {
  306.                 books->b->year = toint(value);
  307.             }
  308.             books = books->next;
  309.         }
  310.     }
  311.     else if (strcmp(edit, "Gatunek\n") == 0) {
  312.         printf("Podaj nowy gatunek:\n");
  313.         fgets(value, size, stdin);
  314.         while (books != NULL) {
  315.             if (books->b->book_id == id) //szukana wartosc
  316.             {
  317.                 books->b->subject = strtok(value, "\n");
  318.             }
  319.             books = books->next;
  320.         }
  321.     }
  322.     else {
  323.         printf("Nie ma takiego pola\n");
  324.     }
  325.     printf("Edycja zakonczona\n");
  326.  
  327.     //return books;
  328. }
  329.  
  330. void count_year_occurence(list* head) //liczy ile razy dany rok wystepuje w liscie
  331. {
  332.     //head=list_sort_by_year(head);
  333.     list* heads = head;
  334.     int year;
  335.     MergeSort(&heads, 3); //sortowanie wedlug roku
  336.     while (heads != NULL) {
  337.         if (heads->b->year == year) {
  338.             heads = heads->next; //jezeli taki sam jak poprzedni to nie licz jeszcze raz
  339.             continue;
  340.         }
  341.         year = heads->b->year;
  342.         int count = 0;
  343.  
  344.         list* l = head;
  345.         while (l != NULL) {
  346.             if (l->b->year == year) //szukana wartosc
  347.             {
  348.                 count += 1;
  349.             }
  350.             l = l->next;
  351.         }
  352.  
  353.         printf("Wystepowanie roku %i - %i razy\n", heads->b->year, count);
  354.         heads = heads->next;
  355.     }
  356. }
  357.  
  358. void count_author_occurence(list* head) //liczy ile ksiazek napisal dany autor
  359. {
  360.     list* heads = head;
  361.     char author[size];
  362.     MergeSort(&heads, 2); //sortuj autorem
  363.     while (heads != NULL) {
  364.         if (strcmp(heads->b->author, author) == 0) {
  365.             heads = heads->next;
  366.             continue;
  367.         }
  368.  
  369.         strncpy(author, heads->b->author, size);
  370.         int count = 0;
  371.  
  372.         list* l = head;
  373.         while (l != NULL) {
  374.             if (strcmp(l->b->author, author) == 0) //szukana wartosc
  375.             {
  376.                 count += 1;
  377.             }
  378.             l = l->next;
  379.         }
  380.  
  381.         printf("Wystepowanie autora %s - %i razy\n", heads->b->author, count);
  382.         heads = heads->next;
  383.     }
  384. }
  385.  
  386. void count_subject_occurence(list* head) //liczy ile razy wystepuje dany gatunek
  387. {
  388.     list* heads = head;
  389.     char subject[size];
  390.     MergeSort(&heads, 4);
  391.     while (heads != NULL) {
  392.         if (strcmp(heads->b->subject, subject) == 0) {
  393.             heads = heads->next;
  394.             continue;
  395.         }
  396.         // author = heads->b->author;
  397.         strncpy(subject, heads->b->subject, size);
  398.         int count = 0;
  399.  
  400.         list* l = head;
  401.         while (l != NULL) {
  402.             if (strcmp(l->b->subject, subject) == 0) //szukana wartosc
  403.             {
  404.                 count += 1;
  405.             }
  406.             l = l->next;
  407.         }
  408.  
  409.         printf("Wystepowanie gatunku %s - %i razy\n", heads->b->subject, count);
  410.         heads = heads->next;
  411.     }
  412. }
  413.  
  414. int count_books(list* head) //liczy ilosc elementow w liscie
  415. {
  416.     //head=list_sort_by_year(head);
  417.     int count = 0;
  418.     while (head != NULL) {
  419.  
  420.         count += 1;
  421.  
  422.         head = head->next;
  423.     }
  424.  
  425.     return count;
  426. }
  427.  
  428. char* remove_extra_white_chars(char* temp) //usuwanie dodatkowych bialych znakow
  429. {
  430.     char temp1[size];
  431.     int j = 0;
  432.     bool white = true;
  433.     for (int i = 0; i < size; i++) {
  434.         if (isspace(temp[i]) == 0) {
  435.             temp1[j] = temp[i];
  436.             white = false;
  437.  
  438.             j++;
  439.         }
  440.         else if (white == false) {
  441.             temp1[j] = temp[i];
  442.             j++;
  443.             white = true;
  444.         }
  445.     }
  446.  
  447.     strncpy(temp, temp1, size); //do poprawy
  448.     return temp;
  449. }
  450.  
  451. char* remove_all_white_chars(char* temp) //usuwanie dodatkowych bialych znakow
  452. {
  453.     char temp1[size];
  454.     int j = 0;
  455.  
  456.     for (int i = 0; i < size; i++) {
  457.         if (isspace(temp[i]) == 0) {
  458.             temp1[j] = temp[i];
  459.  
  460.             j++;
  461.         }
  462.     }
  463.  
  464.     strncpy(temp, temp1, size); //do poprawy
  465.     return temp;
  466. }
  467.  
  468. list* ListDelete(list* currP, int book_id) //usuwa element z listy
  469. {
  470.     if (currP == NULL)
  471.         return NULL;
  472.  
  473.     if (currP->b->book_id == book_id) {
  474.         list* tempNextP;
  475.  
  476.         tempNextP = currP->next;
  477.  
  478.         /* Dealokacja noda */
  479.         free(currP);
  480.         printf("\nUsunieto %i element", book_id);
  481.  
  482.         return tempNextP;
  483.     }
  484.  
  485.     currP->next = ListDelete(currP->next, book_id);
  486.  
  487.     return currP;
  488. }
  489.  
  490. list* add_book(list* head) //dodaje ksiazke do listy + sprawdzanie poprawnosci danych
  491. {
  492.  
  493.     char title[size];
  494.     char author[size];
  495.     char year[size];
  496.     char subject[size];
  497.     char id[size];
  498.     int check_title = 1;
  499.     int check_id = 1;
  500.     while (check_title == 1 || toint(title) != 0) //sprawdzanie unikatowosci imienia
  501.     {
  502.         printf("Podaj tytul:\n");
  503.         fgets(title, size, stdin);
  504.         check_title = check_unique_name(strtok(title, "\n"), head);
  505.     }
  506.     while (toint(author) != 0) {
  507.         printf("Podaj autora:");
  508.         fgets(author, size, stdin);
  509.     }
  510.     while (toint(year) < 999 || toint(year) > 2030) {
  511.         printf("Podaj rok:");
  512.         fgets(year, size, stdin);
  513.     }
  514.     while (toint(subject) != 0) {
  515.         printf("Podaj gatunek:");
  516.         fgets(subject, size, stdin);
  517.     }
  518.     while (check_id == 1 || toint(id) <= 0) //sprawdzanie unikatowosci id
  519.     {
  520.         printf("Podaj id:");
  521.         fgets(id, size, stdin);
  522.         if (toint(id) > 0)
  523.             check_id = check_unique_id(toint(id), head);
  524.     }
  525.  
  526.     list* temps = (list*)malloc(sizeof(struct list));
  527.     temps->b = createBook(strtok(title, "\n"), strtok(author, "\n"), toint(year), strtok(subject, "\n"), toint(id)); //strtok(subject,"\n") remove new line
  528.     temps->next = head;
  529.     head = temps;
  530.     return head;
  531. }
  532. void find_print_books(list* head, int idpi, int idki, int yearpi, int yearki, char* autor, char* tytul)
  533. {
  534.     while (head != NULL) { // printf("%s",autor);
  535.         if ((strcmp(tytul, "-") == 1) && (strcmp(autor, "-") == 1)) {
  536.             char* aut;
  537.             // aut= strstr(head->b->author, autor);
  538.             aut = strstr(head->b->title, autor);
  539.             char* tit;
  540.             tit = strstr(head->b->title, tytul);
  541.  
  542.             if ((head->b->book_id >= idpi && head->b->book_id <= idki) && (head->b->year >= yearpi && head->b->year <= yearki) && aut && tit) {
  543.                 printf("\nNazwa: %s\nAutor: %s\nRok: %i \nGatunek: %s\nId: %i\n", head->b->title, head->b->author, head->b->year, head->b->subject, head->b->book_id);
  544.             }
  545.         }
  546.         else if (strcmp(autor, "-") == 1) {
  547.             char* aut;
  548.             // aut= strstr(head->b->author, autor);
  549.             aut = strstr(head->b->author, autor);
  550.  
  551.             if ((head->b->book_id >= idpi && head->b->book_id <= idki) && (head->b->year >= yearpi && head->b->year <= yearki) && aut) {
  552.                 printf("\nNazwa: %s\nAutor: %s\nRok: %i \nGatunek: %s\nId: %i\n", head->b->title, head->b->author, head->b->year, head->b->subject, head->b->book_id);
  553.             }
  554.         }
  555.         else if (strcmp(tytul, "-") == 1) {
  556.  
  557.             char* tit;
  558.             tit = strstr(head->b->title, tytul);
  559.  
  560.             if ((head->b->book_id >= idpi && head->b->book_id <= idki) && (head->b->year >= yearpi && head->b->year <= yearki) && tit) {
  561.                 printf("\nNazwa: %s\nAutor: %s\nRok: %i \nGatunek: %s\nId: %i\n", head->b->title, head->b->author, head->b->year, head->b->subject, head->b->book_id);
  562.             }
  563.         }
  564.         else {
  565.  
  566.             if ((head->b->book_id >= idpi && head->b->book_id <= idki) && (head->b->year >= yearpi && head->b->year <= yearki)) {
  567.                 printf("\nNazwa: %s\nAutor: %s\nRok: %i \nGatunek: %s\nId: %i\n", head->b->title, head->b->author, head->b->year, head->b->subject, head->b->book_id);
  568.             }
  569.         }
  570.         head = head->next;
  571.     }
  572. }
  573.  
  574. void save_to_txt(list* head, char* file_name, int num) //zapis do pliku txt
  575. {
  576.  
  577.     FILE* s = fopen(file_name, "w"); //zapis do plika
  578.  
  579.     if (num == 1) {
  580.         while (head != NULL) {
  581.  
  582.             fprintf(s, "%s\n%s\n%i\n%s\n%i", head->b->title, head->b->author, head->b->year, head->b->subject, head->b->book_id);
  583.  
  584.             head = head->next;
  585.             if (head != NULL)
  586.                 fprintf(s, "\n\n");
  587.         }
  588.         printf("Zapisano do txt\n");
  589.         fclose(s);
  590.     }
  591.     else if (num == 2) {
  592.         while (head != NULL) {
  593.  
  594.             fprintf(s, "%s\n%s\n%i\n%s\n%i", head->b->author, head->b->title, head->b->year, head->b->subject, head->b->book_id);
  595.  
  596.             head = head->next;
  597.             if (head != NULL)
  598.                 fprintf(s, "\n\n");
  599.         }
  600.         printf("Zapisano do txt\n");
  601.         fclose(s);
  602.     }
  603.     else if (num == 3) {
  604.         while (head != NULL) {
  605.  
  606.             fprintf(s, "%i\n%s\n%s\n%s\n%i", head->b->year, head->b->title, head->b->author, head->b->subject, head->b->book_id);
  607.  
  608.             head = head->next;
  609.             if (head != NULL)
  610.                 fprintf(s, "\n\n");
  611.         }
  612.         printf("Zapisano do txt\n");
  613.         fclose(s);
  614.     }
  615.     else if (num == 4) {
  616.         while (head != NULL) {
  617.  
  618.             fprintf(s, "%s\n%s\n%i\n%s\n%i", head->b->subject, head->b->title, head->b->author, head->b->year, head->b->book_id);
  619.  
  620.             head = head->next;
  621.             if (head != NULL)
  622.                 fprintf(s, "\n\n");
  623.         }
  624.         printf("Zapisano do txt\n");
  625.         fclose(s);
  626.     }
  627.     else if (num == 5) {
  628.         while (head != NULL) {
  629.  
  630.             fprintf(s, "%i\n%s\n%s\n%i\n%s", head->b->book_id, head->b->title, head->b->author, head->b->year, head->b->subject);
  631.  
  632.             head = head->next;
  633.             if (head != NULL)
  634.                 fprintf(s, "\n\n");
  635.         }
  636.         printf("Zapisano do txt\n");
  637.         fclose(s);
  638.     }
  639. }
  640.  
  641. void save_to_json(list* head, char* file_name, int num) //funkcja odpowiadajaca za zapis do pliku w formacie json
  642. {
  643.  
  644.     FILE* s = fopen(file_name, "w"); //zapis do plika
  645.  
  646.     fprintf(s, "{\"Books\":[");
  647.     if (num == 1) {
  648.         while (head != NULL) {
  649.             fprintf(s, "{");
  650.             fprintf(s, "\"Title\": \"%s\",\"Author\": \"%s\",\"Year\": \"%i\",\"Subject\": \"%s\",\"Book_Id\": \"%i\"}", head->b->title, head->b->author, head->b->year, head->b->subject, head->b->book_id);
  651.  
  652.             head = head->next;
  653.             if (head != NULL) {
  654.                 fprintf(s, ",");
  655.             }
  656.         }
  657.     }
  658.     if (num == 2) {
  659.         while (head != NULL) {
  660.             fprintf(s, "{");
  661.             fprintf(s, "\"Author\": \"%s\",\"Title\": \"%s\",\"Year\": \"%i\",\"Subject\": \"%s\",\"Book_Id\": \"%i\"}", head->b->author, head->b->title, head->b->year, head->b->subject, head->b->book_id);
  662.  
  663.             head = head->next;
  664.             if (head != NULL) {
  665.                 fprintf(s, ",");
  666.             }
  667.         }
  668.     }
  669.     if (num == 3) {
  670.         while (head != NULL) {
  671.             fprintf(s, "{");
  672.             fprintf(s, "\"Year\": \"%i\",\"Title\": \"%s\",\"Author\": \"%s\",\"Subject\": \"%s\",\"Book_Id\": \"%i\"}", head->b->year, head->b->title, head->b->author, head->b->subject, head->b->book_id);
  673.  
  674.             head = head->next;
  675.             if (head != NULL) {
  676.                 fprintf(s, ",");
  677.             }
  678.         }
  679.     }
  680.     if (num == 4) {
  681.         while (head != NULL) {
  682.             fprintf(s, "{");
  683.             fprintf(s, "\"Subject\": \"%s\",\"Title\": \"%s\",\"Author\": \"%s\",\"Year\": \"%i\",\"Book_Id\": \"%i\"}", head->b->subject, head->b->title, head->b->author, head->b->year, head->b->book_id);
  684.  
  685.             head = head->next;
  686.             if (head != NULL) {
  687.                 fprintf(s, ",");
  688.             }
  689.         }
  690.     }
  691.     if (num == 5) {
  692.         while (head != NULL) {
  693.             fprintf(s, "{");
  694.             fprintf(s, "\"Book_Id\": \"%i\",\"Title\": \"%s\",\"Author\": \"%s\",\"Year\": \"%i\",\"Subject\": \"%s\"}", head->b->book_id, head->b->title, head->b->author, head->b->year, head->b->subject);
  695.  
  696.             head = head->next;
  697.             if (head != NULL) {
  698.                 fprintf(s, ",");
  699.             }
  700.         }
  701.     }
  702.     fprintf(s, "]}");
  703.     printf("Zapisano do JSON\n");
  704.     fclose(s);
  705. }
  706.  
  707. list* read_parse_file(list* head)
  708. { //funkcja odpowiadajaca za wczytanie pliku i zapisanie go w liscie
  709.     FILE* f;
  710.     char* filename = "xd.txt";
  711.  
  712.     char temp[size];
  713.     int numer = 0; //numer elementu
  714.  
  715.     // list *books = NULL;           //zdefiniowanie pustej listy
  716.     //  books = malloc(sizeof(book)); //przydzial pamieci na liste
  717.  
  718.     char title[size];
  719.     char author[size];
  720.     int year;
  721.     char subject[size];
  722.     int id;
  723.  
  724.     f = fopen(filename, "r");
  725.     if (f == NULL) {
  726.         printf("Nie mozna otworzyc pliku %s", filename);
  727.     }
  728.  
  729.     while (fgets(temp, size, f) != NULL) {
  730.  
  731.         for (int i = 0;; i++) {
  732.  
  733.             if (temp[i] == '\0') //nowa linijka
  734.             {
  735.                 numer++;
  736.                 break;
  737.             }
  738.         }
  739.  
  740.         if (numer % 6 != 0 && strcmp(temp, "\n") == 0) //eliminacja dodatkowych enterow
  741.         {
  742.             numer--;
  743.             continue;
  744.         }
  745.  
  746.         else if (numer % 6 != 0 && isspace(temp[0]) != 0) //eliminacja wszystkich bledow z pustymi liniami
  747.         {
  748.             char temp1[size];
  749.             int j = 0;
  750.  
  751.             for (int i = 1; i < size; i++) {
  752.                 if (isspace(temp[i]) == 0 || j > 0) {
  753.                     temp1[j] = temp[i];
  754.                     j++;
  755.                 }
  756.             }
  757.  
  758.             if (j != 0 && temp1[0] != '\0') {
  759.                 strncpy(temp, temp1, size);
  760.             }
  761.  
  762.             else {
  763.                 numer--;
  764.                 continue;
  765.             }
  766.         }
  767.  
  768.         if (temp[0] >= 97 && temp[0] <= 122) {
  769.             temp[0] = toupper(temp[0]);
  770.         }
  771.  
  772.         switch (numer % 6) {
  773.         case 1: {
  774.             int check_title = check_unique_name(strtok(temp, "\n"), head);
  775.  
  776.             while (check_title == 1 || toint(temp) != 0) //sprawdzanie unikatowosci imienia
  777.             {
  778.                 printf("Podaj unikatowy i poprawny tytul:\n");
  779.                 fgets(temp, size, stdin);
  780.                 check_title = check_unique_name(strtok(temp, "\n"), head);
  781.             }
  782.             strncpy(temp, remove_extra_white_chars(temp), size);
  783.             strncpy(title, temp, size);
  784.         } break;
  785.         case 2:
  786.             while (toint(temp) != 0) {
  787.                 printf("Podaj poprawnego autora %s\n", title);
  788.                 fgets(temp, size, stdin);
  789.             }
  790.             strncpy(temp, remove_extra_white_chars(temp), size);
  791.             strncpy(author, temp, size);
  792.  
  793.             break;
  794.         case 3:
  795.             strncpy(temp, remove_all_white_chars(temp), size);
  796.             while (toint(temp) < 999 || toint(temp) > 2030) {
  797.                 printf("Podaj poprawny rok dla %s\n", title);
  798.                 fgets(temp, size, stdin);
  799.             }
  800.             year = toint(temp);
  801.  
  802.             break;
  803.         case 4:
  804.             while (toint(temp) != 0) {
  805.                 printf("Podaj poprawnego gatunek %s\n", title);
  806.                 fgets(temp, size, stdin);
  807.             }
  808.             strncpy(temp, remove_extra_white_chars(temp), size);
  809.             strncpy(subject, temp, size);
  810.  
  811.             break;
  812.         case 5: {
  813.             strncpy(temp, remove_all_white_chars(temp), size);
  814.             int check_id = check_unique_id(toint(temp), head);
  815.             while (check_id == 1 || toint(temp) <= 0) //sprawdzanie unikatowosci id
  816.             {
  817.                 printf("Podaj poprawne id dla rekordu %s (proponowane %i)\n", title, (numer + 1) / 6);
  818.                 fgets(temp, size, stdin);
  819.                 if (toint(temp) > 0)
  820.                     check_id = check_unique_id(toint(temp), head);
  821.             }
  822.  
  823.             id = toint(temp);
  824.             list* temps = (list*)malloc(sizeof(struct list));
  825.             temps->b = createBook(strtok(title, "\n"), strtok(author, "\n"), year, strtok(subject, "\n"), id); //strtok(subject,"\n") remove new line
  826.             temps->next = head;
  827.             head = temps;
  828.             // books->b =createBook(title, author, year, subject, id);
  829.             //  books->next;
  830.             break;
  831.         }
  832.         }
  833.  
  834.         //   printf("%s", temp);
  835.     }
  836.  
  837.     fclose(f);
  838.     return head;
  839. }
  840.  
  841. int main()
  842. {
  843.     list* head = NULL;
  844.     head = read_parse_file(head);
  845.     char input[10] = "";
  846.     printf("=============================================================================================\n");
  847.     printf("Lista polecen: Eqit-wylacz, Add-dodaj rekord, Check-sprawdzanie czy rekord istnieje, Stat-wyswietlenie danych statystycznych, Sort-sortowanie, Save-zapisz do pliku, Del-usun konkretny, Find-znajdz konkretne, PrintAll-wypisz wszystkie, Help-lista polecen\n");
  848.  
  849.     while (1) { //polecenia od uzytkownika
  850.         fgets(input, sizeof input, stdin);
  851.  
  852.         if (strcmp(input, "Add\n") == 0) {
  853.             head = add_book(head);
  854.         }
  855.         else if (strcmp(input, "Check\n") == 0) {
  856.             char nazwa[size] = "";
  857.             int check = 1;
  858.             while (check) {
  859.                 printf("Podaj nazwe do sprawdzenia:\n");
  860.                 fgets(nazwa, size, stdin);
  861.                 check = check_unique_name(strtok(nazwa, "\n"), head);
  862.                 //printf("%i",check_unique_name(nazwa,head));
  863.             }
  864.         }
  865.         else if (strcmp(input, "Edit\n") == 0) {
  866.             char idc[size] = "";
  867.  
  868.             while (toint(idc) <= 0) {
  869.                 printf("Podaj id do zedytowania:\n");
  870.                 fgets(idc, size, stdin);
  871.             }
  872.  
  873.             int id = toint(idc);
  874.             if (getBook(head, id)) //sprawdzenie czy element istnieje w liscie
  875.             {
  876.                 book xd = *getBook(head, id);
  877.                 printf("Nazwa: %s\nAutor: %s\nRok: %i \nGatunek: %s\nId: %i\n", xd.title, xd.author, xd.year, xd.subject, xd.book_id);
  878.                 editBook(head, id);
  879.             }
  880.             else {
  881.                 printf("Nie znaleziono ksiazki z takim id");
  882.             }
  883.         }
  884.         else if (strcmp(input, "Save\n") == 0) {
  885.             char first[size] = "";
  886.  
  887.             while (toint(first) < 1 || toint(first) > 5) {
  888.                 printf("Podaj pierwsze miejsce przy zapisie i sortowaniu (1-tytul, 2-autor, 3-rok, 4-gatunek, 5-id)\n");
  889.                 fgets(first, size, stdin);
  890.             }
  891.             while (1) {
  892.                 char file_name[size] = "";
  893.                 printf("Podaj nazwe pliku do jakiego chcesz zapisac wraz z rozszerzeniem (.txt/.json):\n");
  894.                 fgets(file_name, size, stdin);
  895.                 strtok(file_name, "\n");
  896.                 char* dot = strrchr(file_name, '.'); //szuka ostatniej .
  897.                 if (dot && !strcmp(dot, ".txt")) //sprawdza czy istnieje kropka i po kropce wystepuje
  898.                 {
  899.                     MergeSort(&head, toint(first));
  900.                     save_to_txt(head, file_name, toint(first));
  901.                     break;
  902.                 }
  903.                 else if (dot && !strcmp(dot, ".json")) //sprawdza czy istnieje kropka i po kropce wystepuje
  904.                 {
  905.                     MergeSort(&head, toint(first));
  906.                     save_to_json(head, file_name, toint(first));
  907.                     break;
  908.                 }
  909.                 else {
  910.                     printf("Niepoprawne rozszerzenie pliku\n");
  911.                 }
  912.             }
  913.         }
  914.         else if (strcmp(input, "Sort\n") == 0) {
  915.             while (1) {
  916.                 printf("Wedlug czego chcesz sortowac: Tytul-1, Autor-2, Rok-3, Gatunek-4, Id-5\n");
  917.                 char sort[size] = "";
  918.                 fgets(sort, size, stdin);
  919.                 if (toint(sort) > 0 && toint(sort) <= 5) {
  920.                     MergeSort(&head, toint(sort));
  921.                     printAllBook(head);
  922.                     break;
  923.                 }
  924.             }
  925.         }
  926.         else if (strcmp(input, "Stat\n") == 0) {
  927.             printf("Ksiazek w bazie: %i\n", count_books(head));
  928.             count_year_occurence(head);
  929.             count_author_occurence(head);
  930.             count_subject_occurence(head);
  931.             MergeSort(&head, 5);
  932.         }
  933.         else if (strcmp(input, "Del\n") == 0) {
  934.             char delinput[size] = "";
  935.  
  936.             while (toint(delinput) <= 0) {
  937.                 printf("Podaj id do usuniecia:\n");
  938.                 fgets(delinput, size, stdin);
  939.             }
  940.  
  941.             head = ListDelete(head, toint(delinput));
  942.         }
  943.         else if (strcmp(input, "PrintAll\n") == 0)
  944.             printAllBook(head);
  945.  
  946.         else if (strcmp(input, "Find\n") == 0) {
  947.             char idp[size] = "";
  948.             char idk[size] = "";
  949.             while (toint(idp) <= 0) {
  950.                 printf("Podaj poczatek przedzialu id pliku do znalezienia:\n");
  951.                 fgets(idp, size, stdin);
  952.             }
  953.  
  954.             while (toint(idk) <= 0) {
  955.                 printf("Podaj koniec przedzialu id pliku do znalezienia:\n");
  956.                 fgets(idk, size, stdin);
  957.             }
  958.  
  959.             int idpi = toint(idp);
  960.             int idki = toint(idk);
  961.  
  962.             if (idpi > idki) {
  963.                 int swap = idpi;
  964.                 idpi = idki;
  965.                 idpi = swap;
  966.                 printf("Zamieniono przedzialy id\n");
  967.             }
  968.  
  969.             char yearp[size] = "";
  970.             char yeark[size] = "";
  971.             while (toint(yearp) <= 0) {
  972.                 printf("Podaj poczatek przedzialu lat pliku do znalezienia:\n");
  973.                 fgets(yearp, size, stdin);
  974.             }
  975.  
  976.             while (toint(yeark) <= 0) {
  977.                 printf("Podaj koniec przedzialu lat pliku do znalezienia:\n");
  978.                 fgets(yeark, size, stdin);
  979.             }
  980.  
  981.             int yearpi = toint(yearp);
  982.             int yearki = toint(yeark);
  983.  
  984.             if (yearpi > yearki) {
  985.                 int swap = yearpi;
  986.                 yearpi = yearki;
  987.                 yearpi = swap;
  988.                 printf("Zamieniono przedzialy\n");
  989.             }
  990.  
  991.             char autor[size] = "";
  992.             printf("Imie/nazwisko lub ich fragenty dla szukanego autora (- brak)\n");
  993.             fgets(autor, size, stdin);
  994.  
  995.             char tytul[size] = "";
  996.             printf("Nazwa lub jej fragenty dla szukanej ksiazki (-brak)\n");
  997.             fgets(tytul, size, stdin);
  998.  
  999.             find_print_books(head, idpi, idki, yearpi, yearki, strtok(autor, "\n"), strtok(tytul, "\n"));
  1000.         }
  1001.         else if (strcmp(input, "Help\n") == 0) {
  1002.             printf("Lista polecen: Eqit-wylacz, Add-dodaj rekord, Check-sprawdzanie czy rekord istnieje, Stat-wyswietlenie danych statystycznych, Sort-sortowanie, Save-zapisz do pliku, Del-usun konkretny, Find-znajdz konkretne, PrintAll-wypisz wszystkie, Help-lista polecen\n");
  1003.         }
  1004.         else if (strcmp(input, "Eqit\n") == 0) {
  1005.             printf("Eqiting\n");
  1006.             break;
  1007.         }
  1008.  
  1009.         else
  1010.             printf("Twoje polecenie jest inwalida\n");
  1011.     }
  1012.  
  1013.     printf("Dobranoc");
  1014.     return 0;
  1015. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement