Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6.  
  7. typedef struct lista_okregow{
  8.     char figura;
  9.     int wsp_x, wsp_y, r;
  10.     struct lista_okregow *next;
  11. }lista_o;
  12.  
  13. typedef struct lista_prostokatow{
  14.     char figura;
  15.     int wsp_x, wsp_y, wsp2_x, wsp2_y;
  16.     struct lista_prostokatow *next;
  17. }lista_p;
  18.  
  19. void sortowanie (char* nazwa_pliku, lista_o **wierzcholek_o, lista_p **wierzcholek_p)
  20. {
  21.     lista_o *nowy1;
  22.     lista_p *nowy2;
  23.     FILE * plik;
  24.     char figura, wiersz[64], pom;
  25.     int wsp_x, wsp_y, r, wsp2_x, wsp2_y;
  26.  
  27.     if((plik = fopen(nazwa_pliku,"r"))==NULL)
  28.     {
  29.         printf("plik nie zostal otwarty");
  30.         return NULL;
  31.     }
  32.  
  33.     while((fgets(wiersz, sizeof(wiersz), plik)))
  34.     {
  35.         pom = wiersz[0];
  36.         if(pom == 'o' || pom == 'O')
  37.         {
  38.             printf("%c\n",pom );
  39.             nowy1 = malloc(sizeof(lista_o));
  40.             sscanf(wiersz, "%c %d %d %d", &figura, &wsp_x, &wsp_y, &r);
  41.             nowy1 -> figura = figura;
  42.             nowy1 -> wsp_x = wsp_x;
  43.             nowy1 -> wsp_y = wsp_y;
  44.             nowy1 -> r = r;
  45.             nowy1 -> next = *wierzcholek_o;
  46.             *wierzcholek_o = nowy1;
  47.         }
  48.         else if(pom == 'p' || pom == 'P')
  49.         {
  50.             printf("%c\n",pom );
  51.             nowy2 = malloc(sizeof(lista_p));
  52.             sscanf(wiersz, "%c %d %d %d %d", &figura, &wsp_x, &wsp_y, &wsp2_x, &wsp2_y);
  53.             nowy2 -> figura = figura;
  54.             nowy2 -> wsp_x = wsp_x;
  55.             nowy2 -> wsp_y = wsp_y;
  56.             nowy2 -> wsp2_x = wsp2_x;
  57.             nowy2 -> wsp2_y = wsp2_y;
  58.             nowy2 -> next = *wierzcholek_p;
  59.             *wierzcholek_p = nowy2;
  60.         }
  61.     }
  62.     fclose(plik);
  63. }
  64.  
  65. void wyswietlanie_o(lista_o* nazwa_listy)
  66. {
  67.     lista_o *pomoc;
  68.     pomoc = nazwa_listy;
  69.  
  70.     while(pomoc->next != NULL) {
  71.         printf("%c %d %d %d\n",pomoc->figura, pomoc->wsp_x, pomoc->wsp_y, pomoc->r);
  72.         pomoc = pomoc->next;
  73.     }
  74. }
  75. void wyswietlanie_p(lista_p* nazwa_listy)
  76. {
  77.     lista_p *pomoc;
  78.     pomoc = nazwa_listy;
  79.  
  80.     while(pomoc->next != NULL) {
  81.         printf("%c %d %d %d %d\n",pomoc->figura, pomoc->wsp_x, pomoc->wsp_y, pomoc->wsp2_x, pomoc->wsp2_y);
  82.         pomoc = pomoc->next;
  83.     }
  84. }
  85.  
  86. int pop_o(lista_o** adres_listy) {
  87.  
  88.         lista_o** head = (lista_o**)adres_listy;
  89.         lista_o* new_head = (*head)->next;
  90.         free(*head);
  91.         if(new_head != NULL) *head = new_head;
  92.         else *head = NULL;
  93.  
  94.     return 0;
  95. }
  96.  
  97. int pop_p(lista_p** adres_listy) {
  98.  
  99.         lista_p** head = (lista_p**)adres_listy;
  100.         lista_p* new_head = (*head)->next;
  101.         free(*head);
  102.         if(new_head != NULL) *head = new_head;
  103.         else *head = NULL;
  104.  
  105.     return 0;
  106. }
  107.  
  108.  
  109. int main()
  110. {
  111.  
  112.  
  113.     lista_o *wierzcholek_o = malloc(sizeof(lista_o)) , *pomoc_o;
  114.     lista_p *wierzcholek_p = malloc(sizeof(lista_p)) , *pomoc_p;
  115.     wierzcholek_o->next = NULL;
  116.     wierzcholek_p->next = NULL;
  117.  
  118.     sortowanie("figury.txt", &wierzcholek_o, &wierzcholek_p);
  119.  
  120.     /*printf("%p\n", *wierzcholek_o);
  121.     printf("%p\n", *wierzcholek_p);
  122.     pomoc_o = wierzcholek_o;
  123.     pomoc_p = wierzcholek_p;*/
  124.  
  125.     wyswietlanie_o(wierzcholek_o);
  126.     wyswietlanie_p(wierzcholek_p);
  127.  
  128.     pop_o(wierzcholek_o);
  129.  
  130.     wyswietlanie_o(wierzcholek_o);
  131.  
  132.  
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement