Loading lang_cpp_ex_0402...
nouvelle_borne_inférieure = std::min( ancienne_borne_inférieure , value); nouvelle_borne_supérieure = std::max( ancienne_borne_supérieure , value);
void test_Range() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; // ... }
nouvelle_quantité_de_carburant = std::max( 0.0 , ancienne_quantité_de_carburant - distance * consommation );
void test_Vehicle() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; // ... }
//----------------------------------------------------------------------------
#ifndef ENCAP_STRUCT_HPP #define ENCAP_STRUCT_HPP
#include <vector> #include <string>
namespace encap {
class Range { public:
Range(int low, int high);
Range(int width);
bool contains(int value) const;
void extend(int value);
private: int low_, high_; };
bool contains(const Range &r, const std::vector<int> &values);
void extend(Range &r, const std::vector<int> &values);
class Vehicle { public:
Vehicle(double fuel, double consumption);
Vehicle(double consumption);
double autonomy() const;
void reach(double distance);
private: double fuel_, consumption_; };
bool reachable(const Vehicle &v, const std::vector<double> &distances);
void reach(Vehicle &v, const std::vector<double> &distances);
} // namespace encap
#endif // ENCAP_STRUCT_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "struct.hpp"
#include <stdexcept> #include <algorithm> #include <numeric>
namespace encap {
Range::Range(int low, int high) : low_{low} , high_{high} { if(low>high) { throw std::runtime_error{"incorrect bounds"}; } }
Range::Range(int width) : Range{0, width} { }
bool Range::contains(int value) const { return (value>=low_)&&(value<high_); }
void Range::extend(int value) { low_=std::min(low_, value); high_=std::max(high_, value); }
bool contains(const Range &r, const std::vector<int> &values) { #if 0 for(const auto &elem: values) { if(!r.contains(elem)) { return false; } } return true; #else return std::find_if(cbegin(values), cend(values), [&](const auto &elem) { return !r.contains(elem); })==cend(values); #endif }
void extend(Range &r, const std::vector<int> &values) { for(const auto &elem: values) { r.extend(elem); } }
Vehicle::Vehicle(double fuel, double consumption) : fuel_{fuel} , consumption_{consumption} { if(consumption<=0.0) { throw std::runtime_error{"positive consumption expected"}; } }
Vehicle::Vehicle(double consumption) : Vehicle{40.0, consumption} { }
double Vehicle::autonomy() const { return fuel_/consumption_; }
void Vehicle::reach(double distance) { fuel_=std::max(0.0, fuel_-distance*consumption_); }
bool reachable(const Vehicle &v, const std::vector<double> &distances) { #if 0 double total_distance=0.0; for(const auto &elem: distances) { total_distance+=elem; } #else const double total_distance= std::accumulate(cbegin(distances), cend(distances), 0.0); #endif return v.autonomy()>=total_distance; }
void reach(Vehicle &v, const std::vector<double> &distances) { for(const auto &elem: distances) { v.reach(elem); } }
} // namespace encap
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "struct.hpp"
#include <iostream>
void test_Range() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; encap::Range r{2}; std::cout << "r contains 3? " << r.contains(3) << '\n'; r.extend(5); std::cout << "r contains 3? " << r.contains(3) << '\n'; const std::vector<int> v={2, 4, 6, 8, 10}; std::cout << "r contains v? " << contains(r, v) << '\n'; extend(r, {3, 6, 9, 12}); std::cout << "r contains v? " << contains(r, v) << '\n'; }
void test_Vehicle() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; encap::Vehicle v{5.0/100.0}; std::cout << "autonomy of v: " << v.autonomy() << '\n'; v.reach(175.5); std::cout << "autonomy of v: " << v.autonomy() << '\n'; const std::vector<double> d={125.0, 40.5, 265.0, 180.5}; std::cout << "d reachable by v? " << reachable(v, d) << '\n'; reach(v, d); std::cout << "d reachable by v? " << reachable(v, d) << '\n'; }
int main() { test_Range(); test_Vehicle(); return 0; }
//----------------------------------------------------------------------------