Skip to content

Instantly share code, notes, and snippets.

@thevar1able
Created November 9, 2015 22:02
Show Gist options
  • Select an option

  • Save thevar1able/3e5ab2300518f34ef621 to your computer and use it in GitHub Desktop.

Select an option

Save thevar1able/3e5ab2300518f34ef621 to your computer and use it in GitHub Desktop.
#include <functional> // 1.1.7.28
#include <algorithm>
#include <iostream>
#include <utility>
#include <string>
#include <vector>
#include <cmath>
typedef struct {
int ground, has_motor, four_wheel, electric;
} data;
typedef struct {
int a, b, g, h;
} abgh;
class abstract_transporter {
protected:
const data reference;
public:
const std::string name;
abstract_transporter(const std::string name, const data reference) :
reference (reference),
name (name)
{};
friend std::ostream& operator<< (std::ostream& stream, const abstract_transporter& vehicle);
virtual float test_hamming(const data input) {
float l = 0;
l = abs(reference.ground - input.ground)
+ abs(reference.has_motor - input.has_motor)
+ abs(reference.four_wheel - input.four_wheel)
+ abs(reference.electric - input.electric);
return l;
};
abgh abgh_counter(const data input) {
abgh t = {0, 0, 0, 0};
t.a = reference.ground * input.ground
+ reference.electric * input.electric
+ reference.four_wheel * input.four_wheel
+ reference.has_motor * input.has_motor;
t.b = (1 - reference.ground) * (1 - input.ground)
+ (1 - reference.electric) * (1 - input.electric)
+ (1 - reference.four_wheel) * (1 - input.four_wheel)
+ (1 - reference.has_motor) * (1 - input.has_motor);
t.g = reference.ground * (1 - input.ground)
+ reference.electric * (1 - input.electric)
+ reference.four_wheel * (1 - input.four_wheel)
+ reference.has_motor * (1 - input.has_motor);
t.h = (1 - reference.ground) * input.ground
+ (1 - reference.electric) * input.electric
+ (1 - reference.four_wheel) * input.four_wheel
+ (1 - reference.has_motor) * input.has_motor;
return t;
};
virtual float test_s1(const data input) {
abgh t = abgh_counter(input);
return ((float)(t.a) / (float)(t.a + t.b + t.g + t.h)) - ((float)t.a / (float)t.h);
};
virtual float test_s2(const data input) {
abgh t = abgh_counter(input);
return ((float)(t.a) / (float)(t.h - t.b));
};
virtual float test_s3(const data input) {
abgh t = abgh_counter(input);
return ((float)(t.a) / (float)(2 * t.a + t.g + t.h));
};
};
std::ostream& operator<< (std::ostream& stream, const abstract_transporter& vehicle) {
stream << vehicle.name << ": "
<< "\t" << vehicle.reference.ground
<< "\t" << vehicle.reference.has_motor
<< "\t" << vehicle.reference.four_wheel
<< "\t" << vehicle.reference.electric;
return stream;
};
class bus : public abstract_transporter {
public:
bus() : abstract_transporter("Автобус", {1, 1, 1, 0}) {};
};
class trolley : public abstract_transporter {
public:
trolley(): abstract_transporter("Трамвай", {1, 1, 0, 1}) {};
};
class helicopter : public abstract_transporter {
public:
helicopter() : abstract_transporter("Вертолёт", {0, 1, 0, 0}) {};
};
class bicycle : public abstract_transporter {
public:
bicycle() : abstract_transporter("Велосипед", {1, 0, 0, 0}) {};
};
abstract_transporter tester(std::vector<abstract_transporter*> vehicles, std::function<float(abstract_transporter*)> lambda) {
std::vector<float> temp(vehicles.size());
std::transform(vehicles.begin(), vehicles.end(), temp.begin(), lambda);
auto r = std::min_element(temp.begin(), temp.end());
return *(vehicles.at(std::distance(temp.begin(), r)));
}
int main() {
std::vector<abstract_transporter*> vehicles;
vehicles.push_back(new helicopter());
vehicles.push_back(new trolley());
vehicles.push_back(new bicycle());
vehicles.push_back(new bus());
std::cout<<"Эталонные данные; ездит, есть мотор, четырёхколёсный, электрический"<<std::endl;
for(auto x : vehicles) {
std::cout << *x << std::endl;
}
data input = {0, 0, 0, 0};
std::cout << "Введите данные: ";
std::cin >> input.ground >> input.has_motor >> input.four_wheel >> input.electric;
std::cout << "Скорее всего это "
<< tester(vehicles, std::function<float(abstract_transporter*)>(
[input](abstract_transporter* x){
return x->test_hamming(input);
})).name
<< " (Хэмминг)"
<< std::endl;
std::cout << "Скорее всего это "
<< tester(vehicles, std::function<float(abstract_transporter*)>(
[input](abstract_transporter* x){
return x->test_s1(input);
})).name
<< " (S1)"
<< std::endl;
std::cout << "Скорее всего это "
<< tester(vehicles, std::function<float(abstract_transporter*)>(
[input](abstract_transporter* x){
return x->test_s2(input);
})).name
<< " (S2)"
<< std::endl;
std::cout << "Скорее всего это "
<< tester(vehicles, std::function<float(abstract_transporter*)>(
[input](abstract_transporter* x){
return x->test_s3(input);
})).name
<< " (S3)"
<< std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment