Skip to content
Snippets Groups Projects
Commit 66e291e0 authored by Simon MAZENOUX's avatar Simon MAZENOUX
Browse files

End labs

parent 5d819d63
No related branches found
No related tags found
No related merge requests found
Showing
with 547 additions and 59 deletions
......@@ -11,11 +11,11 @@ class Nuage
{
private:
using data_type = Point*;
using contenor_type = std::vector<data_type>;
contenor_type points;
using container_type = std::vector<data_type>;
container_type points;
public:
using const_iterator = contenor_type::const_iterator;
using const_iterator = container_type::const_iterator;
void ajouter(Point&);
const_iterator begin() const;
......
......@@ -34,16 +34,16 @@ message ( STATUS "Compiler flags: ${CMAKE_CXX_FLAGS}" )
# Sources #-----------------------------------------------------------------------------------------
set ( ZZ3_HEADERS
#src/cartesien.hpp
#src/nuage.hpp
#src/point.hpp
#src/polaire.hpp
src/cartesien.hpp
src/nuage.hpp
src/point.hpp
src/polaire.hpp
)
set ( ZZ3_SOURCES
#src/cartesien.cpp
#src/point.cpp
#src/polaire.cpp
src/cartesien.cpp
src/point.cpp
src/polaire.cpp
)
set ( ZZ3_HEADERS_TEST
......
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
#include "cartesien.hpp"
#include "polaire.hpp"
#include "nuage.hpp"
Cartesien barycentre_v1(Nuage<Cartesien> const & n)
{
if (n.size() == 0)
{
return Cartesien{};
}
double x = 0;
double y = 0;
for (Cartesien c: n)
{
x += c.getX();
y += c.getY();
}
return Cartesien(x/n.size(), y/n.size());
}
template <typename T>
Cartesien barycentre_v1(Nuage<T> const & n)
{
if (n.size() == 0)
{
return Cartesien{};
}
double x = 0;
double y = 0;
Cartesien c;
for (T point: n)
{
point.convertir(c);
x += c.getX();
y += c.getY();
}
return Cartesien(x/n.size(), y/n.size());
}
template <typename T>
Cartesien barycentre_v2(T const & n)
{
if (n.size() == 0)
{
return Cartesien{};
}
double x = 0;
double y = 0;
Cartesien c;
for (T::data_type point: n)
{
point.convertir(c);
x += c.getX();
y += c.getY();
}
return Cartesien(x/n.size(), y/n.size());
}
\ No newline at end of file
#include "cartesien.hpp"
#include "polaire.hpp"
Cartesien::Cartesien(Polaire const p)
{
p.convertir(*this);
}
Cartesien::Cartesien(double const x, double const y): x(x), y(y)
{}
double Cartesien::getX() const
{
return x;
}
double Cartesien::getY() const
{
return y;
}
void Cartesien::setX(double const _x)
{
x = _x;
}
void Cartesien::setY(double const _y)
{
y = _y;
}
void Cartesien::afficher(std::ostream& flux) const
{
flux << "(x=" << x << ";y=" << y << ")";
}
void Cartesien::convertir(Cartesien& c) const
{
c.setX(x);
c.setY(y);
}
void Cartesien::convertir(Polaire& p) const
{
p.setAngle(x == 0 ? 0 : atan(y / x) * 180 / M_PI);
p.setDistance(sqrt(pow(x, 2) + pow(y, 2)));
}
#ifndef CARTESIEN_HPP
#define CARTESIEN_HPP
#include "point.hpp"
class Cartesien: public Point
{
private:
double x;
double y;
public:
Cartesien(double const x = 0, double const y = 0);
Cartesien(Polaire const p);
double getX() const;
double getY() const;
void setX(double const x);
void setY(double const y);
void afficher(std::ostream& flux) const;
void convertir(Cartesien& c) const;
void convertir(Polaire& p) const;
};
#endif
\ No newline at end of file
#ifndef NUAGE_HPP
#define NUAGE_HPP
#include <vector>
template <typename T>
class Nuage
{
private:
using data_type = T;
using container_type = std::vector<data_type>;
container_type points;
public:
using const_iterator = typename container_type::const_iterator;
void ajouter(T const & );
const_iterator begin() const;
const_iterator end() const;
unsigned int size() const;
};
template <typename T>
void Nuage<T>::ajouter(T const & p)
{
points.push_back(p);
}
template <typename T>
typename Nuage<T>::const_iterator Nuage<T>::begin() const
{
return points.begin();
}
template <typename T>
typename Nuage<T>::const_iterator Nuage<T>::end() const
{
return points.end();
}
template <typename T>
unsigned int Nuage<T>::size() const
{
return points.size();
}
// Cartesien barycentre(Nuage const n)
// {
// double x = 0;
// double y = 0;
// Cartesien c;
// for (Point* point: n)
// {
// point->convertir(c);
// x += c.getX();
// y += c.getY();
// }
// return Cartesien(x/n.size(), y/n.size());
// }
// Cartesien BarycentreCartesien::operator()(Nuage const n)
// {
// return barycentre(n);
// }
// Polaire BarycentrePolaire::operator()(Nuage const n)
// {
// return Polaire(barycentre(n));
// }
// template <typename T>
// Cartesien barycentre(Nuage<T> const);
// class BarycentrePolaire
// {
// public:
// Polaire operator()(Nuage const);
// };
// class BarycentreCartesien
// {
// public:
// Cartesien operator()(Nuage const);
// };
#endif
\ No newline at end of file
#include "point.hpp"
std::ostream& operator<<(std::ostream& out, const Point& point)
{
point.afficher(out);
return out;
}
#ifndef POINT_HPP
#define POINT_HPP
#include <sstream>
#include <cmath>
class Cartesien;
class Polaire;
class Point
{
public:
virtual void afficher(std::ostream& flux) const = 0;
virtual void convertir(Cartesien& c) const = 0;
virtual void convertir(Polaire& p) const = 0;
};
std::ostream& operator<<(std::ostream& out, const Point& point);
#endif
\ No newline at end of file
#include "polaire.hpp"
Polaire::Polaire(double const angle, double const distance): angle(angle), distance(distance)
{}
Polaire::Polaire(Cartesien const c)
{
c.convertir(*this);
}
double Polaire::getAngle() const
{
return angle;
}
double Polaire::getDistance() const
{
return distance;
}
void Polaire::setAngle(double const a)
{
angle = a;
}
void Polaire::setDistance(double const d)
{
distance = d;
}
void Polaire::afficher(std::ostream& flux) const
{
flux << "(a=" << angle << ";d=" << distance << ")";
}
void Polaire::convertir(Cartesien& c) const
{
c.setX(distance * (cos(angle * M_PI / 180)));
c.setY(distance * (sin(angle * M_PI / 180)));
}
void Polaire::convertir(Polaire& p) const
{
p.setAngle(getAngle());
p.setDistance(getDistance());
}
\ No newline at end of file
#ifndef POLAIRE_HPP
#define POLAIRE_HPP
#include <iostream>
#include <sstream>
#include "point.hpp"
#include "cartesien.hpp"
class Polaire: public Point
{
private:
double angle;
double distance;
public:
Polaire(double const angle = 0, double const distance = 0);
Polaire(Cartesien const c);
double getAngle() const;
double getDistance() const;
void setAngle(double const a);
void setDistance(double const d);
void afficher(std::ostream& flux) const override;
void convertir(Cartesien& c) const override;
void convertir(Polaire& p) const override;
};
#endif
\ No newline at end of file
......@@ -2,13 +2,15 @@
#include "catch.hpp"
#include <cmath>
//#include <nuage.hpp>
#include <cartesien.hpp>
#include <polaire.hpp>
#include <nuage.hpp>
#include "barycentre.cpp"
// Tests //-----------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------ 1
/*TEST_CASE ( "TP2_Nuage::Ajout" ) {
TEST_CASE ( "TP2_Nuage::Ajout" ) {
Nuage<Cartesien> n;
REQUIRE ( n.size() == 0u );
......@@ -19,10 +21,10 @@
n.ajouter(Cartesien(34,56));
REQUIRE ( n.size() == 4u );
}*/
}
//------------------------------------------------------------------------------------------------ 2
/*TEST_CASE ( "TP2_Nuage::Iterateurs" ) {
TEST_CASE ( "TP2_Nuage::Iterateurs" ) {
Polaire p1(12,34);
Polaire p2(56,78);
Polaire p3(90,12);
......@@ -49,10 +51,10 @@
REQUIRE ( t[2].getDistance() == Approx(p3.getDistance()) );
REQUIRE ( t[3].getAngle() == Approx(p4.getAngle()) );
REQUIRE ( t[3].getDistance() == Approx(p4.getDistance()) );
}*/
}
//------------------------------------------------------------------------------------------------ 3
/*TEST_CASE ( "TP2_Nuage::BarycentreCartesien_V1" ) {
TEST_CASE ( "TP2_Nuage::BarycentreCartesien_V1" ) {
Nuage<Cartesien> n;
Cartesien p1(12,34);
......@@ -80,10 +82,10 @@
REQUIRE ( b3.getX() == Approx((p1.getX()+p2.getX()+p3.getX()+p4.getX())/4) );
REQUIRE ( b3.getY() == Approx((p1.getY()+p2.getY()+p3.getY()+p4.getY())/4) );
}*/
}
//----------------------------------------------------------------------------------------------- 4a
/*TEST_CASE ( "TP2_Nuage::BarycentrePolaire_V1" ) {
// //----------------------------------------------------------------------------------------------- 4a
TEST_CASE ( "TP2_Nuage::BarycentrePolaire_V1" ) {
Nuage<Polaire> n;
Polaire p1(12,34);
......@@ -111,44 +113,44 @@
REQUIRE ( b3.getAngle() == Approx(43.017260).epsilon(1e-3) );
REQUIRE ( b3.getDistance() == Approx(42.159772).epsilon(1e-3) );
}*/
}
//----------------------------------------------------------------------------------------------- 4b
/*TEST_CASE ( "TP2_Nuage::BarycentrePolaire_V1" ) {
Nuage<Polaire> n;
// TEST_CASE ( "TP2_Nuage::BarycentrePolaire_V1" ) {
// Nuage<Polaire> n;
Polaire p1(12,34);
Polaire p2(56,78);
Polaire p3(90,12);
Polaire p4(34,56);
// Polaire p1(12,34);
// Polaire p2(56,78);
// Polaire p3(90,12);
// Polaire p4(34,56);
Polaire b1 = barycentre_v1(n);
// Polaire b1 = barycentre_v1(n);
REQUIRE ( b1.getAngle() == Approx(0.0) );
REQUIRE ( b1.getDistance() == Approx(0.0) );
// REQUIRE ( b1.getAngle() == Approx(0.0) );
// REQUIRE ( b1.getDistance() == Approx(0.0) );
n.ajouter(p1);
// n.ajouter(p1);
Polaire b2 = barycentre_v1(n);
// Polaire b2 = barycentre_v1(n);
REQUIRE ( b2.getAngle() == Approx(p1.getAngle()) );
REQUIRE ( b2.getDistance() == Approx(p1.getDistance()) );
// REQUIRE ( b2.getAngle() == Approx(p1.getAngle()) );
// REQUIRE ( b2.getDistance() == Approx(p1.getDistance()) );
n.ajouter(p2);
n.ajouter(p3);
n.ajouter(p4);
// n.ajouter(p2);
// n.ajouter(p3);
// n.ajouter(p4);
Polaire b3 = barycentre_v1(n);
// Polaire b3 = barycentre_v1(n);
REQUIRE ( b3.getAngle() == Approx((p1.getAngle()+p2.getAngle()
+p3.getAngle()+p4.getAngle())/4) );
// REQUIRE ( b3.getAngle() == Approx((p1.getAngle()+p2.getAngle()
// +p3.getAngle()+p4.getAngle())/4) );
REQUIRE ( b3.getDistance() == Approx((p1.getDistance()+p2.getDistance()
+p3.getDistance()+p4.getDistance())/4) );
}*/
// REQUIRE ( b3.getDistance() == Approx((p1.getDistance()+p2.getDistance()
// +p3.getDistance()+p4.getDistance())/4) );
// }
//------------------------------------------------------------------------------------------------ 5
/*TEST_CASE ( "TP2_Nuage::BarycentreCartesien_V2" ) {
TEST_CASE ( "TP2_Nuage::BarycentreCartesien_V2" ) {
Nuage<Cartesien> n;
Cartesien p1(12,34);
......@@ -176,10 +178,10 @@
REQUIRE ( b3.getX() == Approx((p1.getX()+p2.getX()+p3.getX()+p4.getX())/4) );
REQUIRE ( b3.getY() == Approx((p1.getY()+p2.getY()+p3.getY()+p4.getY())/4) );
}*/
}
//------------------------------------------------------------------------------------------------ 6
/*TEST_CASE ( "TP2_Nuage::BarycentreCartesienVecteur" ) {
TEST_CASE ( "TP2_Nuage::BarycentreCartesienVecteur" ) {
std::vector<Cartesien> n;
Cartesien p1(12,34);
......@@ -207,7 +209,7 @@
REQUIRE ( b3.getX() == Approx((p1.getX()+p2.getX()+p3.getX()+p4.getX())/4) );
REQUIRE ( b3.getY() == Approx((p1.getY()+p2.getY()+p3.getY()+p4.getY())/4) );
}*/
}
//------------------------------------------------------------------------------------------------ 7
/*TEST_CASE ( "TP2_Nuage::BarycentrePolaire_V2" ) {
......
......@@ -5,25 +5,31 @@
#include "exception.hpp"
template <typename ... Args> std::string const chaine(Args... args)
{
return ((chaine(args) + " ") + ...);
}
template <typename T> std::string const chaine(T x)
{
throw ExceptionChaine(x);
}
std::string const chaine(int x)
template <> std::string const chaine(int x)
{
return std::to_string(x);
}
std::string const chaine(std::string const & x)
template <> std::string const chaine(std::string const & x)
{
return x;
}
std::string const chaine(double x)
template <> std::string const chaine(double x)
{
return std::to_string(x);
}
#endif
\ No newline at end of file
......@@ -90,7 +90,7 @@ TEST_CASE ( "TP3_Chaine::ConversionSimple" ) {
}
//------------------------------------------------------------------------------------------------ 3
/*TEST_CASE ( "TP3_Chaine::ConversionVariadic" ) {
TEST_CASE ( "TP3_Chaine::ConversionVariadic" ) {
std::string n = "Smith";
int i = 10;
double d = 13.27;
......@@ -118,10 +118,10 @@ TEST_CASE ( "TP3_Chaine::ConversionSimple" ) {
}
REQUIRE ( erreur == 2 );
}*/
}
//------------------------------------------------------------------------------------------------ 4
/*TEST_CASE ( "TP3_Chaine::ConversionTuple1" ) {
TEST_CASE ( "TP3_Chaine::ConversionTuple1" ) {
std::tuple<std::string,int,double> t1{"Smith",10,13.27};
std::tuple<std::string,int,double,long> t2{"Smith",10,13.27,100};
......@@ -147,7 +147,7 @@ TEST_CASE ( "TP3_Chaine::ConversionSimple" ) {
}
REQUIRE ( erreur == 2 );
}*/
}
//------------------------------------------------------------------------------------------------ 5
using Identite = std::tuple<std::string,std::string>; // {nom,prenom}
......
......@@ -34,14 +34,17 @@ message ( STATUS "Compiler flags: ${CMAKE_CXX_FLAGS}" )
# Sources #-----------------------------------------------------------------------------------------
set ( ZZ3_HEADERS
#src/classe.hpp
src/classe.hpp
#src/comparateur.hpp
#src/echantillon.hpp
#src/histogramme.hpp
#src/valeur.hpp
src/echantillon.hpp
src/histogramme.hpp
src/valeur.hpp
)
set ( ZZ3_SOURCES
src/classe.cpp
src/echantillon.cpp
src/valeur.cpp
)
set ( ZZ3_HEADERS_TEST
......
#include "classe.hpp"
Classe::Classe(double const & lower_bound, double const & upper_bound): _lower_bound(lower_bound), _upper_bound(upper_bound), _quantity(0)
{}
double Classe::getBorneInf() const
{
return _lower_bound;
}
double Classe::getBorneSup() const
{
return _upper_bound;
}
unsigned int Classe::getQuantite() const
{
return _quantity;
}
void Classe::setBorneInf(double const lower_bound)
{
_lower_bound = lower_bound;
}
void Classe::setBorneSup(double const upper_bound)
{
_upper_bound = upper_bound;
}
void Classe::setQuantite(unsigned const quantity)
{
_quantity = quantity;
}
void Classe::ajouter()
{
_quantity++;
}
bool operator<(const Classe &a, const Classe &b)
{
return a.getBorneInf() < b.getBorneInf();
}
bool operator>(Classe const &a, Classe const &b)
{
return a.getBorneInf() > b.getBorneInf();
}
#ifndef CLASSE_HPP
#define CLASSE_HPP
class Classe
{
private:
double _lower_bound;
double _upper_bound;
double _quantity;
public:
Classe(double const & lower_bound, double const & upper_bound);
double getBorneInf() const;
double getBorneSup() const;
unsigned int getQuantite() const;
void setBorneInf(double const);
void setBorneSup(double const);
void setQuantite(unsigned const n);
void ajouter();
};
bool operator<(const Classe &a, const Classe &b);
bool operator>(Classe const &a, Classe const &b);
#endif
\ No newline at end of file
#ifndef COMPARATEUR_HPP
#define COMPARATEUR_HPP
template<class T>
class ComparateurQuantite
{
public:
bool operator()(T const & t1, T const & t2)
{
return (t1.getQuantite() == t2.getQuantite() && t1.getBorneInf() < t2.getBorneInf() || t1.getQuantite() > t2.getQuantite());
}
};
#endif
\ No newline at end of file
#include "echantillon.hpp"
unsigned int Echantillon::getTaille() const
{
return _valeurs.size();
}
void Echantillon::ajouter(double const v)
{
_valeurs.push_back(v);
}
Valeur Echantillon::getMinimum() const
{
if (_valeurs.size() == 0){
throw std::domain_error("Empty list");
}
return *std::min_element(_valeurs.begin(), _valeurs.end(), [] (Valeur const & a, Valeur const & b) {return a.getNombre() < b.getNombre();});
}
Valeur Echantillon::getMaximum() const
{
if (_valeurs.size() == 0){
throw std::domain_error("Empty list");
}
return *std::max_element(_valeurs.begin(), _valeurs.end(), [] (Valeur const & a, Valeur const & b) {return a.getNombre() < b.getNombre();});
}
Valeur Echantillon::getValeur(unsigned int const i) const
{
if (_valeurs.size() <= i){
throw std::out_of_range("");
}
return _valeurs[i];
}
\ No newline at end of file
#ifndef ECHANTILLON_HPP
#define ECHANTILLON_HPP
#include <algorithm>
#include <stdexcept>
#include <vector>
#include "valeur.hpp"
class Echantillon
{
private:
std::vector<Valeur> _valeurs;
public:
unsigned int getTaille() const;
void ajouter(double const v);
Valeur getMinimum() const;
Valeur getMaximum() const;
Valeur getValeur(unsigned int const i) const;
};
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment