Advertisement
kpfp_linux

Trochę zabawy, czyli początek alkoc++

Jul 10th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <functional>
  6. #include <numeric>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13. // Funkcje pomocnicze aka "boilerplate"
  14. int wiekszy(int x, int y) { return max(x,y); }
  15. int mniejszy(int x, int y) { return min(x,y); }
  16. float kwadrat(float x) { return x * x; }
  17.  
  18.  
  19.  
  20. // Klasy
  21. struct Gracz {
  22.     int _kolejki;
  23.     int _kolejki_wlasne;
  24.     int _pozycja;
  25.  
  26.     Gracz() : _kolejki(0), _kolejki_wlasne(0), _pozycja(0) {}
  27.  
  28.     // Gettery aka "boilerplate"
  29.     int kolejki() { return _kolejki; }
  30.     int kolejki_wlasne() { return _kolejki_wlasne; }
  31. };
  32.  
  33.  
  34. struct Plansza {
  35.     vector<Gracz> gracze;
  36.  
  37.     Plansza(int n) : gracze(n, Gracz()) {
  38.         if(n<1) throw new exception("Zbyt mala ilosc graczy!");
  39.     }
  40.  
  41.     float srednia(int (Gracz::*transf)()) {
  42.         auto func = [&transf](int z, Gracz x) {
  43.             return z + (x.*transf)();
  44.         };
  45.         return static_cast<float>( accumulate(gracze.begin(), gracze.end(), 0, func) ) / static_cast<float>( gracze.size() );
  46.     }
  47.  
  48.     pair<float,float> srednia_i_odchylenie(int (Gracz::*transf)()) {
  49.         float ssrednia = srednia(transf);
  50.         auto func2 = [&transf, &ssrednia](float z, Gracz x) {
  51.             return z + kwadrat(static_cast<float>((x.*transf)()) - ssrednia);
  52.         };
  53.         float wariancja = accumulate(gracze.begin(), gracze.end(), 0.0f, func2);
  54.         return make_pair(srednia, sqrt(wariancja));
  55.     }
  56.  
  57.     int naj(int (*najf)(int, int), int (Gracz::*transf)()) {
  58.         auto drugi = gracze.begin(); ++drugi;
  59.         auto func = [&transf, &najf](int z, Gracz x) {
  60.             return najf(z, (x.*transf)());
  61.         };
  62.         return accumulate(drugi, gracze.end(), (gracze.front().*transf)(), func);
  63.     }
  64. };
  65.  
  66.  
  67.  
  68. int main() {
  69.     Plansza p(10);
  70.  
  71.     // HELL YEAH!
  72.     cout << p.srednia(&Gracz::kolejki);
  73.     cout << p.naj(wiekszy, &Gracz::kolejki);
  74.     cout << p.naj(mniejszy, &Gracz::kolejki_wlasne);
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement