Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define _XOPEN_SOURCE
- #include <time.h>
- #include <math.h>
- #include <string.h>
- typedef struct person
- {
- char name[25];
- char surname[50];
- struct person* next;
- }author_t, borrower_t;
- typedef struct book
- {
- char title[50];
- int pages;
- int yearOfRelease;
- char date[12];
- struct person * borrower;
- struct person * author;
- struct book* next;
- }book_t;
- void loadFile(book_t *head)
- {
- char line[150];
- FILE *fp;
- fp = fopen("Books.csv", "r");
- if(fp==NULL)
- {
- printf("Nie ma takiego pliku.");
- exit(1);
- }
- else
- {
- int iterator = 0;
- while(fgets (line, 150, fp) != NULL)
- {
- loadLine(head, line, iterator);
- iterator++;
- //printf("%s", line);
- }
- fclose(fp);
- }
- }
- void loadLine (book_t * head, char line[], int iterator){
- char *tempName;
- char *tempSurname;
- int iter;
- if (iterator == 0) {
- iter = 0;
- strcpy (head->title, strtok (line, ";"));
- head->pages = atoi (strtok (NULL, ";"));
- head->yearOfRelease = atoi (strtok (NULL, ";"));
- head->borrower = (borrower_t *) malloc (sizeof (borrower_t));
- strcpy (head->borrower->name, strtok (NULL, ";"));
- strcpy (head->borrower->surname, strtok (NULL, ";"));
- strcpy (head->date, strtok (NULL, ";"));
- tempName = strtok (NULL, ";");
- tempSurname = strtok (NULL, ";");
- while (tempName != NULL && tempSurname != NULL){
- if (iter == 0){
- head->author = (author_t *) malloc (sizeof (author_t));
- strcpy (head->author->name, tempName);
- strcpy (head->author->surname, tempSurname);
- tempName = strtok (NULL, ";");
- tempSurname = strtok (NULL, ";");
- iter++;
- }
- else{
- author_t *current = head->author;
- while (current->next != NULL){
- current = current->next;
- }
- current->next = (author_t *) malloc (sizeof (author_t));
- strcpy (current->next->name, tempName);
- strcpy (current->next->surname, tempSurname);
- tempName = strtok (NULL, ";");
- tempSurname = strtok (NULL, ";");
- }
- }
- }
- else{
- iter = 0;
- book_t *current = head;
- while (current->next != NULL){
- current = current->next;
- }
- current->next = (book_t *) malloc (sizeof (book_t));
- strcpy (current->next->title, strtok (line, ";"));
- current->next->pages = atoi (strtok (NULL, ";"));
- current->next->yearOfRelease = atoi (strtok (NULL, ";"));
- current->next->borrower = (borrower_t *) malloc (sizeof (borrower_t));
- strcpy (current->next->borrower->name, strtok (NULL, ";"));
- strcpy (current->next->borrower->surname, strtok (NULL, ";"));
- strcpy (current->next->date, strtok (NULL, ";"));
- tempName= strtok (NULL, ";");
- tempSurname = strtok (NULL, ";");
- while (tempName != NULL && tempSurname != NULL){
- if (iter == 0){
- current->next->author = (author_t *) malloc (sizeof (author_t));
- strcpy (current->next->author->name, tempName);
- strcpy (current->next->author->surname, tempSurname);
- tempName = strtok (NULL, ";");
- tempSurname = strtok (NULL, ";");
- iter++;
- }
- else{
- author_t *current2 = current->next->author;
- while (current2->next != NULL){
- current2 = current2->next;
- }
- current2->next = (author_t *) malloc (sizeof (author_t));
- strcpy (current2->next->name, tempName);
- strcpy (current2->next->surname, tempSurname);
- tempName = strtok (NULL, ";");
- tempSurname = strtok (NULL, ";");
- }
- }
- }
- }
- void printList(book_t * head)
- {
- book_t * current = head;
- while(current != NULL)
- {
- printBook(current);
- current = current->next;
- }}
- void printBook(book_t *current){
- printf("Tytul: %s \n", current->title);
- printf("Autor/autorzy: ");
- author_t *curr_author = current->author;
- while (curr_author != NULL){
- printf("%s ", curr_author->name);
- printf("%s", curr_author->surname);
- if(!strstr(curr_author->surname, "\n")){
- printf("\n");
- }
- if (curr_author->next != NULL){
- printf(",");
- }
- curr_author = curr_author->next;
- }
- printf("Liczba stron: %d\n", current->pages);
- printf("Rok wydania: %d \n", current->yearOfRelease);
- printf("Data wypozyczenia (rrrr-mm-dd), jesli ksiazka nie jest wypozyczona to '0': %s\n", current->date);
- printf("Osoba wypozyczajaca, jesli ksiazka nie jest wypozyczona to '0': %s ", current->borrower->name);
- printf("%s ", current->borrower->surname);
- printf("\n");
- printf("---------------------------------------------------------------------------------------------");
- //printf("%d", moreThanHalfYear(current->date));
- printf("\n");
- }
- void printBorrowedBooks(book_t * head)
- {
- book_t * current = head;
- while(current != NULL)
- {
- if(isBorrowed(current)==1)
- {
- printBook(current);
- }
- current = current->next;
- }
- }
- int isBorrowed(book_t *current){
- if((strcasecmp(current->date, "0")!=0))
- {
- return 1;
- }
- return 0;
- }
- void booksOfBorrower(book_t * head)
- {
- char name[25];
- char surname[25];
- int choice;
- printf("Wedlug jakiej danej pozyczajacego chcesz przeszukac liste.\n");
- printf("Wpisz:\n");
- printf("1 Jesli chcesz przeszukac liste wedlug imienie.\n");
- printf("2 Jesli chcesz przeszukac liste wedlug nazwiska.\n");
- printf("3 Jesli chcesz przeszukac liste wedlug imienia i nazwiska.\n");
- scanf("%d", &choice);
- fflush(stdin);
- switch(choice)
- {
- case 1:
- printf("Podaj imie wypozyczajacego.\n");
- gets(name);
- book_t * current = head;
- while(current!=NULL)
- {
- while(current != NULL && strcasecmp(current->borrower->name, name)!=0)
- {
- current = current->next;
- }
- if(current!=NULL)
- {
- printBook(current);
- current = current->next;
- }
- }
- break;
- case 2:
- printf("Podaj nazwisko wypozyczajacego.\n");
- gets(surname);
- current = head;
- while(current!=NULL)
- {
- while(current != NULL && strcasecmp(current->borrower->surname, surname)!=0)
- {
- current = current->next;
- }
- if(current!=NULL)
- {
- printBook(current);
- current = current->next;
- }
- }
- break;
- case 3:
- printf("Podaj imie wypozyczajacego.\n");
- gets(name);
- printf("Podaj nazwisko wypozyczajacego.\n");
- gets(surname);
- current = head;
- while(current!=NULL)
- {
- while(current != NULL && strcasecmp(current->borrower->name, name)!=0 && strcasecmp(current->borrower->surname, surname)!=0)
- {
- current = current->next;
- }
- if(current!=NULL)
- {
- printBook(current);
- current = current->next;
- }
- }
- break;
- }
- }
- void addBook(book_t* head)
- {
- book_t *last = head;
- if (head == NULL)
- {
- head = last->next;
- return;
- }
- while (last->next != NULL)
- {
- last = last->next;
- }
- last->next = (book_t*)malloc(sizeof(book_t));
- last->next->author = (author_t*)malloc(sizeof(author_t));
- last->next->borrower = (borrower_t*)malloc(sizeof(borrower_t));
- printf("Podaj tytul nowej ksiazki: \n");
- gets(last->next->title);
- printf("Ilu ksiazka ma autorow?\n");
- //scanf("")
- printf("Podaj imie autora: \n");
- gets(last->next->author->name);
- printf("Podaj nazwisko autora: \n");
- gets(last->next->author->surname);
- printf("Podaj liczbe stron: \n");
- scanf("%d", &last->next->pages);
- printf("Podaj rok wydania: \n");
- scanf("%d", &last->next->yearOfRelease);
- printf("Podaj date wypozyczenia ksiazki (rrrr-mm-dd), jesli ksiazka nie jest wypozyczona, wpisz 0: \n");
- scanf("%s" , &last->next->date);
- printf("Podaj imie osoby wypozyczajacej, jesli brak to 0: \n");
- fflush(stdin);
- gets(last->next->borrower->name);
- printf("Podaj nazwisko osoby wypozyczajacej, jesli brak to 0: \n");
- gets(last->next->borrower->surname);
- last->next->next = NULL;
- return;
- }
- void deleteBook(book_t **head)
- {
- char titleToDelete[50];
- printf("Podaj tytul ksiazki, ktora chcesz usunac. \n");
- gets(titleToDelete);
- book_t * current = *head;
- book_t * tmp_book = NULL;
- if(strcasecmp(current->title, titleToDelete)==0){
- tmp_book = (*head)->next;
- free(*head);
- *head = tmp_book;
- } else {
- while(current->next!=NULL && strcasecmp(current->next->title, titleToDelete)!=0 ){
- current=current->next;
- }
- if(strcasecmp(current->next->title, titleToDelete)==0){
- tmp_book = current->next;
- current->next = tmp_book->next;
- free(tmp_book);
- }
- }
- }
- void findBookToEdit(book_t *head)
- {
- char titleToFind[50];
- printf("Podaj tytul ksiazki, ktor¹ chcesz edytowac\n");
- gets(titleToFind);
- book_t * current = head;
- while(current!=NULL && strcasecmp(current->title, titleToFind)!=0)
- {
- current = current->next;
- }
- int choice;
- printf("Wpisz:\n");
- printf("1 Jesli chcesz edytowac ogolne informacje o ksiazce.\n");
- printf("2 Jesli ktos zwraca ksiazke.\n");
- printf("3 Jesli ktos wypozycza ksiazke.\n");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- editBook(current);
- break;
- case 2:
- returnBook(current);
- break;
- case 3:
- wantToBorrow(current);
- break;
- }
- //printf("%s", current->title);
- }
- void editBook(book_t *current)
- {
- char newName[50];
- printf("Jaka informacje chcesz edytowac?");
- int choice;
- printf("Wpisz: \n");
- printf("1 Jesli chcesz edytowac tytul.\n");
- printf("2 Jesli chcesz edytowac imie autora.\n");
- printf("3 Jesli chcesz edytowac nazwisko autora.\n");
- printf("4 Jesli chcesz edytowac liczbe stron.\n");
- printf("5 Jesli chcesz edytowac date wydania.\n");
- scanf("%d", &choice);
- fflush(stdin);
- switch(choice)
- {
- case 1:
- printf("Podaj nowy tytul\n");
- gets(newName);
- strcpy(current->title, newName);
- break;
- case 2:
- printf("Podaj nowe imie autoran");
- gets(newName);
- strcpy(current->author->name, newName);
- break;
- case 3:
- printf("Podaj nowe nazwisko autora\n");
- gets(newName);
- strcpy(current->author->surname, newName);
- break;
- case 4:
- printf("Podaj nowa liczbe stron\n");
- scanf("%d", ¤t->pages);
- fflush(stdin);
- break;
- case 5:
- printf("Podaj nowa rok");
- scanf("%d", ¤t->yearOfRelease);
- break;
- }
- }
- void returnBook(book_t *current)
- {
- strcpy(current->date, "0");
- strcpy(current->borrower->name, "0");
- strcpy(current->borrower->surname, "0");
- printf("Ksizka wrocila do biblioteczki");
- }
- void wantToBorrow(book_t *current)
- {
- fflush(stdin);
- printf("Podaj date wypozyczenia ksiazki (rrrr-mm-dd).\n");
- gets(current->date);
- printf("Podaj imie osoby wypozyczajacej ksiazke.\n");
- gets(current->borrower->name);
- printf("Podaj nazwisko osoby wypozyczajacej ksiazke.\n");
- gets(current->borrower->surname);
- printf("Ksiazka znajduje sie u %s %s", current->borrower->name, current->borrower->surname);
- }
- void findBookBy(book_t *head)
- {
- char title[50];
- char name[25];
- char surname[25];
- int choice;
- printf("Wedlug jakiej danej chcesz wyszukac ksiazke? Wpisz:\n");
- printf("1 Jesli chcesz znalezc ksiazke wedlug tytulu.\n");
- printf("2 jesli chcesz znalezc ksiazke wedlug autora.\n");
- scanf("%d", &choice);
- fflush(stdin);
- switch(choice)
- {
- case 1:
- printf("Jakiego tytulu szukasz? \n");
- gets(name);
- book_t *current = head;
- while(current!=NULL)
- {
- while(current!=NULL && strcasecmp(current->title, name)!=0)
- {
- current = current->next;
- }
- if(current!=NULL)
- {
- printBook(current);
- current = current->next;
- }
- }
- break;
- case 2:
- printf("Podaj imie autora ksiazki.\n");
- gets(name);
- printf("Podaj nazwisko autora ksiazki.\n");
- gets(surname);
- current = head;
- while(current!=NULL)
- {
- while(current!=NULL && strcasecmp(current->author->name, name)!=0 && strcasecmp(current->author->surname, surname)!=0)
- {
- current = current->next;
- }
- if(current!=NULL)
- {
- printBook(current);
- current = current->next;
- }
- }
- break;
- }
- }
- void saveToFile (book_t * head){
- FILE *fp;
- fp = fopen ("Books.csv", "w");
- if(fp==NULL)
- {
- printf("Nie można zapisac zaaktualizowanej listy.\n");
- }
- book_t *current = head;
- while (current != NULL){
- fprintf (fp, "%s;", current->title);
- fprintf (fp, "%d;", current->pages);
- fprintf (fp, "%d;", current->yearOfRelease);
- fprintf (fp, "%s;", current->date);
- fprintf (fp, "%s;", current->borrower->name);
- fprintf (fp, "%s;", current->borrower->surname);
- author_t *curr_author = current->author;
- while (curr_author != NULL){
- fprintf (fp, "%s;", curr_author->name);
- fprintf (fp, "%s", curr_author->surname);
- if (curr_author->next != NULL){
- fprintf (fp, ";");
- }
- if(!strstr(current->author->surname, "\n")){
- fprintf(fp,"\n");
- }
- curr_author = curr_author->next;
- }
- current = current->next;
- }
- fclose (fp);
- }
- int main()
- {
- book_t *head = NULL;
- head = (book_t*)malloc(sizeof(book_t));
- int choice;
- loadFile(head);
- printf("***********************************DOMOWA BIBLIOTECZKA***********************************\n");
- printf("\n\n");
- while(1)
- {
- printf("Menu Glowne:\n");
- printf("Wybierz 1 jesli chcesz wyswietlic liste ksiazek.\n");
- printf("wybierz 2 jesli chcesz wyszukac danej ksiazki.\n");
- printf("Wybierz 3 jesli chcesz wyswietlic liste pozyczonych ksiazek.\n");
- printf("wybierz 4 jesli chcesz wyswietlic liste ksiazek pozyczonych poszczegolnym osobom.\n");
- printf("wybierz 5 jesli chcesz dodac nowa ksiazke do listy.\n");
- printf("wybierz 6 jesli chcesz usunac ksiazke z listy.\n");
- printf("wybierz 7 jesli chcesz edytowac informacje o ksiazce.\n");
- printf("wybierz 8 jesli chcesz zakonczyc korzystanie z programu\n");
- scanf("%d", &choice);
- fflush(stdin);
- switch(choice)
- {
- case 1:
- printList(head);
- break;
- case 2:
- findBookBy(head);
- break;
- case 3:
- printBorrowedBooks(head);
- break;
- case 4:
- booksOfBorrower(head);
- break;
- case 5:
- addBook(head);
- break;
- case 6:
- deleteBook(&head);
- break;
- case 7:
- findBookToEdit(head);
- break;
- case 8:
- saveToFile(head);
- exit(1);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement