Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 41
- #include <unistd.h> //liczba znaków w tablicy
- struct kat
- {
- char autor[MAX];
- char tytul[MAX];
- char wydawnictwo[MAX];
- int rok;
- int stron;
- int numer;
- struct kat* up;
- struct kat* down;
- };
- typedef struct kat kat;
- void print(kat* head);//wyświetla to co jest do tej pory
- void options();//wyswietla dostępne opcje
- void clearBUF(void);//czysci bufor
- void case1(char autor[], char tytul[], char wydawnictwo[], int* rok, int* stron, int* numer);
- void insertfront(kat** head,kat** tail,char autor[], char tytul[], char wydawnictwo[], int rok, int stron, int numer);//dodaje na przód
- void vanishall(kat **head);//usuwa całość
- void vanishfront(kat**head);//usuwa przód
- void vanishrear(kat **head);//usuwa tył
- void vanishindex(kat**head, int index, kat **tail);
- void writefile(char *filename,kat *head);
- void importfile( kat *head, kat *tail);
- void sort(kat *head);
- void swap(kat *i, kat *j);
- int main(void)
- {
- char autor[MAX];
- char tytul[MAX];
- char wydawnictwo[MAX];
- int rok;
- int stron;
- int numer;
- kat* head = NULL;
- kat* tail = NULL;
- int option=-1; //potrzebny do switch
- int index=-1 ;
- int x; //zabezpiecza opcje
- char str[100]; //nazwa pliku
- while(option!=0)
- {
- system("clear"); //czysci ekran
- printf("\nAktualny stan listy: \n");
- print(head);
- options(); // wyświetla dostępne możliwości
- x=scanf("%d", &option);
- while(x!=1 || option<1 || option >6 )
- {
- clearBUF();
- puts("Panie od 0 do 6");
- x=scanf("%d", &option);
- }
- switch (option)
- {
- case 0:
- return 0;
- break;
- case 1:
- clearBUF();
- case1( autor, tytul, wydawnictwo, &rok, &stron, &numer);
- insertfront(&head,&tail, autor, tytul, wydawnictwo,rok, stron,numer ); //adres do wskaznika daje &
- break;
- case 2:
- clearBUF();
- vanishindex(&head,index,&tail);
- break;
- case 3:
- clearBUF();
- vanishall(&head);
- break;
- case 4:
- clearBUF();
- printf("\n Nazwij plik:");
- gets(str);
- writefile(str,head);
- break;
- case 5:
- clearBUF();
- importfile(head,tail);
- sleep(2);
- break;
- case 6:
- clearBUF();
- if(head==NULL){
- puts("HOLA HOLA");
- sleep(1);
- }
- else
- sort(head);
- break;
- }
- }
- return 0;
- }
- void insertfront(kat** head,kat** tail,char autor[], char tytul[], char wydawnictwo[], int rok, int stron, int numer)
- {
- kat *current = malloc(sizeof (kat));
- strcpy(current->tytul, tytul);
- strcpy(current->autor, autor);
- strcpy(current->wydawnictwo,wydawnictwo);
- current->rok=rok;
- current->stron=stron;
- current->numer=numer;
- if (*head==NULL)
- {
- *head=(*tail)=current;
- current->down=current->up=NULL;
- }
- else
- {
- current->down=NULL;
- current->up=(*tail);
- (*tail)->down=current;
- (*tail)=current;
- }
- }
- void print (kat* head)
- {
- int i=1;
- printf("\n");
- if(head==NULL)
- printf("Katalog jest pusty");
- else
- {
- do
- {
- printf("\n%d. Autor: %10s tytul: %10s Wydawnictwo: %10s Rok: %5d Stron: %5d Numer: %5d\t ",
- i,head->autor, head->tytul, head->wydawnictwo, head->rok, head->stron, head->numer );
- head = head->down;
- ++i;
- }
- while(head !=NULL);
- }
- }
- void vanishfront(kat **head)
- {
- if (*head!=NULL)
- {
- if((*head)->down==NULL)
- {
- *head=NULL;
- }
- else
- {
- kat *tmp;
- tmp=(*head)->down;
- free(*head);
- *head=tmp;
- (*head)->up=NULL;
- }
- }
- }
- void vanishall(kat **head)
- {
- if (*head!=NULL)
- {
- if((*head)->up==NULL)
- {
- *head=NULL;
- }
- else
- {
- kat *tmp;
- tmp=(*head)->up;
- free(*head);
- *head=tmp;
- (*head)->down=NULL;
- }
- }
- }
- void options()
- {
- printf("\n\n\nDrogi uzytkowniku! Co chcesz zrobic?\n\n");
- printf("[1] Dodac ksiazke na koniec listy\n");
- printf("[2] Usunac ksiazke o wybranym indeksie\n");
- printf("[3] excute order 66\n");
- printf("[4] Zapisac plik \n");
- printf("[5] Wyswietlic ksiazki z pliku\n");
- printf("[6] Posortowac ksiazki \n");
- printf("[0] Zakonczyc program\n");
- }
- void clearBUF(void)
- {
- char c;
- do
- c = getchar();
- while ( c != '\n' && c != EOF);
- }
- void case1(char autor[], char tytul[], char wydawnictwo[], int* rok, int* stron, int* numer)
- {
- int q; //zabezpiecza rok
- int w; //zabezpiecza stron
- int e; //zabezpiecza numer
- printf("Podaj autora \t"); //Jak robisz scanfa z tablicamiTo bez &To wynika z tego ze & bierze adres A tablica sama w soboe jest adresem pierwszego jej elementu
- fgets(autor, MAX, stdin);
- autor[strlen(autor) - 1] = '\0'; // by enter nie wchodzil dalej
- printf("Podaj tytul \t");
- fgets(tytul, MAX, stdin);
- tytul[strlen(tytul) - 1] = '\0';
- printf("Podaj wydawnictwo \t");
- fgets(wydawnictwo, MAX, stdin);
- wydawnictwo[strlen(wydawnictwo) - 1] = '\0';
- printf("Podaj rok \t");
- q=scanf("%d", rok);
- while (*rok>2019 || q !=1)
- {
- clearBUF();
- printf ("Uprzejmie prosze sprawdz poprowanosc danych\n");
- q=scanf("%d", rok);
- }
- printf("Podaj liczbe stron \t");
- w=scanf("%d", stron);
- while (*stron<0 || w !=1)
- {
- clearBUF();
- printf ("Uprzejmie prosze sprawdz poprowanosc danych\n");
- w=scanf("%d", stron);
- }
- printf("Podaj numer wydania \t");
- e=scanf("%d", numer);
- while (*numer<0 || e !=1)
- {
- clearBUF();
- printf ("Uprzejmie prosze sprawdz poprowanosc danych\n");
- e=scanf("%d", numer);
- }
- }
- void vanishrear(kat **tail)
- {
- if (*tail!=NULL)
- {
- if((*tail)->up==NULL)
- {
- *tail=NULL;
- }
- else
- {
- kat *tmp;
- tmp=(*tail)->up;
- free(*tail);
- *tail=tmp;
- (*tail)->down=NULL;
- }
- }
- }
- void vanishindex(kat** head, int index, kat** tail)
- {
- kat* current=*head;
- int j=1;
- int t; //zabezpiecza index
- while(current->down!=NULL)
- {
- current=current->down;
- ++j;
- }
- printf("\ntyle %d", j);
- printf("\npodaj nr do usuniecia: ");
- t=scanf("%d", &index);
- while (index>j || t!=1)
- {
- clearBUF();
- printf ("Panie co Pan:p\n");
- printf("MAX:%d", j);
- printf("\npodaj nr do usuniecia: ");
- t=scanf("%d", &index);
- }
- if(index==1)
- {
- vanishfront(head);
- }
- else if(current->down!=NULL && current->up !=NULL)
- {
- for(; index>1; current=current->down,--index);
- current->down->up=current->up;
- current->up->down=current->down;
- free (current);
- }
- else
- {
- vanishrear(tail);
- }
- }
- void writefile(char *filename, kat*head)
- {
- printf(" tworzenie %s.txt file",filename);
- FILE *fp;
- filename=strcat(filename,".txt");
- fp=fopen(filename,"w");
- {
- if(head==NULL)
- fprintf(fp,"?");
- else
- {
- do
- {
- fprintf(fp,"%s %s %s %d %d %d",
- head->autor, head->tytul, head->wydawnictwo, head->rok, head->stron, head->numer );
- head = head->down;
- fprintf(fp,"\n");
- }
- while(head !=NULL);
- fprintf(fp,"?");
- }
- }
- fclose(fp);
- printf("\n %s file utworzony",filename);
- }
- void importfile(kat *head,kat *tail)
- {
- FILE *fp;
- fp=fopen("1.txt","r+");
- char autor[MAX];
- char tytul[MAX];
- char wydawnictwo[MAX];
- int rok;
- int stron;
- int numer;
- if(fp ==NULL)
- {
- puts("Nie udalo sie wczytac ksiązek! Konczenie pracy programu...");
- exit(-1);
- }
- fscanf(fp,"%s",autor);
- if (autor[0]=='?')
- {
- puts("Nic tu nie ma");
- }
- else
- {
- while(autor[0]!='?')
- {
- fscanf(fp,"%s %s %d %d %d", tytul, wydawnictwo, &rok, &stron, &numer);
- insertfront(&head,&tail, autor, tytul, wydawnictwo,rok, stron, numer);
- fscanf(fp,"%s",autor);
- }
- }
- fclose(fp);
- print(head);
- }
- void sort(kat *head)
- {
- kat *i;
- kat *j;
- int jak;
- int k;
- int l;
- int x;
- puts("Po którym polu chcesz posortować \n[1] autor \n[2] tytul \n[3] wydawnictwo \n[4] rok \n[5] stron \n[6] wydanie?");
- x=scanf("%d", &jak);
- while(x!=1 || jak >6 || jak<1)
- {
- clearBUF();
- puts("Panie od 0 do 6");
- x=scanf("%d", &jak);
- }
- if(jak == 1 || jak ==2 || jak ==3)
- {
- for(i=head, k=0; i->down!=NULL; i=i->down,k++)
- {
- for(j=i->down,l = k+1; j!=NULL; j=j->down,l++)
- {
- if(jak==1)
- {
- if (strcmp(i->autor,j->autor) > 0)
- {
- swap(i,j);
- }
- }
- else if(jak==2)
- {
- if (strcmp(i->tytul,j->tytul) > 0)
- {
- swap(i,j);
- }
- }
- else {
- if (strcmp(i->wydawnictwo,j->wydawnictwo) > 0)
- {
- swap(i,j);
- }
- }
- }
- }
- }
- if(jak==4 || jak==5 || jak==6)
- {
- for(i=head; i->down!=NULL; i=i->down)
- {
- for(j=i->down; j!=NULL; j=j->down)
- {
- if(jak==4)
- {
- if(i->rok > j->rok)
- {
- swap(i,j);
- }
- }
- else if(jak==5)
- {
- if(i->stron > j->stron)
- {
- swap(i,j);
- }
- }
- else
- {
- if(i->numer > j->numer)
- {
- swap(i,j);
- }
- }
- }
- }
- }
- }
- void swap(kat *i, kat *j)
- {
- int temp;
- char temp2[MAX];
- temp=i->rok;
- i->rok=j->rok;
- j->rok=temp;
- temp=i->stron;
- i->stron=j->stron;
- j->stron=temp;
- temp=i->numer;
- i->numer=j->numer;
- j->numer=temp;
- strcpy(temp2,i->autor);
- strcpy(i->autor,j->autor);
- strcpy(j->autor,temp2);
- strcpy(temp2,i->wydawnictwo);
- strcpy(i->wydawnictwo,j->wydawnictwo);
- strcpy(j->wydawnictwo,temp2);
- strcpy(temp2,i->tytul);
- strcpy(i->tytul,j->tytul);
- strcpy(j->tytul,temp2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement