Advertisement
Guest User

Untitled

a guest
Jan 4th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct punkt
  5. {
  6.     int x;
  7.     int y;
  8. };
  9.  
  10. struct punkt nowyPunkt()
  11. {
  12.     struct punkt ret;
  13.     printf("X:>");
  14.     fflush(stdin);
  15.     scanf("%d", &ret.x);
  16.     printf("Y:>");
  17.     fflush(stdin);
  18.     scanf("%d", &ret.y);
  19.  
  20.     return ret;
  21. }
  22.  
  23. struct trojkat
  24. {
  25.     struct punkt punkty[3];
  26.     double pole;
  27. };
  28.  
  29. struct trojkat nowyTrojkat(struct punkt a, struct punkt b, struct punkt c)
  30. {
  31.     struct trojkat ret;
  32.     ret.punkty[0] = a;
  33.     ret.punkty[1] = b;
  34.     ret.punkty[2] = c;
  35.     ret.pole = 0.5 * abs((a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x));
  36.    
  37.     return ret;
  38. }
  39.  
  40. int sprawdzCzyTworzaTrojkat(struct punkt a, struct punkt b, struct punkt c)
  41. {
  42.     if((double) (b.y - a.y) / (double) (b.x - a.x) == (double) (c.y - a.y) / (double) (c.x - a.x))
  43.         return 0;
  44.     return 1;
  45. }
  46.  
  47. void drukujTrojkat(struct trojkat t)
  48. {
  49.  
  50.     printf("\ta\tb\tc\nX:\t%d\t%d\t%d\nY:\t%d\t%d\t%d\n\nPole wynosi: %g\n", t.punkty[0].x, t.punkty[1].x, t.punkty[2].x, t.punkty[0].y, t.punkty[1].y, t.punkty[2].y, t.pole);
  51. }
  52.  
  53. int main()
  54. {
  55.     int lp = 0; //liczba punktów
  56.     int i = 0;
  57.     int j = 0;
  58.     int k = 0;
  59.     int lt = 0; //liczba trójkątów
  60.     struct punkt* A;  //punkty
  61.     struct trojkat* T; //trojkaty
  62.     void* tmp;
  63.  
  64.     printf("Witaj w programie.\nPodaj ile punktow chcesz wprowadzic[4-6]>");
  65.  
  66.     while(1)
  67.     {
  68.         fflush(stdin);
  69.         scanf("%d", &lp);
  70.         if(lp == 0) break;
  71.         if(lp < 4 || lp > 6)
  72.         {
  73.             printf("Podano niepoprawna ilosc punktow!\nPodaj ilosc jeszcze raz, lub wpisz 0 aby zakonczyc program.\nIlosc>");
  74.             continue;
  75.         }
  76.         break;
  77.     }
  78.  
  79.     A = (struct punkt*) malloc(sizeof(struct punkt) * lp);
  80.     T = (struct trojkat*) malloc(sizeof(struct trojkat));
  81.  
  82.     for(i = 0; i < lp; i++)
  83.     {
  84.         printf("Podawanie punktu nr %d.\n", i+1);
  85.         A[i] = nowyPunkt();
  86.     }
  87.  
  88.     for(i = 0; i < lp; i++)
  89.     {
  90.         for(j = i+1; j < lp; j++)
  91.         {
  92.             for(k = j+1; k < lp; k++)
  93.             {
  94.                 if(sprawdzCzyTworzaTrojkat(A[i], A[j], A[k]) == 1)
  95.                 {
  96.                     //printf("Nastepujace punkty tworza trojkat:\n\tA[%d]\tA[%d]\tA[%d]\nX:\t%d\t%d\t%d\nY:\t%d\t%d\t%d\n", i, j, k, A[i].x, A[j].x, A[k].x, A[i].y, A[j].y, A[k].y);
  97.                     lt++;
  98.                     T = (struct trojkat*) realloc(T, sizeof(struct trojkat) * lt);
  99.                     T[lt-1] = nowyTrojkat(A[i], A[j], A[k]);
  100.                 }
  101.             }
  102.         }
  103.         if(i+2 >= lp)
  104.             break;
  105.     }
  106.  
  107.     if(lt == 0)
  108.     {
  109.         printf("\nZ podanych puntkow nie mozna utworzyc zadnego trojkata!.\n");
  110.     }
  111.     else
  112.     {
  113.         printf("\nLiczba trojkatow mozliwych do stworzenia: %d\nOto te trojkaty:\n", lt);
  114.    
  115.         for(i = 0; i < lt; i++)
  116.         {
  117.             printf("\nTrojkat nr %d:\n", i+1);
  118.             drukujTrojkat(T[i]);
  119.         }
  120.        
  121.     }
  122.  
  123.     printf("\n\nKoniec programu. Nacisnij dowolny klawisz..\n");
  124.     getch();
  125.    
  126.  
  127.     return 0;
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement