Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <iterator>
  6. #include <random>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. class generatorLiczbNorm {
  12.     // static – ponieważ ma być jeden generator dla całego programu:
  13.     static default_random_engine rng;
  14.  
  15.     // produkuje liczby o rozkł. normalnym korzystając z generatora rng:
  16.     normal_distribution<double> dist;
  17.  
  18. public:
  19.     generatorLiczbNorm(double mi = 0, double sigma = 1) : dist(mi, sigma) {};
  20.  
  21.     double operator()() {
  22.         return dist(rng);
  23.     }
  24. };
  25. default_random_engine generatorLiczbNorm::rng;
  26.  
  27. class wypisanie
  28. {
  29.     int help;
  30. public:
  31.     wypisanie() : help(0) {};
  32.     void operator()(const double x)
  33.     {
  34.         cout << ++help << " = " << x << endl;
  35.         return;
  36.     }
  37. };
  38.  
  39. class sumowanie
  40. {
  41. public:
  42.     vector<double> operator()(vector<double> a, vector<double> b)
  43.     {
  44.         vector<double> zwrot;
  45.         for (vector<double>::iterator aa = a.begin(), bb = b.begin(); aa != a.end() && bb !=b.end(); ++aa, ++bb)
  46.         {
  47.             zwrot.push_back((*aa)+(*bb));
  48.         }
  49.         return zwrot;
  50.     }
  51. };
  52.  
  53. ostream& operator<<(ostream& os, const vector<double>& vec)
  54. {
  55.     copy(vec.begin(), vec.end(), ostream_iterator<double>(cout, " "));
  56.     return os;
  57. }
  58.  
  59. double diffPow2(double x, double y) { return pow((x - y), 2.0); }
  60.  
  61. int main()
  62. {
  63.     vector<double> A(3), B(3);
  64.     vector<vector<double>> AB, BA;
  65.     generatorLiczbNorm generatorek(0, 10), generatorek2(10, 10);
  66.  
  67.     #pragma region Zapełnienie i wypisanie wektorów
  68.     for (int i = 0; i < 10; ++i)
  69.     {
  70.         for_each(A.begin(), A.end(), [&generatorek](double &a) {a = generatorek(); });
  71.         AB.push_back(A);
  72.         for_each(B.begin(), B.end(), [&generatorek2](double &a) {a = generatorek2(); });
  73.         BA.push_back(B);
  74.     }
  75.  
  76.     /*cout << "Pierwsza chmura: " << endl;
  77.     for_each(AB.begin(), AB.end(), [](vector<double> xx) {for_each(xx.begin(), xx.end(), wypisanie());});
  78.  
  79.     cout << "Druga chmura: " << endl;
  80.     for_each(BA.begin(), BA.end(), [](vector<double> xx) {for_each(xx.begin(), xx.end(), wypisanie());});*/
  81. #pragma endregion
  82.  
  83.     #pragma region Sumowanie i dzielenie vectorów - powstanie i wypisanie centroidów
  84.     vector<double> centroidA(3), centroidB(3);
  85.     centroidA = accumulate(AB.begin(), AB.end(), centroidA, sumowanie());
  86.     centroidB = accumulate(BA.begin(), BA.end(), centroidB, sumowanie());
  87.  
  88.     for_each(centroidA.begin(), centroidA.end(), [](double &x) {x /= 100; });
  89.     for_each(centroidB.begin(), centroidB.end(), [](double &x) {x /= 100; });
  90.  
  91.     /*cout << "Centroid A: " << endl;
  92.     for_each(centroidA.begin(), centroidA.end(), wypisanie());
  93.     cout << "Centroid B: " << endl;
  94.     for_each(centroidB.begin(), centroidB.end(), wypisanie());*/
  95. #pragma endregion
  96.  
  97.     //Wypisanie centroidów przez copy
  98.     cout << "CentroidA: " << endl;
  99.     copy(centroidA.begin(), centroidA.end(), ostream_iterator<double>(cout, "\n"));
  100.     cout << endl << "CentroidB: " << endl;
  101.     copy(centroidB.begin(), centroidB.end(), ostream_iterator<double>(cout, "\n"));
  102.     //Odległość euklidesowa
  103.     cout << endl << "Odleglosc euklidesowa wynosi: " << sqrt(inner_product(centroidA.begin(), centroidA.end(), centroidB.begin(), 0.0, std::plus<double>(), diffPow2)) << endl;
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement