Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- #include <numeric>
- #include <ctime>
- #include <math.h>
- #include <ostream>
- #include <random>
- #include <iterator>
- using namespace std;
- double dodawanie(double a, double b);
- double potega(double a, double b);
- template<class Type>
- class dzielenie
- {
- private:
- Type dzielnik;
- public:
- dzielenie(const Type& wartosc) : dzielnik(wartosc) {};
- void operator()(Type& elem) const
- {
- elem /= dzielnik;
- };
- };
- class generatorLiczbNorm {
- // static ponieważ ma być jeden generator dla całego programu:
- static default_random_engine rng;
- // produkuje liczby o rozkł. normalnym korzystajšc z generatora rng:
- normal_distribution<double> dist;
- public:
- generatorLiczbNorm(double mi = 0, double sigma = 1) : dist(mi, sigma) {};
- double operator()() {
- return dist(rng);
- }
- };
- class generator_pkt{
- int n;
- double mi;
- double sigma;
- public:
- vector<double> operator()()
- {
- vector<double> punkt(n);
- generate_n(punkt.begin(), n, generatorLiczbNorm(mi, sigma));
- return punkt;
- }
- generator_pkt(int i, double x, double y) : n(i), mi(x), sigma(y) {};
- };
- default_random_engine generatorLiczbNorm::rng;
- class dodaj
- {
- public:
- vector<double> operator()(vector<double> const a, vector<double> const b)
- {
- vector<double> suma(a.size());
- transform(a.begin(), a.end(), b.begin(), suma.begin(), dodawanie);
- return suma;
- }
- };
- class Vector : public vector<double>
- {
- public:
- char pochodzenie;
- Vector(char x) : pochodzenie(x) {};
- };
- int main()
- {
- srand(time(NULL));
- int const rozmiar = 100;
- int n;
- cout << "Podaj liczbe wymiarow: ";
- cin >> n;
- vector<vector<double>> A(rozmiar);
- generate(A.begin(), A.end(), generator_pkt(n, 0, 10));
- vector<vector<double>>::iterator itA= A.begin();
- //while (itA != A.end())
- //{
- // vector<double>::iterator iti = (*itA).begin();
- // while (iti != (*itA).end())
- // {
- // cout << *iti << " ";
- // iti++;
- // }
- // cout << endl;
- // itA++;
- //}
- vector<vector<double>> B(rozmiar);
- generate(B.begin(), B.end(), generator_pkt(n, 10, 10));
- vector<double> centroidA(n);
- centroidA=accumulate(A.begin(), A.end(), centroidA, dodaj());
- for_each(centroidA.begin(), centroidA.end(), dzielenie<double>(100));
- vector<double> centroidB(n);
- centroidB=accumulate(B.begin(), B.end(), centroidB, dodaj());
- vector<double>::iterator it = centroidB.begin();
- for_each(centroidB.begin(), centroidB.end(), dzielenie<double>(100));
- //while (it != centroidB.end())
- //{
- // cout << *it << " ";
- // it++;
- //}
- double odleglosc=0;
- odleglosc=inner_product(centroidA.begin(), centroidA.end(), centroidB.begin(), 0.0, dodawanie,potega );
- odleglosc = sqrt(odleglosc);
- cout << endl<< "Odleglosc wynosi: " << odleglosc<< endl;
- ostream_iterator<double> out_it(cout, ", ");
- cout << "Centroid A:" << endl<<endl;
- copy(centroidA.begin(), centroidA.end(), out_it);
- cout <<endl<<endl<< "Centroid B:" << endl;
- copy(centroidB.begin(), centroidB.end(), out_it);
- cout << endl;
- }
- double dodawanie(double a, double b)
- {
- return (double) (a + b);
- }
- double potega(double a, double b)
- {
- return (double)(pow((a-b),2.0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement