Advertisement
wojtas626

[C] Pudełka

Jan 17th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. struct Cuboid
  7. {
  8.     char name;
  9.     double x;
  10.     double y;
  11.     double z;
  12.     struct Cuboid *next;
  13. };
  14.  
  15.  
  16. void loadCuboidData(struct Cuboid **pointer)
  17. {
  18.     (*pointer)->next = NULL;
  19.     printf("\n\n\n\n\n\n\n\nWprowadz identyfikator pudelka (1 znak): ");
  20.     scanf(" %c", &((*pointer)->name));
  21.     printf("Wprowadz wymiar X: ");
  22.     scanf("%lf", &((*pointer)->x));
  23.     printf("Wprowadz wymiar Y: ");
  24.     scanf("%lf", &((*pointer)->y));
  25.     printf("Wprowadz wymiar Z: ");
  26.     scanf("%lf", &((*pointer)->z));
  27. }
  28.  
  29.  
  30. void addCuboid(struct Cuboid **headPointer)
  31. {
  32.     struct Cuboid *pointer;
  33.  
  34.     pointer = *headPointer;
  35.     if (pointer == NULL)
  36.     {
  37.         pointer = (struct Cuboid*)malloc(sizeof(struct Cuboid));
  38.         loadCuboidData(&pointer);
  39.         *headPointer = pointer;
  40.     }
  41.     else
  42.     {
  43.         while (pointer->next != NULL)
  44.         {
  45.             pointer = pointer->next;
  46.         }
  47.         pointer->next = (struct Cuboid*)malloc(sizeof(struct Cuboid));
  48.         pointer = pointer->next;
  49.         loadCuboidData(&pointer);
  50.     }
  51. }
  52.  
  53.  
  54. void loadCuboids(struct Cuboid **headPointer)
  55. {
  56.     int choose;
  57.     printf("1. Dodaj prostopadloscian.\n");
  58.     printf("2. Zakoncz dodawanie.\n");
  59.     scanf("%d", &choose);
  60.  
  61.     while(choose == 1)
  62.     {
  63.         addCuboid(headPointer);
  64.  
  65.         printf("1. Dodaj prostopadloscian.\n");
  66.         printf("2. Zakoncz dodawanie.\n");
  67.         scanf("%d", &choose);
  68.     }
  69.  
  70. }
  71.  
  72.  
  73. int listSize(struct Cuboid *head)
  74. {
  75.     int sizeOfList;
  76.  
  77.     sizeOfList = 0;
  78.     if (head != NULL)
  79.     {
  80.         sizeOfList = 1;
  81.         while (head->next != NULL)
  82.         {
  83.             head = head->next;
  84.             sizeOfList++;
  85.         }
  86.     }
  87.     return sizeOfList;
  88. }
  89.  
  90.  
  91. void checkCouboids(struct Cuboid *head, int ***boxSizesArray)
  92. {
  93.     int sizeOfList, i, j;
  94.     struct Cuboid *pointer, *pointer2;
  95.  
  96.     sizeOfList = listSize(head);
  97.  
  98.     *boxSizesArray = (int**)malloc(sizeof(int*) * (sizeOfList - 1));
  99.     j = sizeOfList - 1;
  100.     for (i = 0; i < (sizeOfList - 1); i++)
  101.     {
  102.         (*boxSizesArray)[i] = (int*)malloc(sizeof(int)*j);
  103.         j--;
  104.     }
  105.  
  106.     pointer = head;
  107.     pointer2 = head;
  108.     for (i = 0; i < (sizeOfList - 1); i++)
  109.     {
  110.         for(j = 0; j < (sizeOfList - 1 - i); j++)
  111.         {
  112.             pointer2 = pointer2->next;
  113.             if ( (pointer->x > pointer2->x) && (pointer->y > pointer2->y) && (pointer->z > pointer2->z) )
  114.             {
  115.                 (*boxSizesArray)[i][j] = 1;
  116.             }
  117.             else if ( (pointer->x < pointer2->x) && (pointer->y < pointer2->y) && (pointer->z < pointer2->z) )
  118.             {
  119.                 (*boxSizesArray)[i][j] = -1;
  120.             }
  121.             else
  122.             {
  123.                 (*boxSizesArray)[i][j] = 0;
  124.             }
  125.         }
  126.         pointer = pointer->next;
  127.         pointer2 = pointer;
  128.     }
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134.     struct Cuboid *head;
  135.     int **boxSizesArray;
  136.     int sizeOfList, i, j;
  137.  
  138.  
  139.  
  140.     head = NULL;
  141.     loadCuboids(&head);
  142.     checkCouboids(head, &boxSizesArray);
  143.  
  144.     sizeOfList = listSize(head);
  145.     for (i = 0; i < (sizeOfList - 1); i++)
  146.     {
  147.         for (j = 0; j < (sizeOfList - 1 - i); j++)
  148.         {
  149.             if (boxSizesArray[i][j] == 1)
  150.             {
  151.                 printf(">  ");
  152.             }
  153.             else if (boxSizesArray[i][j] == -1)
  154.             {
  155.                 printf("<  ");
  156.             }
  157.             else
  158.             {
  159.                 printf("~  ");
  160.             }
  161.         }
  162.         printf("\n");
  163.     }
  164.  
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement