Tucancitto

Lab1 - Pb3

Mar 3rd, 2021 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <climits>
  4.  
  5. struct Punct
  6. {
  7.     int X, Y;
  8.     void citirePunct()
  9.     {
  10.         std::cin >> X >> Y;
  11.     }
  12.     float distanta(Punct P)
  13.     {
  14.         return sqrt((P.X - X) * (P.X - X) + (P.Y - Y) * (P.Y - Y));
  15.     }
  16. };
  17.  
  18. struct PerechePuncte
  19. {
  20.     Punct P1, P2;
  21. };
  22.  
  23. void citire(Punct*& vectorPuncte, int& nrPuncte)
  24. {
  25.     std::cin >> nrPuncte;
  26.     vectorPuncte = new Punct[nrPuncte];
  27.  
  28.     for (int index = 0; index < nrPuncte; ++index)
  29.         vectorPuncte[index].citirePunct();
  30. }
  31.  
  32. void atribuire(PerechePuncte& pereche, Punct Punct1, Punct Punct2)
  33. {
  34.     pereche.P1.X = Punct1.X, pereche.P1.Y = Punct1.Y;
  35.     pereche.P2.X = Punct2.X, pereche.P2.Y = Punct2.Y;
  36. }
  37.  
  38. void perechi(Punct* vectorPuncte, int nrPuncte, PerechePuncte& apropiata, PerechePuncte& departata)
  39. {
  40.     float distMin = FLT_MAX, distMax = 0;
  41.  
  42.     for (int i = 0; i < nrPuncte - 1; ++i)
  43.         for (int j = i + 1; j < nrPuncte; ++j)
  44.         {
  45.             float dist = vectorPuncte[i].distanta(vectorPuncte[j]);
  46.  
  47.             if (dist < distMin)
  48.             {
  49.                 distMin = dist;
  50.                 atribuire(apropiata, vectorPuncte[i], vectorPuncte[j]);
  51.             }
  52.  
  53.             if (dist > distMax)
  54.             {
  55.                 distMax = dist;
  56.                 atribuire(departata, vectorPuncte[i], vectorPuncte[j]);
  57.             }
  58.         }
  59. }
  60.  
  61. void afisarePereche(PerechePuncte pereche)
  62. {
  63.     std::cout << "{(" << pereche.P1.X << ", " << pereche.P1.Y << "), ";
  64.     std::cout << "(" << pereche.P2.X << ", " << pereche.P2.Y << ")}\n";
  65. }
  66.  
  67. int main()
  68. {
  69.     int nrPuncte;
  70.     Punct* vectorPuncte;
  71.     PerechePuncte apropiata, departata;
  72.  
  73.     citire(vectorPuncte, nrPuncte);
  74.     perechi(vectorPuncte, nrPuncte, apropiata, departata);
  75.  
  76.     std::cout << "Perechea de puncte cele mai apropiate este: ";
  77.     afisarePereche(apropiata);
  78.  
  79.     std::cout << "Perechea de puncte aflate la cea mai mare distanta unul fata de celelalt este: ";
  80.     afisarePereche(departata);
  81.  
  82.     delete[]vectorPuncte;
  83.     return 0;
  84. }
  85.  
Add Comment
Please, Sign In to add comment