| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 
 | #include <fstream>
#include <boost/random/triangle_distribution.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <ctime>
#include <sstream>
#include <string>
#include <iostream>
/*
 * Une classe pour encapsuler les éléments de Boost.Random.
 * Elle-même un foncteur.
 */
class TriangleRandomGenerator
{
public:
    // Le constructeur prend les trois valeurs dont tu parles: a (borne basse),
    // c (borne haute) et b (valeur la plus probable).
    TriangleRandomGenerator(double a, double b, double c);
 
    // Ici on fabrique un foncteur, mais on pourrait aussi bien avoir une méthode
    // comme "double next_number();" qui ferait le même effet
    double operator()();
 
private:
    boost::mt19937 engine;
    boost::triangle_distribution<double> generator;
    boost::variate_generator<boost::mt19937& ,boost::triangle_distribution<double> > binded;
};
 
TriangleRandomGenerator::TriangleRandomGenerator(double a, double b, double c):
engine(static_cast<long unsigned int>(time(0)))
, generator(a, b, c)
, binded(engine, generator)
{
}
 
double TriangleRandomGenerator::operator()()
{
    return binded();
}
 
 
int main()
{
    double VN, max, min;
 
    //récupération de la valeur nominale du fichier texte "valeur.txt"
    std::ifstream fichier( "valeur.txt" );
    // variable contenant chaque ligne lue
    std::string ligne;
 
    while (std::getline( fichier, ligne ) )
    {
 
        std::string string1 = ligne;
        std::string string2;
        string2 = string1.substr(0, string1.find(';'));
        string1 = string1.substr(string1.find(';')+1);
        //conversion des chaines
        std::istringstream iss( string2 );
        iss >> VN;
        std::istringstream iss1( string1 );
        iss1 >> max;
        //intervalle [min,max]
        min=max*(-1);
        //affichage
        std :: cout << VN << std::endl;
        std :: cout << max << std::endl;
        std :: cout << min << std::endl;
 
 
 
        TriangleRandomGenerator gen(min, VN, max);
        std::ofstream file("fichier.txt", std::ios_base::app);
        for (int i = 0; i < 10; ++i)
            {
                file << gen() << "\n";
            }
        file.flush();
 
    }
 
} | 
Partager