Skip to content

Instantly share code, notes, and snippets.

@jkomyno
Last active January 29, 2018 12:40
Show Gist options
  • Save jkomyno/777944007838870a2e0c0cdebfb5422d to your computer and use it in GitHub Desktop.
Save jkomyno/777944007838870a2e0c0cdebfb5422d to your computer and use it in GitHub Desktop.
P2 - 1° appello 2015
#pragma once
#include <string>
#include <vector>
#include <list>
class Allenamento {
private:
int span;
public:
Allenamento(int s) :
span(s) {}
virtual ~Allenamento() {}
virtual Allenamento* clone() const = 0;
virtual int calorie() const = 0;
int Span() const {
return span;
}
};
class Ciclismo : public Allenamento {
private:
double dist;
double dist_climb;
static int get_calorie(double K, int D, double S) {
return 200 * static_cast<int>(K*K / D) + 100 * S;
}
public:
Ciclismo(int s, double d, double dc) :
Allenamento(s),
dist(d),
dist_climb(dc) {}
Ciclismo* clone() const {
return new Ciclismo(*this);
}
int calorie() const {
return get_calorie(dist, Span(), dist_climb);
}
double Dist() const {
return dist;
}
double Climb() const {
return dist_climb;
}
};
class Corsa : public Allenamento {
private:
double dist;
double dist_dirt;
static int get_calorie(double K, int D) {
return static_cast<int>((600 * K * K) / D);
}
public:
Corsa(int s, double d, double dd) :
Allenamento(s),
dist(d),
dist_dirt(dd) {}
Corsa* clone() const {
return new Corsa(*this);
}
int calorie() const {
return get_calorie(dist, Span());
}
double Dirt() const {
return dist_dirt;
}
};
class Nuoto : public Allenamento {
private:
int laps;
int style;
public:
static enum STYLES {
LIBERO,
RANA
};
Nuoto(int s, int l, int st) :
Allenamento(s),
laps(l),
style(st) {}
Nuoto* clone() const {
return new Nuoto(*this);
}
int calorie() const {
return 35 * laps;
}
int Style() const {
return style;
}
};
class FitApp {
private:
std::vector<const Allenamento*> v;
typedef std::vector<const Allenamento*>::const_iterator ci;
static double get_perc(const Ciclismo* cc) {
return (cc->Climb() / cc->Dist()) * 100;
}
static bool is_new_record(Corsa* p, const std::list<const Corsa*>& lst) {
double curr_record = 0;
typedef std::list<const Corsa*>::const_iterator cil;
for (cil it = lst.cbegin(); it != lst.cend(); it++) {
double curr_dirt = (*it)->Dirt();
if (curr_dirt > curr_record) {
curr_record = curr_dirt;
}
}
return p->Dirt() > curr_record;
}
public:
FitApp(const std::vector<const Allenamento*>& cv) :
v(cv) {}
std::vector<Ciclismo> salita(double perc) const {
std::vector<Ciclismo> result;
for (ci it = v.cbegin(); it != v.cend(); it++) {
const Ciclismo* cc = dynamic_cast<const Ciclismo*>(*it);
if (cc && get_perc(cc) > perc) {
result.push_back(*cc);
}
}
return result;
}
std::vector<Allenamento*> calorie(int x) const {
std::vector<Allenamento*> result;
for (ci it = v.cbegin(); it != v.cend(); it++) {
if ((*it)->calorie() > x) {
const Nuoto* cn = dynamic_cast<const Nuoto*>(*it);
if (!cn || cn->Style() == cn->LIBERO) {
Allenamento* a = const_cast<Allenamento*>((*it)->clone());
result.push_back(a);
}
}
}
return result;
}
void insert(Corsa* p) throw(std::string) {
std::list<const Corsa*> lst;
for (ci it = v.cbegin(); it != v.cend(); it++) {
const Corsa* cc = dynamic_cast<const Corsa*>(*it);
if (cc) {
lst.push_back(cc);
}
}
if (is_new_record(p, lst)) {
v.push_back(p);
}
else {
throw std::string("No Insert");
}
}
int conta(double limit) {
int count = 0;
[&]() -> void {
for (ci it = v.cbegin(); it != v.cend(); it++) {
if ((*it)->Span() > limit) {
count++;
}
}
}();
return count;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment