Advertisement
wojtas626

[C] Kolos JiMP (Szklarz)

Jan 20th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.50 KB | None | 0 0
  1. /*
  2.  *  Autor: Wojciech KumoƱ
  3.  *  Wybrana wersja: C
  4.  *  Numer: 4
  5.  */
  6.  
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12.  
  13. struct Rectangle
  14. {
  15.     double x;
  16.     double y;
  17.     int amount;
  18.     struct Rectangle *next;
  19. };
  20.  
  21.  
  22. //  wczytywanie danych
  23. void loadData(struct Rectangle **pointer)
  24. {
  25.     (*pointer)->next = NULL;
  26.     do
  27.     {
  28.         printf("Wprowadz poprawna dlugosc: ");
  29.         scanf("%lf", &((*pointer)->x));
  30.     }
  31.     while ((*pointer)->x <= 0);
  32.  
  33.     do
  34.     {
  35.         printf("Wprowadz poprawna szerokosc: ");
  36.         scanf("%lf", &((*pointer)->y));
  37.     }
  38.     while ((*pointer)->y <= 0);
  39.  
  40.     do
  41.     {
  42.         printf("Wprowadz poprawna ilosc: ");
  43.         scanf("%d", &((*pointer)->amount));
  44.     }
  45.     while ((*pointer)->amount < 0);
  46.  
  47.  
  48.     printf("\n");
  49. }
  50.  
  51.  
  52. // dodwanie nowego elementu listy
  53. void addNew(struct Rectangle **headPointer)
  54. {
  55.     struct Rectangle *pointer;
  56.  
  57.     pointer = *headPointer;
  58.     if (pointer == NULL)
  59.     {
  60.         pointer = (struct Rectangle*)malloc(sizeof(struct Rectangle));
  61.         loadData(&pointer);
  62.         *headPointer = pointer;
  63.     }
  64.     else
  65.     {
  66.         while(pointer->next != NULL)
  67.         {
  68.             pointer = pointer->next;
  69.         }
  70.         pointer->next = (struct Rectangle*)malloc(sizeof(struct Rectangle));
  71.         pointer = pointer->next;
  72.         loadData(&pointer);
  73.     }
  74. }
  75.  
  76.  
  77. // glowna funkcja wczytujaca
  78. void loadState(struct Rectangle **headPointer)
  79. {
  80.     int choose;
  81.  
  82.     printf("1. Dodaj scinki.\n");
  83.     printf("2. Zakoncz dodawanie.\n");
  84.     scanf("%d", &choose);
  85.  
  86.     while(choose == 1)
  87.     {
  88.         addNew(headPointer);
  89.  
  90.         printf("1. Dodaj scinki.\n");
  91.         printf("2. Zakoncz dodawanie.\n");
  92.         scanf("%d", &choose);
  93.     }
  94. }
  95.  
  96.  
  97. // funckja wyswietlajaca cala liste (m.in. do testow)
  98. void showAll(struct Rectangle *head)
  99. {
  100.     if (head == NULL)
  101.     {
  102.         printf("Lista pusta!\n");
  103.     }
  104.     else
  105.     {
  106.         printf("Dlugosc: %lf\n", head->x);
  107.         printf("Szerokosc: %lf\n", head->y);
  108.         printf("Ilosc: %d\n\n", head->amount);
  109.  
  110.         while (head->next != NULL)
  111.         {
  112.             head = head->next;
  113.             printf("Dlugosc: %lf\n", head->x);
  114.             printf("Szerokosc: %lf\n", head->y);
  115.             printf("Ilosc: %d\n\n", head->amount);
  116.         }
  117.     }
  118. }
  119.  
  120.  
  121. //  zamowienie
  122. void order(double *x, double *y)
  123. {
  124.     printf("Wprowadz wymiary poszukiwanej szyby.\n");
  125.     do
  126.     {
  127.         printf("Wprowadz poprawna dlugosc: ");
  128.         scanf("%lf", x);
  129.     }
  130.     while (*x <= 0);
  131.  
  132.     do
  133.     {
  134.         printf("Wprowadz poprawna szerokosc: ");
  135.         scanf("%lf", y);
  136.     }
  137.     while (*y <= 0);
  138. }
  139.  
  140.  
  141. //  funkcja znajdujaca odpowiednia szybe
  142. void findGlass(struct Rectangle **head, double x, double y, double *foundX, double *foundY)
  143. {
  144.     struct Rectangle *pointer;
  145.     double surfaceMin;
  146.     int counter, savedCounter, i;
  147.  
  148.     *foundX = -1;
  149.     *foundY = -1;
  150.     pointer = *head;
  151.     counter = 0;
  152.     savedCounter = 0;
  153.     surfaceMin = -1;
  154.     if (pointer != NULL)
  155.     {
  156.         counter++;
  157.         if ( (pointer->x >= x) && (pointer->y >= y) && (pointer->amount > 0) )
  158.         {
  159.             surfaceMin = (pointer->x) * (pointer->y);
  160.             *foundX = pointer->x;
  161.             *foundY = pointer->y;
  162.             savedCounter = counter;
  163.         }
  164.         while (pointer->next != NULL)
  165.         {
  166.             pointer = pointer->next;
  167.             counter++;
  168.             if ( (pointer->x >= x) && (pointer->y >= y) && (pointer->amount > 0))
  169.             {
  170.                 if ( (surfaceMin == -1) || (surfaceMin > pointer->x * pointer->y) )
  171.                 {
  172.                     surfaceMin = (pointer->x) * (pointer->y);
  173.                     *foundX = pointer->x;
  174.                     *foundY = pointer->y;
  175.                     savedCounter = counter;
  176.                 }
  177.             }
  178.         }
  179.     }
  180.  
  181.  
  182.     if (savedCounter == 1)
  183.     {
  184.         ((*head)->amount)--;
  185.         if ((*head)->amount == 0)
  186.         {
  187.             if ((*head)->next != NULL)
  188.             {
  189.                 pointer = (*head)->next;
  190.                 free(*head);
  191.                 *head = pointer;
  192.             }
  193.             else
  194.             {
  195.                 free(*head);
  196.                 *head = NULL;
  197.             }
  198.         }
  199.  
  200.  
  201.     }
  202.     else if (savedCounter > 1)
  203.     {
  204.         struct Rectangle *pointerBefore;
  205.  
  206.         pointer = *head;
  207.         for (i = 0; i < (savedCounter - 1); i++)
  208.         {
  209.             pointerBefore = pointer;
  210.             pointer = pointer->next;
  211.         }
  212.         (pointer->amount)--;
  213.  
  214.         if (pointer->amount == 0)
  215.         {
  216.             if (pointer->next != NULL)
  217.             {
  218.                 pointerBefore->next = pointer->next;
  219.                 free(pointer);
  220.             }
  221.             else
  222.             {
  223.                 free(pointer);
  224.                 pointerBefore->next = NULL;
  225.             }
  226.         }
  227.  
  228.     }
  229.  
  230. }
  231.  
  232.  
  233. int main()
  234. {
  235.     struct Rectangle *head;
  236.     double orderedX, orderedY, foundX, foundY;
  237.  
  238.  
  239.  
  240.     head = NULL;
  241.  
  242.     loadState(&head);
  243.  
  244.     order(&orderedX, &orderedY);
  245.  
  246.     findGlass(&head, orderedX, orderedY, &foundX, &foundY);
  247.  
  248.  
  249.     if (foundX != -1)
  250.     {
  251.         printf("Znaleziony prostokat ma wymiary: %lf x %lf\n", foundX, foundY);
  252.         printf("Powstaly odpad: %lf\n\n\n", (foundX*foundY - orderedX*orderedY));
  253.     }
  254.     else
  255.     {
  256.         printf("Nie znaleziono odpowiedniego prostokata!\n\n\n");
  257.     }
  258.  
  259.  
  260.     printf("Pozostale scinki:\n");
  261.     showAll(head);
  262.  
  263.  
  264.     return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement