Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdbool.h>
- #define size 30
- typedef struct //definicja struktury ksiazki
- {
- char* title;
- char* author;
- int year;
- char* subject;
- int book_id;
- } book;
- book* createBook(char* title, char* author, int year, char* subject, int book_id) //tworzenie ksiazki do struktury
- {
- book* b = malloc(sizeof(book)); // book *b = malloc(sizeof(struct book));
- b->title = strdup(title);
- b->author = strdup(author);
- b->year = year;
- b->subject = strdup(subject);
- b->book_id = book_id;
- printf("ID:%i wczytana\n", book_id);
- return b;
- }
- typedef struct list //struktura listy
- {
- book* b;
- struct list* next;
- } list;
- int toint(char* s) //zamiana char to int
- {
- return atoi(s);
- }
- void printAllBook(list* head) //wypisanie ksiazek
- {
- while (head != NULL) {
- 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);
- head = head->next;
- }
- }
- list* SortedMerge(list* a, list* b, int num);
- void FrontBackSplit(list* source,
- list** frontRef, list** backRef);
- // sortuje liste zmieniajac wskazniki(next)
- void MergeSort(list** headRef, int num)
- {
- list* head = *headRef;
- list* a;
- list* b;
- //Rozmiar 0 lub 1
- if ((head == NULL) || (head->next == NULL)) {
- return;
- }
- // Dzieli heada na podlisty a i b
- FrontBackSplit(head, &a, &b);
- // Rekursywnie sortuje podlisty
- MergeSort(&a, num);
- MergeSort(&b, num);
- //laczy podlisty w jedna liste
- *headRef = SortedMerge(a, b, num);
- }
- list* SortedMerge(list* a, list* b, int num)
- {
- list* result = NULL;
- // sprawdza czy podlisty nie sa puste
- if (a == NULL)
- return (b);
- else if (b == NULL)
- return (a);
- //wybiera a lub b i sortuje zaleznie od parametru num
- if (num == 1) {
- //if (a->b->title <= b->b->title)
- if (strcmp(a->b->title, b->b->title) <= 0) {
- result = a;
- result->next = SortedMerge(a->next, b, num);
- }
- else {
- result = b;
- result->next = SortedMerge(a, b->next, num);
- }
- return (result);
- }
- else if (num == 2) {
- if (strcmp(a->b->author, b->b->author) <= 0) {
- result = a;
- result->next = SortedMerge(a->next, b, num);
- }
- else {
- result = b;
- result->next = SortedMerge(a, b->next, num);
- }
- return (result);
- }
- else if (num == 3) {
- if (a->b->year <= b->b->year) {
- result = a;
- result->next = SortedMerge(a->next, b, num);
- }
- else {
- result = b;
- result->next = SortedMerge(a, b->next, num);
- }
- return (result);
- }
- else if (num == 4) {
- if (strcmp(a->b->subject, b->b->subject) <= 0) {
- result = a;
- result->next = SortedMerge(a->next, b, num);
- }
- else {
- result = b;
- result->next = SortedMerge(a, b->next, num);
- }
- return (result);
- }
- else if (num == 5) {
- if (a->b->book_id <= b->b->book_id) {
- result = a;
- result->next = SortedMerge(a->next, b, num);
- }
- else {
- result = b;
- result->next = SortedMerge(a, b->next, num);
- }
- return (result);
- }
- }
- /* Dzieli lista na przednia i tylnia czesc,
- zwraca dwie listy przy pomocy referencji.
- Jezeli rozmiar listy jest nieparzysty, dodatkowy node trafi do przedniej listy.
- fast/slow pointer strategy
- */
- void FrontBackSplit(list* source,
- list** frontRef, list** backRef)
- {
- list* fast;
- list* slow;
- slow = source;
- fast = source->next;
- /* Bierze dwie "fast" nody i jedna "slow" node*/
- while (fast != NULL) {
- fast = fast->next;
- if (fast != NULL) {
- slow = slow->next;
- fast = fast->next;
- }
- }
- //Jezeli "slow" jest przed srodkiem listy, podziel ja na dwie w tym miejscu
- *frontRef = source;
- *backRef = slow->next;
- slow->next = NULL;
- }
- int check_similarity(const char* s, const char* t) //algorytm Levenshtein'a do sprawdzania podobienstwa
- {
- int ls = strlen(s), lt = strlen(t);
- int d[ls + 1][lt + 1];
- for (int i = 0; i <= ls; i++)
- for (int j = 0; j <= lt; j++)
- d[i][j] = -1;
- int dist(int i, int j)
- {
- if (d[i][j] >= 0)
- return d[i][j];
- int x;
- if (i == ls)
- x = lt - j;
- else if (j == lt)
- x = ls - i;
- else if (s[i] == t[j])
- x = dist(i + 1, j + 1);
- else {
- x = dist(i + 1, j + 1);
- int y;
- if ((y = dist(i, j + 1)) < x)
- x = y;
- if ((y = dist(i + 1, j)) < x)
- x = y;
- x++;
- }
- return d[i][j] = x;
- }
- return dist(0, 0); //zwraca ilosc operacji jaka trzeba wykonac zeby string s doprowadzic do stringa l
- }
- int check_unique_name(char* name, list* head) //sprawdza unikatowosc tytulu (0 unikatowy, 1 podobny)
- {
- while (head != NULL) {
- // printf("%i",check_similarity(head->b->title,name));
- //check_similarity(head->b->author,name);
- if (check_similarity(head->b->title, name) < 3 && strlen(name) > 5) //szukana wartosc
- {
- printf("Podobny rekord z id %i z tytulem %s\n", head->b->book_id, head->b->title);
- return 1;
- }
- else if (check_similarity(head->b->title, name) <= 1 && strlen(name) > 3) //szukana wartosc
- {
- printf("Podobny rekord z id %i z tytulem %s\n", head->b->book_id, head->b->title);
- return 1;
- }
- head = head->next;
- }
- // printf("Unikatowy rekord\n");
- return 0;
- }
- int check_unique_id(int id, list* head) //sprawdza unikatowosc id
- {
- while (head != NULL) {
- if (head->b->book_id == id) //szukana wartosc
- {
- printf("Juz istnieje rekord z id %i z tytulem %s\n", head->b->book_id, head->b->title);
- return 1;
- }
- head = head->next;
- }
- // printf("Unikatowe id\n");
- return 0;
- }
- book* getBook(list* books, int book_id) //funkcja przeszukujaca liste
- {
- list* l = books;
- while (l != NULL) {
- if (l->b->book_id == book_id) //szukana wartosc
- {
- return l->b;
- }
- l = l->next;
- }
- return NULL;
- }
- void* editBook(list* head, int id) //funkcja edytujaca element listy
- {
- list* books = head;
- char value[size];
- char edit[size];
- printf("Ktore pole chcesz zedytowac:\n");
- fgets(edit, size, stdin);
- if (strcmp(edit, "Nazwa\n") == 0) {
- printf("Podaj nowa nazwe:\n");
- fgets(value, size, stdin);
- while (books != NULL) {
- if (books->b->book_id == id) //szukana wartosc
- {
- books->b->title = strtok(value, "\n");
- }
- books = books->next;
- }
- }
- else if (strcmp(edit, "Autor\n") == 0) {
- printf("Podaj nowego autora:\n");
- fgets(value, size, stdin);
- while (books != NULL) {
- if (books->b->book_id == id) //szukana wartosc
- {
- books->b->author = strtok(value, "\n");
- }
- books = books->next;
- }
- }
- else if (strcmp(edit, "Rok\n") == 0) {
- printf("Podaj nowy rok:\n");
- fgets(value, size, stdin);
- while (books != NULL) {
- if (books->b->book_id == id) //szukana wartosc
- {
- books->b->year = toint(value);
- }
- books = books->next;
- }
- }
- else if (strcmp(edit, "Gatunek\n") == 0) {
- printf("Podaj nowy gatunek:\n");
- fgets(value, size, stdin);
- while (books != NULL) {
- if (books->b->book_id == id) //szukana wartosc
- {
- books->b->subject = strtok(value, "\n");
- }
- books = books->next;
- }
- }
- else {
- printf("Nie ma takiego pola\n");
- }
- printf("Edycja zakonczona\n");
- //return books;
- }
- void count_year_occurence(list* head) //liczy ile razy dany rok wystepuje w liscie
- {
- //head=list_sort_by_year(head);
- list* heads = head;
- int year;
- MergeSort(&heads, 3); //sortowanie wedlug roku
- while (heads != NULL) {
- if (heads->b->year == year) {
- heads = heads->next; //jezeli taki sam jak poprzedni to nie licz jeszcze raz
- continue;
- }
- year = heads->b->year;
- int count = 0;
- list* l = head;
- while (l != NULL) {
- if (l->b->year == year) //szukana wartosc
- {
- count += 1;
- }
- l = l->next;
- }
- printf("Wystepowanie roku %i - %i razy\n", heads->b->year, count);
- heads = heads->next;
- }
- }
- void count_author_occurence(list* head) //liczy ile ksiazek napisal dany autor
- {
- list* heads = head;
- char author[size];
- MergeSort(&heads, 2); //sortuj autorem
- while (heads != NULL) {
- if (strcmp(heads->b->author, author) == 0) {
- heads = heads->next;
- continue;
- }
- strncpy(author, heads->b->author, size);
- int count = 0;
- list* l = head;
- while (l != NULL) {
- if (strcmp(l->b->author, author) == 0) //szukana wartosc
- {
- count += 1;
- }
- l = l->next;
- }
- printf("Wystepowanie autora %s - %i razy\n", heads->b->author, count);
- heads = heads->next;
- }
- }
- void count_subject_occurence(list* head) //liczy ile razy wystepuje dany gatunek
- {
- list* heads = head;
- char subject[size];
- MergeSort(&heads, 4);
- while (heads != NULL) {
- if (strcmp(heads->b->subject, subject) == 0) {
- heads = heads->next;
- continue;
- }
- // author = heads->b->author;
- strncpy(subject, heads->b->subject, size);
- int count = 0;
- list* l = head;
- while (l != NULL) {
- if (strcmp(l->b->subject, subject) == 0) //szukana wartosc
- {
- count += 1;
- }
- l = l->next;
- }
- printf("Wystepowanie gatunku %s - %i razy\n", heads->b->subject, count);
- heads = heads->next;
- }
- }
- int count_books(list* head) //liczy ilosc elementow w liscie
- {
- //head=list_sort_by_year(head);
- int count = 0;
- while (head != NULL) {
- count += 1;
- head = head->next;
- }
- return count;
- }
- char* remove_extra_white_chars(char* temp) //usuwanie dodatkowych bialych znakow
- {
- char temp1[size];
- int j = 0;
- bool white = true;
- for (int i = 0; i < size; i++) {
- if (isspace(temp[i]) == 0) {
- temp1[j] = temp[i];
- white = false;
- j++;
- }
- else if (white == false) {
- temp1[j] = temp[i];
- j++;
- white = true;
- }
- }
- strncpy(temp, temp1, size); //do poprawy
- return temp;
- }
- char* remove_all_white_chars(char* temp) //usuwanie dodatkowych bialych znakow
- {
- char temp1[size];
- int j = 0;
- for (int i = 0; i < size; i++) {
- if (isspace(temp[i]) == 0) {
- temp1[j] = temp[i];
- j++;
- }
- }
- strncpy(temp, temp1, size); //do poprawy
- return temp;
- }
- list* ListDelete(list* currP, int book_id) //usuwa element z listy
- {
- if (currP == NULL)
- return NULL;
- if (currP->b->book_id == book_id) {
- list* tempNextP;
- tempNextP = currP->next;
- /* Dealokacja noda */
- free(currP);
- printf("\nUsunieto %i element", book_id);
- return tempNextP;
- }
- currP->next = ListDelete(currP->next, book_id);
- return currP;
- }
- list* add_book(list* head) //dodaje ksiazke do listy + sprawdzanie poprawnosci danych
- {
- char title[size];
- char author[size];
- char year[size];
- char subject[size];
- char id[size];
- int check_title = 1;
- int check_id = 1;
- while (check_title == 1 || toint(title) != 0) //sprawdzanie unikatowosci imienia
- {
- printf("Podaj tytul:\n");
- fgets(title, size, stdin);
- check_title = check_unique_name(strtok(title, "\n"), head);
- }
- while (toint(author) != 0) {
- printf("Podaj autora:");
- fgets(author, size, stdin);
- }
- while (toint(year) < 999 || toint(year) > 2030) {
- printf("Podaj rok:");
- fgets(year, size, stdin);
- }
- while (toint(subject) != 0) {
- printf("Podaj gatunek:");
- fgets(subject, size, stdin);
- }
- while (check_id == 1 || toint(id) <= 0) //sprawdzanie unikatowosci id
- {
- printf("Podaj id:");
- fgets(id, size, stdin);
- if (toint(id) > 0)
- check_id = check_unique_id(toint(id), head);
- }
- list* temps = (list*)malloc(sizeof(struct list));
- temps->b = createBook(strtok(title, "\n"), strtok(author, "\n"), toint(year), strtok(subject, "\n"), toint(id)); //strtok(subject,"\n") remove new line
- temps->next = head;
- head = temps;
- return head;
- }
- void find_print_books(list* head, int idpi, int idki, int yearpi, int yearki, char* autor, char* tytul)
- {
- while (head != NULL) { // printf("%s",autor);
- if ((strcmp(tytul, "-") == 1) && (strcmp(autor, "-") == 1)) {
- char* aut;
- // aut= strstr(head->b->author, autor);
- aut = strstr(head->b->title, autor);
- char* tit;
- tit = strstr(head->b->title, tytul);
- if ((head->b->book_id >= idpi && head->b->book_id <= idki) && (head->b->year >= yearpi && head->b->year <= yearki) && aut && tit) {
- 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);
- }
- }
- else if (strcmp(autor, "-") == 1) {
- char* aut;
- // aut= strstr(head->b->author, autor);
- aut = strstr(head->b->author, autor);
- if ((head->b->book_id >= idpi && head->b->book_id <= idki) && (head->b->year >= yearpi && head->b->year <= yearki) && aut) {
- 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);
- }
- }
- else if (strcmp(tytul, "-") == 1) {
- char* tit;
- tit = strstr(head->b->title, tytul);
- if ((head->b->book_id >= idpi && head->b->book_id <= idki) && (head->b->year >= yearpi && head->b->year <= yearki) && tit) {
- 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);
- }
- }
- else {
- if ((head->b->book_id >= idpi && head->b->book_id <= idki) && (head->b->year >= yearpi && head->b->year <= yearki)) {
- 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);
- }
- }
- head = head->next;
- }
- }
- void save_to_txt(list* head, char* file_name, int num) //zapis do pliku txt
- {
- FILE* s = fopen(file_name, "w"); //zapis do plika
- if (num == 1) {
- while (head != NULL) {
- 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);
- head = head->next;
- if (head != NULL)
- fprintf(s, "\n\n");
- }
- printf("Zapisano do txt\n");
- fclose(s);
- }
- else if (num == 2) {
- while (head != NULL) {
- 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);
- head = head->next;
- if (head != NULL)
- fprintf(s, "\n\n");
- }
- printf("Zapisano do txt\n");
- fclose(s);
- }
- else if (num == 3) {
- while (head != NULL) {
- 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);
- head = head->next;
- if (head != NULL)
- fprintf(s, "\n\n");
- }
- printf("Zapisano do txt\n");
- fclose(s);
- }
- else if (num == 4) {
- while (head != NULL) {
- 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);
- head = head->next;
- if (head != NULL)
- fprintf(s, "\n\n");
- }
- printf("Zapisano do txt\n");
- fclose(s);
- }
- else if (num == 5) {
- while (head != NULL) {
- 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);
- head = head->next;
- if (head != NULL)
- fprintf(s, "\n\n");
- }
- printf("Zapisano do txt\n");
- fclose(s);
- }
- }
- void save_to_json(list* head, char* file_name, int num) //funkcja odpowiadajaca za zapis do pliku w formacie json
- {
- FILE* s = fopen(file_name, "w"); //zapis do plika
- fprintf(s, "{\"Books\":[");
- if (num == 1) {
- while (head != NULL) {
- fprintf(s, "{");
- 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);
- head = head->next;
- if (head != NULL) {
- fprintf(s, ",");
- }
- }
- }
- if (num == 2) {
- while (head != NULL) {
- fprintf(s, "{");
- 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);
- head = head->next;
- if (head != NULL) {
- fprintf(s, ",");
- }
- }
- }
- if (num == 3) {
- while (head != NULL) {
- fprintf(s, "{");
- 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);
- head = head->next;
- if (head != NULL) {
- fprintf(s, ",");
- }
- }
- }
- if (num == 4) {
- while (head != NULL) {
- fprintf(s, "{");
- 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);
- head = head->next;
- if (head != NULL) {
- fprintf(s, ",");
- }
- }
- }
- if (num == 5) {
- while (head != NULL) {
- fprintf(s, "{");
- 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);
- head = head->next;
- if (head != NULL) {
- fprintf(s, ",");
- }
- }
- }
- fprintf(s, "]}");
- printf("Zapisano do JSON\n");
- fclose(s);
- }
- list* read_parse_file(list* head)
- { //funkcja odpowiadajaca za wczytanie pliku i zapisanie go w liscie
- FILE* f;
- char* filename = "xd.txt";
- char temp[size];
- int numer = 0; //numer elementu
- // list *books = NULL; //zdefiniowanie pustej listy
- // books = malloc(sizeof(book)); //przydzial pamieci na liste
- char title[size];
- char author[size];
- int year;
- char subject[size];
- int id;
- f = fopen(filename, "r");
- if (f == NULL) {
- printf("Nie mozna otworzyc pliku %s", filename);
- }
- while (fgets(temp, size, f) != NULL) {
- for (int i = 0;; i++) {
- if (temp[i] == '\0') //nowa linijka
- {
- numer++;
- break;
- }
- }
- if (numer % 6 != 0 && strcmp(temp, "\n") == 0) //eliminacja dodatkowych enterow
- {
- numer--;
- continue;
- }
- else if (numer % 6 != 0 && isspace(temp[0]) != 0) //eliminacja wszystkich bledow z pustymi liniami
- {
- char temp1[size];
- int j = 0;
- for (int i = 1; i < size; i++) {
- if (isspace(temp[i]) == 0 || j > 0) {
- temp1[j] = temp[i];
- j++;
- }
- }
- if (j != 0 && temp1[0] != '\0') {
- strncpy(temp, temp1, size);
- }
- else {
- numer--;
- continue;
- }
- }
- if (temp[0] >= 97 && temp[0] <= 122) {
- temp[0] = toupper(temp[0]);
- }
- switch (numer % 6) {
- case 1: {
- int check_title = check_unique_name(strtok(temp, "\n"), head);
- while (check_title == 1 || toint(temp) != 0) //sprawdzanie unikatowosci imienia
- {
- printf("Podaj unikatowy i poprawny tytul:\n");
- fgets(temp, size, stdin);
- check_title = check_unique_name(strtok(temp, "\n"), head);
- }
- strncpy(temp, remove_extra_white_chars(temp), size);
- strncpy(title, temp, size);
- } break;
- case 2:
- while (toint(temp) != 0) {
- printf("Podaj poprawnego autora %s\n", title);
- fgets(temp, size, stdin);
- }
- strncpy(temp, remove_extra_white_chars(temp), size);
- strncpy(author, temp, size);
- break;
- case 3:
- strncpy(temp, remove_all_white_chars(temp), size);
- while (toint(temp) < 999 || toint(temp) > 2030) {
- printf("Podaj poprawny rok dla %s\n", title);
- fgets(temp, size, stdin);
- }
- year = toint(temp);
- break;
- case 4:
- while (toint(temp) != 0) {
- printf("Podaj poprawnego gatunek %s\n", title);
- fgets(temp, size, stdin);
- }
- strncpy(temp, remove_extra_white_chars(temp), size);
- strncpy(subject, temp, size);
- break;
- case 5: {
- strncpy(temp, remove_all_white_chars(temp), size);
- int check_id = check_unique_id(toint(temp), head);
- while (check_id == 1 || toint(temp) <= 0) //sprawdzanie unikatowosci id
- {
- printf("Podaj poprawne id dla rekordu %s (proponowane %i)\n", title, (numer + 1) / 6);
- fgets(temp, size, stdin);
- if (toint(temp) > 0)
- check_id = check_unique_id(toint(temp), head);
- }
- id = toint(temp);
- list* temps = (list*)malloc(sizeof(struct list));
- temps->b = createBook(strtok(title, "\n"), strtok(author, "\n"), year, strtok(subject, "\n"), id); //strtok(subject,"\n") remove new line
- temps->next = head;
- head = temps;
- // books->b =createBook(title, author, year, subject, id);
- // books->next;
- break;
- }
- }
- // printf("%s", temp);
- }
- fclose(f);
- return head;
- }
- int main()
- {
- list* head = NULL;
- head = read_parse_file(head);
- char input[10] = "";
- printf("=============================================================================================\n");
- 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");
- while (1) { //polecenia od uzytkownika
- fgets(input, sizeof input, stdin);
- if (strcmp(input, "Add\n") == 0) {
- head = add_book(head);
- }
- else if (strcmp(input, "Check\n") == 0) {
- char nazwa[size] = "";
- int check = 1;
- while (check) {
- printf("Podaj nazwe do sprawdzenia:\n");
- fgets(nazwa, size, stdin);
- check = check_unique_name(strtok(nazwa, "\n"), head);
- //printf("%i",check_unique_name(nazwa,head));
- }
- }
- else if (strcmp(input, "Edit\n") == 0) {
- char idc[size] = "";
- while (toint(idc) <= 0) {
- printf("Podaj id do zedytowania:\n");
- fgets(idc, size, stdin);
- }
- int id = toint(idc);
- if (getBook(head, id)) //sprawdzenie czy element istnieje w liscie
- {
- book xd = *getBook(head, id);
- printf("Nazwa: %s\nAutor: %s\nRok: %i \nGatunek: %s\nId: %i\n", xd.title, xd.author, xd.year, xd.subject, xd.book_id);
- editBook(head, id);
- }
- else {
- printf("Nie znaleziono ksiazki z takim id");
- }
- }
- else if (strcmp(input, "Save\n") == 0) {
- char first[size] = "";
- while (toint(first) < 1 || toint(first) > 5) {
- printf("Podaj pierwsze miejsce przy zapisie i sortowaniu (1-tytul, 2-autor, 3-rok, 4-gatunek, 5-id)\n");
- fgets(first, size, stdin);
- }
- while (1) {
- char file_name[size] = "";
- printf("Podaj nazwe pliku do jakiego chcesz zapisac wraz z rozszerzeniem (.txt/.json):\n");
- fgets(file_name, size, stdin);
- strtok(file_name, "\n");
- char* dot = strrchr(file_name, '.'); //szuka ostatniej .
- if (dot && !strcmp(dot, ".txt")) //sprawdza czy istnieje kropka i po kropce wystepuje
- {
- MergeSort(&head, toint(first));
- save_to_txt(head, file_name, toint(first));
- break;
- }
- else if (dot && !strcmp(dot, ".json")) //sprawdza czy istnieje kropka i po kropce wystepuje
- {
- MergeSort(&head, toint(first));
- save_to_json(head, file_name, toint(first));
- break;
- }
- else {
- printf("Niepoprawne rozszerzenie pliku\n");
- }
- }
- }
- else if (strcmp(input, "Sort\n") == 0) {
- while (1) {
- printf("Wedlug czego chcesz sortowac: Tytul-1, Autor-2, Rok-3, Gatunek-4, Id-5\n");
- char sort[size] = "";
- fgets(sort, size, stdin);
- if (toint(sort) > 0 && toint(sort) <= 5) {
- MergeSort(&head, toint(sort));
- printAllBook(head);
- break;
- }
- }
- }
- else if (strcmp(input, "Stat\n") == 0) {
- printf("Ksiazek w bazie: %i\n", count_books(head));
- count_year_occurence(head);
- count_author_occurence(head);
- count_subject_occurence(head);
- MergeSort(&head, 5);
- }
- else if (strcmp(input, "Del\n") == 0) {
- char delinput[size] = "";
- while (toint(delinput) <= 0) {
- printf("Podaj id do usuniecia:\n");
- fgets(delinput, size, stdin);
- }
- head = ListDelete(head, toint(delinput));
- }
- else if (strcmp(input, "PrintAll\n") == 0)
- printAllBook(head);
- else if (strcmp(input, "Find\n") == 0) {
- char idp[size] = "";
- char idk[size] = "";
- while (toint(idp) <= 0) {
- printf("Podaj poczatek przedzialu id pliku do znalezienia:\n");
- fgets(idp, size, stdin);
- }
- while (toint(idk) <= 0) {
- printf("Podaj koniec przedzialu id pliku do znalezienia:\n");
- fgets(idk, size, stdin);
- }
- int idpi = toint(idp);
- int idki = toint(idk);
- if (idpi > idki) {
- int swap = idpi;
- idpi = idki;
- idpi = swap;
- printf("Zamieniono przedzialy id\n");
- }
- char yearp[size] = "";
- char yeark[size] = "";
- while (toint(yearp) <= 0) {
- printf("Podaj poczatek przedzialu lat pliku do znalezienia:\n");
- fgets(yearp, size, stdin);
- }
- while (toint(yeark) <= 0) {
- printf("Podaj koniec przedzialu lat pliku do znalezienia:\n");
- fgets(yeark, size, stdin);
- }
- int yearpi = toint(yearp);
- int yearki = toint(yeark);
- if (yearpi > yearki) {
- int swap = yearpi;
- yearpi = yearki;
- yearpi = swap;
- printf("Zamieniono przedzialy\n");
- }
- char autor[size] = "";
- printf("Imie/nazwisko lub ich fragenty dla szukanego autora (- brak)\n");
- fgets(autor, size, stdin);
- char tytul[size] = "";
- printf("Nazwa lub jej fragenty dla szukanej ksiazki (-brak)\n");
- fgets(tytul, size, stdin);
- find_print_books(head, idpi, idki, yearpi, yearki, strtok(autor, "\n"), strtok(tytul, "\n"));
- }
- else if (strcmp(input, "Help\n") == 0) {
- 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");
- }
- else if (strcmp(input, "Eqit\n") == 0) {
- printf("Eqiting\n");
- break;
- }
- else
- printf("Twoje polecenie jest inwalida\n");
- }
- printf("Dobranoc");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement