Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cmath>
  6. #include <string>
  7. #include <iostream>
  8.  
  9. int main()
  10. {
  11.     char buf[16], war = 0;
  12.     srand( time( NULL ) );
  13.     int n = 0;
  14.     printf("podaj liczbe punktow do wygenerowania\n");
  15.     scanf("%d", &n);
  16.     while(1)
  17.     {
  18.         printf("wyswietlac wygenerowane punkty? [T/N]\n");
  19.         std::cin >> war;
  20.         if(war == 'T' || war == 't' || war == 'N' || war == 'n')
  21.             break;
  22.     }
  23.  
  24.     struct punkt
  25.     {
  26.         double x, y, z;
  27.  
  28.         std::string nazwa;
  29.         double odleglosc(const punkt punkty)
  30.         {
  31.             return sqrt((punkty.x - x) * (punkty.x - x) + (punkty.y - y) * (punkty.y - y) + (punkty.z - z) * (punkty.z - z));
  32.         }
  33.     };
  34.     punkt *punkty = new punkt[n];
  35.  
  36.     if(war == 'T' || war == 't')
  37.         printf("%9s%9s%9s%9s\n", "x", "y", "z", "nazwa");
  38.  
  39.  
  40.     for (int i = 0; i < n; i++)
  41.     {
  42.         punkty[i].nazwa = "0x";
  43.         punkty[i].x = (rand() % 200) - 100;
  44.         punkty[i].y = (rand() % 200) - 100;
  45.         punkty[i].z = (rand() % 200) - 100;
  46.         punkty[i].nazwa.append(itoa(i, buf, 16));
  47.         if(war == 'T' || war == 't')
  48.             printf("%9.3lf%9.3lf%9.3lf%9s\n", punkty[i].x, punkty[i].y, punkty[i].z, punkty[i].nazwa.c_str());
  49.     }
  50.  
  51.     int licznik = 0;
  52.     int *odleglosci = new int[(n * n - n) / 2]; //liczba odległości do przechowywania to kombinacja 2 elementowa ze zbioru n elementowego
  53.     double x1_min = punkty[0].x, y1_min = punkty[0].y, z1_min = punkty[0].z, odleglosc_min = punkty[0].odleglosc(punkty[1]);
  54.     double x2_min = punkty[0].x, y2_min = punkty[0].y, z2_min = punkty[0].z;
  55.     std::string nazwa_min1 = punkty[0].nazwa.c_str();
  56.     std::string nazwa_min2 = punkty[0].nazwa.c_str();
  57.     double x1_max = punkty[0].x, y1_max = punkty[0].y, z1_max = punkty[0].z, odleglosc_max = punkty[0].odleglosc(punkty[1]);
  58.     double x2_max = punkty[0].x, y2_max = punkty[0].y, z2_max = punkty[0].z;
  59.     std::string nazwa_max1 = punkty[0].nazwa.c_str();
  60.     std::string nazwa_max2 = punkty[0].nazwa.c_str();
  61.  
  62.     for (int indeks_poczatku = 0; indeks_poczatku < n - 1; indeks_poczatku++)
  63.     {
  64.         for (int i = indeks_poczatku + 1; i < n; i++,licznik++)
  65.         {
  66.             odleglosci[licznik] = punkty[indeks_poczatku].odleglosc(punkty[i]);
  67.             if(punkty[indeks_poczatku].odleglosc(punkty[i]) < odleglosc_min)
  68.             {
  69.                 x1_min = punkty[indeks_poczatku].x;
  70.                 x2_min = punkty[i].x;
  71.                 y1_min = punkty[indeks_poczatku].y;
  72.                 y2_min = punkty[i].y;
  73.                 z1_min = punkty[indeks_poczatku].z;
  74.                 z2_min = punkty[i].z;
  75.                 odleglosc_min = punkty[indeks_poczatku].odleglosc(punkty[i]);
  76.                 nazwa_min1 = punkty[indeks_poczatku].nazwa.c_str();
  77.                 nazwa_min2 = punkty[i].nazwa.c_str();
  78.             }
  79.             if(punkty[indeks_poczatku].odleglosc(punkty[i]) > odleglosc_max)
  80.             {
  81.                 x1_max = punkty[indeks_poczatku].x;
  82.                 x2_max = punkty[i].x;
  83.                 y1_max = punkty[indeks_poczatku].y;
  84.                 y2_max = punkty[i].y;
  85.                 z1_max = punkty[indeks_poczatku].z;
  86.                 z2_max = punkty[i].z;
  87.                 odleglosc_max = punkty[indeks_poczatku].odleglosc(punkty[i]);
  88.                 nazwa_max1 = punkty[indeks_poczatku].nazwa.c_str();
  89.                 nazwa_max2 = punkty[i].nazwa.c_str();
  90.             }
  91.            
  92.         }
  93.     }
  94.     printf("punkty polozone najblizej siebie\n\n");
  95.     printf("%9s%9s%9s%9s%9s\n", "nazwa", "x", "y", "z","odleglosc");
  96.     printf("%9s%9.3lf%9.3lf%9.3lf%9.3lf\n", nazwa_min1, x1_min, y1_min, z1_min, odleglosc_min);
  97.     printf("%9s%9.3lf%9.3lf%9.3lf\n\n\n", nazwa_min2, x2_min, y2_min, z2_min);
  98.     printf("punkty polozone najdalej od siebie\n\n");
  99.     printf("%9s%9s%9s%9s%9s\n", "nazwa", "x", "y", "z","odleglosc");
  100.     printf("%9s%9.3lf%9.3lf%9.3lf%9.3lf\n", nazwa_max1, x1_max, y1_max, z1_max, odleglosc_max);
  101.     printf("%9s%9.3lf%9.3lf%9.3lf\n\n\n", nazwa_max2, x2_max, y2_max, z2_max);
  102.    
  103.     getch();
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement