Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- #include <iostream>
- #include <functional>
- #include <numeric>
- #include <cmath>
- using namespace std;
- // Funkcje pomocnicze aka "boilerplate"
- int wiekszy(int x, int y) { return max(x,y); }
- int mniejszy(int x, int y) { return min(x,y); }
- float kwadrat(float x) { return x * x; }
- // Klasy
- struct Gracz {
- int _kolejki;
- int _kolejki_wlasne;
- int _pozycja;
- Gracz() : _kolejki(0), _kolejki_wlasne(0), _pozycja(0) {}
- // Gettery aka "boilerplate"
- int kolejki() { return _kolejki; }
- int kolejki_wlasne() { return _kolejki_wlasne; }
- };
- struct Plansza {
- vector<Gracz> gracze;
- Plansza(int n) : gracze(n, Gracz()) {
- if(n<1) throw new exception("Zbyt mala ilosc graczy!");
- }
- float srednia(int (Gracz::*transf)()) {
- auto func = [&transf](int z, Gracz x) {
- return z + (x.*transf)();
- };
- return static_cast<float>( accumulate(gracze.begin(), gracze.end(), 0, func) ) / static_cast<float>( gracze.size() );
- }
- pair<float,float> srednia_i_odchylenie(int (Gracz::*transf)()) {
- float ssrednia = srednia(transf);
- auto func2 = [&transf, &ssrednia](float z, Gracz x) {
- return z + kwadrat(static_cast<float>((x.*transf)()) - ssrednia);
- };
- float wariancja = accumulate(gracze.begin(), gracze.end(), 0.0f, func2);
- return make_pair(srednia, sqrt(wariancja));
- }
- int naj(int (*najf)(int, int), int (Gracz::*transf)()) {
- auto drugi = gracze.begin(); ++drugi;
- auto func = [&transf, &najf](int z, Gracz x) {
- return najf(z, (x.*transf)());
- };
- return accumulate(drugi, gracze.end(), (gracze.front().*transf)(), func);
- }
- };
- int main() {
- Plansza p(10);
- // HELL YEAH!
- cout << p.srednia(&Gracz::kolejki);
- cout << p.naj(wiekszy, &Gracz::kolejki);
- cout << p.naj(mniejszy, &Gracz::kolejki_wlasne);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement