Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 41
  5. #include <unistd.h>    //liczba znaków w tablicy
  6.  
  7. struct kat
  8. {
  9.     char autor[MAX];
  10.     char tytul[MAX];
  11.     char wydawnictwo[MAX];
  12.     int rok;
  13.     int stron;
  14.     int numer;
  15.     struct kat* up;
  16.     struct kat* down;
  17. };
  18. typedef struct kat kat;
  19.  
  20. void print(kat* head);//wyświetla to co jest do tej pory
  21. void options();//wyswietla dostępne opcje
  22. void clearBUF(void);//czysci bufor
  23. void case1(char autor[], char tytul[], char wydawnictwo[], int* rok, int* stron, int* numer);
  24. void insertfront(kat** head,kat** tail,char autor[], char tytul[], char wydawnictwo[], int rok, int stron, int numer);//dodaje na przód
  25. void vanishall(kat **head);//usuwa całość
  26. void vanishfront(kat**head);//usuwa przód
  27. void vanishrear(kat **head);//usuwa tył
  28. void vanishindex(kat**head, int index, kat **tail);
  29. void writefile(char *filename,kat *head);
  30. void importfile( kat *head, kat *tail);
  31. void sort(kat *head);
  32. void swap(kat *i, kat *j);
  33.  
  34.  
  35.  
  36. int main(void)
  37. {
  38.     char autor[MAX];
  39.     char tytul[MAX];
  40.     char wydawnictwo[MAX];
  41.     int rok;
  42.     int stron;
  43.     int numer;
  44.     kat* head = NULL;
  45.     kat* tail = NULL;
  46.     int option=-1;  //potrzebny do switch
  47.     int index=-1 ;
  48.     int x;          //zabezpiecza opcje
  49.     char str[100];  //nazwa pliku
  50.  
  51.  
  52.  
  53.  
  54.     while(option!=0)
  55.     {
  56.         system("clear"); //czysci ekran
  57.         printf("\nAktualny stan listy: \n");
  58.         print(head);
  59.  
  60.         options(); // wyświetla dostępne możliwości
  61.         x=scanf("%d", &option);
  62.  
  63.         while(x!=1 || option<1 || option >6 )
  64.         {
  65.             clearBUF();
  66.             puts("Panie od 0 do 6");
  67.             x=scanf("%d", &option);
  68.         }
  69.  
  70.         switch (option)
  71.         {
  72.  
  73.         case 0:
  74.             return 0;
  75.             break;
  76.  
  77.         case 1:
  78.             clearBUF();
  79.             case1( autor,  tytul,  wydawnictwo,  &rok,  &stron,  &numer);
  80.             insertfront(&head,&tail, autor, tytul, wydawnictwo,rok, stron,numer ); //adres do wskaznika daje &
  81.             break;
  82.  
  83.         case 2:
  84.             clearBUF();
  85.             vanishindex(&head,index,&tail);
  86.             break;
  87.  
  88.         case 3:
  89.             clearBUF();
  90.             vanishall(&head);
  91.             break;
  92.  
  93.         case 4:
  94.             clearBUF();
  95.             printf("\n Nazwij plik:");
  96.             gets(str);
  97.             writefile(str,head);
  98.             break;
  99.  
  100.         case 5:
  101.             clearBUF();
  102.             importfile(head,tail);
  103.             sleep(2);
  104.             break;
  105.  
  106.         case 6:
  107.             clearBUF();
  108.             if(head==NULL){
  109.             puts("HOLA HOLA");
  110.             sleep(1);
  111.             }
  112.             else
  113.             sort(head);
  114.             break;
  115.  
  116.         }
  117.     }
  118.     return 0;
  119. }
  120.  
  121.  
  122.  
  123. void insertfront(kat** head,kat** tail,char autor[], char tytul[], char wydawnictwo[], int rok, int stron, int numer)
  124. {
  125.     kat *current = malloc(sizeof (kat));
  126.     strcpy(current->tytul, tytul);
  127.     strcpy(current->autor, autor);
  128.     strcpy(current->wydawnictwo,wydawnictwo);
  129.     current->rok=rok;
  130.     current->stron=stron;
  131.     current->numer=numer;
  132.  
  133.     if (*head==NULL)
  134.     {
  135.         *head=(*tail)=current;
  136.         current->down=current->up=NULL;
  137.     }
  138.     else
  139.     {
  140.         current->down=NULL;
  141.         current->up=(*tail);
  142.         (*tail)->down=current;
  143.         (*tail)=current;
  144.     }
  145. }
  146. void print (kat* head)
  147. {
  148.     int i=1;
  149.     printf("\n");
  150.     if(head==NULL)
  151.         printf("Katalog jest pusty");
  152.     else
  153.     {
  154.         do
  155.         {
  156.             printf("\n%d. Autor: %10s tytul: %10s Wydawnictwo: %10s Rok: %5d  Stron: %5d  Numer: %5d\t  ",
  157.                    i,head->autor, head->tytul, head->wydawnictwo, head->rok, head->stron, head->numer );
  158.             head = head->down;
  159.             ++i;
  160.         }
  161.         while(head !=NULL);
  162.     }
  163. }
  164. void vanishfront(kat **head)
  165. {
  166.     if (*head!=NULL)
  167.     {
  168.         if((*head)->down==NULL)
  169.         {
  170.             *head=NULL;
  171.         }
  172.         else
  173.         {
  174.             kat *tmp;
  175.             tmp=(*head)->down;
  176.             free(*head);
  177.             *head=tmp;
  178.             (*head)->up=NULL;
  179.         }
  180.     }
  181. }
  182. void vanishall(kat **head)
  183. {
  184.     if (*head!=NULL)
  185.     {
  186.         if((*head)->up==NULL)
  187.         {
  188.             *head=NULL;
  189.         }
  190.         else
  191.         {
  192.             kat *tmp;
  193.             tmp=(*head)->up;
  194.             free(*head);
  195.             *head=tmp;
  196.             (*head)->down=NULL;
  197.         }
  198.     }
  199. }
  200. void options()
  201. {
  202.     printf("\n\n\nDrogi uzytkowniku! Co chcesz zrobic?\n\n");
  203.     printf("[1] Dodac ksiazke na koniec listy\n");
  204.     printf("[2] Usunac ksiazke o wybranym indeksie\n");
  205.     printf("[3] excute order 66\n");
  206.     printf("[4] Zapisac plik \n");
  207.     printf("[5] Wyswietlic ksiazki z pliku\n");
  208.     printf("[6] Posortowac ksiazki \n");
  209.     printf("[0] Zakonczyc program\n");
  210.  
  211. }
  212. void clearBUF(void)
  213. {
  214.     char c;
  215.     do
  216.         c = getchar();
  217.     while ( c != '\n' && c != EOF);
  218. }
  219. void case1(char autor[], char tytul[], char wydawnictwo[], int* rok, int* stron, int* numer)
  220. {
  221.  
  222.     int q;  //zabezpiecza rok
  223.     int w;  //zabezpiecza stron
  224.     int e;  //zabezpiecza numer
  225.  
  226.  
  227.     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
  228.     fgets(autor, MAX, stdin);
  229.     autor[strlen(autor) - 1] = '\0';       // by enter nie wchodzil dalej
  230.  
  231.     printf("Podaj tytul         \t");
  232.     fgets(tytul, MAX, stdin);
  233.     tytul[strlen(tytul) - 1] = '\0';
  234.  
  235.     printf("Podaj wydawnictwo   \t");
  236.     fgets(wydawnictwo, MAX, stdin);
  237.     wydawnictwo[strlen(wydawnictwo) - 1] = '\0';
  238.  
  239.     printf("Podaj rok           \t");
  240.     q=scanf("%d", rok);
  241.     while (*rok>2019 || q !=1)
  242.     {
  243.         clearBUF();
  244.         printf ("Uprzejmie prosze sprawdz poprowanosc danych\n");
  245.         q=scanf("%d", rok);
  246.     }
  247.     printf("Podaj liczbe stron  \t");
  248.     w=scanf("%d", stron);
  249.     while (*stron<0 || w !=1)
  250.     {
  251.         clearBUF();
  252.         printf ("Uprzejmie prosze sprawdz poprowanosc danych\n");
  253.         w=scanf("%d", stron);
  254.     }
  255.     printf("Podaj numer wydania \t");
  256.     e=scanf("%d", numer);
  257.     while (*numer<0 || e !=1)
  258.     {
  259.         clearBUF();
  260.         printf ("Uprzejmie prosze sprawdz poprowanosc danych\n");
  261.         e=scanf("%d", numer);
  262.     }
  263. }
  264. void vanishrear(kat **tail)
  265. {
  266.     if (*tail!=NULL)
  267.     {
  268.         if((*tail)->up==NULL)
  269.         {
  270.             *tail=NULL;
  271.         }
  272.         else
  273.         {
  274.             kat *tmp;
  275.             tmp=(*tail)->up;
  276.             free(*tail);
  277.             *tail=tmp;
  278.             (*tail)->down=NULL;
  279.         }
  280.  
  281.     }
  282. }
  283. void vanishindex(kat** head, int index, kat** tail)
  284. {
  285.     kat* current=*head;
  286.  
  287.     int j=1;
  288.     int t; //zabezpiecza index
  289.     while(current->down!=NULL)
  290.     {
  291.         current=current->down;
  292.         ++j;
  293.     }
  294.     printf("\ntyle %d", j);
  295.     printf("\npodaj nr do usuniecia: ");
  296.     t=scanf("%d", &index);
  297.     while (index>j ||  t!=1)
  298.     {
  299.         clearBUF();
  300.         printf ("Panie co Pan:p\n");
  301.         printf("MAX:%d", j);
  302.         printf("\npodaj nr do usuniecia: ");
  303.         t=scanf("%d", &index);
  304.     }
  305.  
  306.     if(index==1)
  307.     {
  308.         vanishfront(head);
  309.     }
  310.  
  311.     else if(current->down!=NULL && current->up !=NULL)
  312.     {
  313.         for(; index>1; current=current->down,--index);
  314.  
  315.         current->down->up=current->up;
  316.         current->up->down=current->down;
  317.         free (current);
  318.     }
  319.     else
  320.     {
  321.         vanishrear(tail);
  322.     }
  323.  
  324. }
  325. void writefile(char *filename, kat*head)
  326. {
  327.     printf(" tworzenie %s.txt file",filename);
  328.  
  329.     FILE *fp;
  330.     filename=strcat(filename,".txt");
  331.     fp=fopen(filename,"w");
  332.     {
  333.         if(head==NULL)
  334.             fprintf(fp,"?");
  335.         else
  336.         {
  337.             do
  338.             {
  339.                 fprintf(fp,"%s %s %s %d %d %d",
  340.                         head->autor, head->tytul, head->wydawnictwo, head->rok, head->stron, head->numer );
  341.                 head = head->down;
  342.                 fprintf(fp,"\n");
  343.             }
  344.             while(head !=NULL);
  345.             fprintf(fp,"?");
  346.         }
  347.     }
  348.     fclose(fp);
  349.     printf("\n %s file utworzony",filename);
  350. }
  351. void importfile(kat *head,kat *tail)
  352. {
  353.     FILE *fp;
  354.     fp=fopen("1.txt","r+");
  355.     char autor[MAX];
  356.     char tytul[MAX];
  357.     char wydawnictwo[MAX];
  358.     int rok;
  359.     int stron;
  360.     int numer;
  361.  
  362.     if(fp ==NULL)
  363.     {
  364.         puts("Nie udalo sie wczytac ksiązek! Konczenie pracy programu...");
  365.         exit(-1);
  366.     }
  367.     fscanf(fp,"%s",autor);
  368.     if (autor[0]=='?')
  369.     {
  370.         puts("Nic tu nie ma");
  371.     }
  372.     else
  373.     {
  374.         while(autor[0]!='?')
  375.         {
  376.             fscanf(fp,"%s %s %d %d %d", tytul, wydawnictwo, &rok, &stron, &numer);
  377.             insertfront(&head,&tail, autor, tytul, wydawnictwo,rok, stron, numer);
  378.             fscanf(fp,"%s",autor);
  379.         }
  380.     }
  381.     fclose(fp);
  382.     print(head);
  383.  
  384. }
  385. void sort(kat *head)
  386. {
  387.     kat *i;
  388.     kat *j;
  389.     int jak;
  390.     int k;
  391.     int l;
  392.     int x;
  393.  
  394.     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?");
  395.     x=scanf("%d", &jak);
  396.  
  397.     while(x!=1 || jak >6 || jak<1)
  398.     {
  399.         clearBUF();
  400.          puts("Panie od 0 do 6");
  401.         x=scanf("%d", &jak);
  402.  
  403.     }
  404.  
  405.     if(jak == 1 || jak ==2 || jak ==3)
  406.     {
  407.         for(i=head, k=0; i->down!=NULL; i=i->down,k++)
  408.         {
  409.             for(j=i->down,l = k+1; j!=NULL; j=j->down,l++)
  410.             {
  411.                 if(jak==1)
  412.                 {
  413.                     if (strcmp(i->autor,j->autor) > 0)
  414.                     {
  415.                         swap(i,j);
  416.                     }
  417.  
  418.                 }
  419.  
  420.                else if(jak==2)
  421.  
  422.                 {
  423.                     if (strcmp(i->tytul,j->tytul) > 0)
  424.                     {
  425.                         swap(i,j);
  426.                     }
  427.  
  428.                 }
  429.                 else {
  430.                  if (strcmp(i->wydawnictwo,j->wydawnictwo) > 0)
  431.                     {
  432.                         swap(i,j);
  433.                     }
  434.  
  435.                 }
  436.  
  437.             }
  438.         }
  439.     }
  440.  
  441.  
  442.     if(jak==4 || jak==5 || jak==6)
  443.     {
  444.         for(i=head; i->down!=NULL; i=i->down)
  445.         {
  446.             for(j=i->down; j!=NULL; j=j->down)
  447.             {
  448.                 if(jak==4)
  449.                 {
  450.                     if(i->rok > j->rok)
  451.                     {
  452.  
  453.                        swap(i,j);
  454.                     }
  455.                 }
  456.  
  457.                 else if(jak==5)
  458.                 {
  459.                     if(i->stron > j->stron)
  460.                     {
  461.                         swap(i,j);
  462.                     }
  463.                 }
  464.  
  465.                 else
  466.                 {
  467.                     if(i->numer > j->numer)
  468.                     {
  469.                         swap(i,j);
  470.                     }
  471.                 }
  472.  
  473.  
  474.             }
  475.         }
  476.     }
  477. }
  478. void swap(kat *i, kat *j)
  479. {
  480.     int temp;
  481.     char temp2[MAX];
  482.  
  483.     temp=i->rok;
  484.     i->rok=j->rok;
  485.     j->rok=temp;
  486.  
  487.     temp=i->stron;
  488.     i->stron=j->stron;
  489.     j->stron=temp;
  490.  
  491.     temp=i->numer;
  492.     i->numer=j->numer;
  493.     j->numer=temp;
  494.  
  495.     strcpy(temp2,i->autor);
  496.     strcpy(i->autor,j->autor);
  497.     strcpy(j->autor,temp2);
  498.  
  499.     strcpy(temp2,i->wydawnictwo);
  500.     strcpy(i->wydawnictwo,j->wydawnictwo);
  501.     strcpy(j->wydawnictwo,temp2);
  502.  
  503.     strcpy(temp2,i->tytul);
  504.     strcpy(i->tytul,j->tytul);
  505.     strcpy(j->tytul,temp2);
  506. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement