Loading lang_cpp_tr_01...
<cmath>,
<iostream>, usage du préfixe std::...)
std::cout <<...)
<string>, permettent de transformer une valeur numérique en chaîne C++.
<stdexcept>.
<algorithm>, pourront vous être utiles ici.
//----------------------------------------------------------------------------
#ifndef BASICS_MY_MODULE_HPP #define BASICS_MY_MODULE_HPP
namespace basics {
double my_function(double a, double b);
} // namespace basics
#endif // BASICS_MY_MODULE_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "my_module.hpp" #include <cmath>
namespace basics {
double my_function(double a, double b) { return std::sqrt(a)+std::log(b); }
} // namespace basics
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#ifndef BASICS_BOUNDING_BOX_HPP #define BASICS_BOUNDING_BOX_HPP
#include <string>
namespace basics {
struct BoundingBox { int x, y, w, h; };
static_assert(sizeof(BoundingBox)<=16, "a smaller size was expected for BoundingBox");
BoundingBox make_BoundingBox(int x, int y, int w=0, int h=0);
std::string to_string(BoundingBox b);
inline bool contains(BoundingBox b, int x, int y) { return (x>=b.x)&&(x<=b.x+b.w)&& (y>=b.y)&&(y<=b.y+b.h); }
inline bool // ob contains ib contains(BoundingBox ob, // outer-box BoundingBox ib) // inner-box { return contains(ob, ib.x, ib.y)&& contains(ob, ib.x+ib.w, ib.y+ib.h); }
BoundingBox operator+(BoundingBox lhs, // left-hand-side BoundingBox rhs); // right-hand-side
} // namespace basics
#endif // BASICS_BOUNDING_BOX_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "bounding_box.hpp" #include <stdexcept> #include <algorithm>
namespace basics {
BoundingBox make_BoundingBox(int x, int y, int w, int h) { if((w<0)||(h<0)) { throw std::runtime_error{"dimensions should not be negative"}; } return BoundingBox{x, y, w, h}; }
std::string to_string(BoundingBox b) { return "BoundingBox{x="+std::to_string(b.x)+ ", y="+std::to_string(b.y)+ ", w="+std::to_string(b.w)+ ", h="+std::to_string(b.h)+"}"; }
BoundingBox operator+(BoundingBox lhs, // left-hand-side BoundingBox rhs) // right-hand-side { const int left =std::min(lhs.x , rhs.x ); const int top =std::min(lhs.y , rhs.y ); const int right =std::max(lhs.x+lhs.w, rhs.x+rhs.w); const int bottom=std::max(lhs.y+lhs.h, rhs.y+rhs.h); return BoundingBox{left, top, right-left, bottom-top}; }
} // namespace basics
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "my_module.hpp" #include "bounding_box.hpp" #include <iostream> #include <vector>
void test_my_function() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; for(int j=0; j<4; ++j) { const double b=1.5*(j+1); for(int i=0; i<4; ++i) { const double a=0.5*(i+1); std::cout << "a=" << a << " b=" << b << " --> " << basics::my_function(a, b) << '\n'; } } }
void test_BoundingBox() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; // basics::BoundingBox b1=basics::make_BoundingBox(20, 20, 30, -40); const auto bb1=basics::make_BoundingBox(10, 20, 30, 40); std::cout << "bb1: " << to_string(bb1) << '\n'; const auto bb2=basics::make_BoundingBox(50, 60); std::cout << "bb2: " << to_string(bb2) << '\n'; // std::cout << "bb1 contains bb2? " << contains(bb1, bb2) << '\n'; std::cout << "bb2 contains bb1? " << contains(bb2, bb1) << '\n'; const auto bb3=bb1+bb2; std::cout << "bb3: " << to_string(bb3) << '\n'; std::cout << "bb3 contains bb1? " << contains(bb3, bb1) << '\n'; std::cout << "bb3 contains bb2? " << contains(bb3, bb2) << '\n'; // std::vector<basics::BoundingBox> boxes={bb1, bb2, bb3}; boxes.emplace_back(basics::make_BoundingBox(100, 200, 300, 400)); for(auto &elem: boxes) { elem.x+=100; } while(!empty(boxes)) { std::cout << "size is " << size(boxes) << '\n'; for(const auto &elem: boxes) { std::cout << " " << to_string(elem) << '\n'; } boxes.pop_back(); } }
int main() { test_my_function(); test_BoundingBox(); return 0; }
//----------------------------------------------------------------------------