IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C++ Discussion :

Matrice générique w


Sujet :

C++

  1. #1
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut Matrice générique w
    Bonjour,

    Étant donnée que j'utilise souvent les matrices lorsque je programme en C++, j'ai décidé de faire une classe "Matrice" capable de tout faire (addition, multiplication, inverse, transposer etc ...). J'ai besoin d'aide sur certaine choses que je ne sais pas faire et pour améliorer ce que j'ai déjà fait.

    Ma classe "Matrice" est générique et doit pouvoir contenir n'importe quelle objet.
    Lorsqu'elle contient des nombres (int, double, bool etc ...), on doit pouvoir faire les opérations usuelles sur les matrices.
    Je dois pouvoir afficher ma matrice grâce à l'opérateur << (peut importe le type d'objet quelle contient).

    Mon code pour l'instant :

    Matrice.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    // Gardien //---------------------------------------------------------------------------------------------
    #ifndef MATRICE_H
    #define MATRICE_H
     
    // En-tête //---------------------------------------------------------------------------------------------
     
    /* !> ------------------------------------------------------------------------------------------------- <!
       !>  Classe : Matrice                                                                                 <!
       !>                                                                                                   <!
       !>  Principe : Classe représentant une matrice.                                                      <!
       !> ------------------------------------------------------------------------------------------------- <! */
    // Classe  M a t r i c e //--------------------------------------------------------------------------------
    template<class T>
    class Matrice {
        private:
            // ------------------------------------------------------------------------------- // Attributs //
            unsigned int _nbrLig, // Nombre de lignes.
                         _nbrCol, // Nombre de colonnes.
                         _nbrElt; // Nombre total d'éléments (lignes x colonnes).
            T * _matrice;
     
        public:
            // ---------------------------------------------------------------------- // Méthodes publiques //
            Matrice();
            Matrice(unsigned int inNbrLig, unsigned int inNbrCol);
            Matrice(const Matrice<T> & inMatrice);
            ~Matrice();
     
            T operator()(unsigned int i, unsigned int j) const;
            T operator()(unsigned int indice) const;
            T & operator()(unsigned int i, unsigned int j);
            T & operator()(unsigned int indice);
            void operator=(const Matrice<T> & inMatrice);
     
            // --------------------------------------------------------------------------------- // Getters //
            unsigned int getNbrLig() const;
            unsigned int getNbrCol() const;
            unsigned int getNbrElt() const;
    };
     
    // Fin //-------------------------------------------------------------------------------------------------
    #endif // MATRICE_H
    Matrice.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    // En-tête //---------------------------------------------------------------------------------------------
    #include "Matrice.h"
    #include <stdexcept>
     
    // ----------------------------------------------------------------------------------- // Constructeurs //
    template<class T>
    Matrice<T>::Matrice() :
        _nbrLig(0), _nbrCol(0), _nbrElt(0)
    {
        _matrice = NULL;
    }
     
    template<class T>
    Matrice<T>::Matrice(unsigned int inNbrLig, unsigned int inNbrCol) :
        _nbrLig(inNbrLig), _nbrCol(inNbrCol), _nbrElt(inNbrLig * inNbrCol)
    {
        try {
            _matrice = new T[_nbrElt];
        }
        catch (std::bad_alloc & ba) {
            std::cerr << "bad_alloc caught : " << ba.what() << "\n";
        }
    }
     
    template<class T>
    Matrice<T>::Matrice(const Matrice<T> & inMatrice) {
        _nbrLig = inMatrice.getNbrLig();
        _nbrCol = inMatrice.getNbrCol();
        _nbrElt = _nbrLig * _nbrCol;
     
        try {
            _matrice = new T[_nbrElt];
            for (unsigned int i = 0; i < _nbrElt; ++i) {
                _matrice[i] = inMatrice(i);
            }
        }
        catch (std::bad_alloc & ba) {
            std::cerr << "bad_alloc caught : " << ba.what() << "\n";
        }
    }
     
    // ------------------------------------------------------------------------------------- // Destructeur //
    template<class T>
    Matrice<T>::~Matrice() {
        delete[] _matrice;
    }
     
    // ---------------------------------------------------------------------------------------- // Méthodes //
    template<class T>
    T Matrice<T>::operator()(unsigned int i, unsigned int j) const {
        try {
            return _matrice[_nbrCol * i + j];
        }
        catch (std::out_of_range & oor) {
            std::cerr << "out of range : " << oor.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    template<class T>
    T & Matrice<T>::operator()(unsigned int i, unsigned int j) {
        try {
            return _matrice[_nbrCol * i + j];
        }
        catch (std::out_of_range & oor) {
            std::cerr << "out of range : " << oor.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    template<class T>
    T Matrice<T>::operator()(unsigned int indice) const {
        try {
            return _matrice[indice];
        }
        catch (std::out_of_range & oor) {
            std::cerr << "out of range : " << oor.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    template<class T>
    T & Matrice<T>::operator()(unsigned int indice) {
        try {
            return _matrice[indice];
        }
        catch (std::out_of_range & oor) {
            std::cerr << "out of range : " << oor.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    template<class T>
    void Matrice<T>::operator=(const Matrice<T> & inMatrice) {
        _nbrLig = inMatrice.getNbrLig();
        _nbrCol = inMatrice.getNbrCol();
        _nbrElt = _nbrLig * _nbrCol;
     
        delete[] _matrice;
        try {
            _matrice = new T[_nbrElt];
            for (unsigned int i = 0; i < _nbrElt; ++i) {
                _matrice[i] = inMatrice(i);
            }
        }
        catch (std::bad_alloc & ba) {
            std::cerr << "bad_alloc caught : " << ba.what() << "\n";
        }
    }
     
    // ----------------------------------------------------------------------------------------- // Getters //
    template<class T> unsigned int Matrice<T>::getNbrLig() const { return _nbrLig; }
    template<class T> unsigned int Matrice<T>::getNbrCol() const { return _nbrCol; }
    template<class T> unsigned int Matrice<T>::getNbrElt() const { return _nbrElt; }
     
    // Fin //-------------------------------------------------------------------------------------------------
    Je bloque pour écrire une fonction efficace capable d'afficher une matrice. J'ai besoin de cette fonction pour vérifier mon code.

    Merci.

  2. #2
    Membre émérite
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    2 764
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 2 764
    Points : 2 705
    Points
    2 705
    Par défaut
    Citation Envoyé par CliffeCSTL Voir le message
    Je dois pouvoir afficher ma matrice grâce à l'opérateur << (peut importe le type d'objet quelle contient).
    Je bloque pour écrire une fonction efficace capable d'afficher une matrice. J'ai besoin de cette fonction pour vérifier mon code.
    As-tu essayé de coder l'opérateur << ?

  3. #3
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    538
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 538
    Points : 262
    Points
    262
    Par défaut
    Oui. Je ne vois pas comment aligner les colonnes lors de l'affichage, pour que sa soit propre.

    Voila la suite du code : addition + multiplication + affichage

    Matrice.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    // Gardien //---------------------------------------------------------------------------------------------
    #ifndef MATRICE_H
    #define MATRICE_H
     
    // En-têtes //--------------------------------------------------------------------------------------------
    #include <string>       // std::string std::erase
    #include <vector>       // std::vector
    #include <sstream>      // std::ostream
     
    /* !> ------------------------------------------------------------------------------------------------- <!
       !>  Classe : Matrice                                                                                 <!
       !>                                                                                                   <!
       !>  Principe : Classe représentant une matrice.                                                      <!
       !> ------------------------------------------------------------------------------------------------- <! */
    // Classe  M a t r i c e //--------------------------------------------------------------------------------
    template<class T>
    class Matrice {
        private:
            // ------------------------------------------------------------------------------- // Attributs //
            unsigned int _nbrLig, // Nombre de lignes.
                         _nbrCol, // Nombre de colonnes.
                         _nbrElt; // Nombre total d'éléments (lignes x colonnes).
            T * _matrice;
     
        public:
            // ---------------------------------------------------------------------- // Méthodes publiques //
            Matrice();
            Matrice(unsigned int inNbrLig, unsigned int inNbrCol);
            Matrice(const Matrice<T> & inMatrice);
            Matrice(std::string matrice_string);
            ~Matrice();
     
            T operator()(unsigned int i, unsigned int j) const;
            T operator()(unsigned int indice) const;
            T & operator()(unsigned int i, unsigned int j);
            T & operator()(unsigned int indice);
            void operator=(const Matrice<T> & inMatrice);
     
            // --------------------------------------------------------------------------------- // Getters //
            unsigned int getNbrLig() const;
            unsigned int getNbrCol() const;
            unsigned int getNbrElt() const;
    };
     
    // ------------------------------------------------------------------------------------------ Déclarations
    template<class T> T from_string(const std::string & str);
    template<class T> std::ostream & operator<<(std::ostream & os, Matrice<T> & M);
    template<class T> Matrice<T> operator+(const Matrice<T> & M1, const Matrice<T> & M2);
    template<class T> Matrice<T> operator*(const Matrice<T> & M1, const Matrice<T> & M2);
    template<class T, class U> Matrice<T> operator*(const U & lambda, const Matrice<T> & M1);
    std::vector<std::string> func_split_string(const std::string & s, char delim);
     
    // En-têtes //--------------------------------------------------------------------------------------------
    #include "Matrice.ipp"
     
    // Fin //-------------------------------------------------------------------------------------------------
    #endif // MATRICE_H
    Matrice.ipp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    // En-têtes //--------------------------------------------------------------------------------------------
    #include <algorithm>
     
    // ----------------------------------------------------------------------------------- // Constructeurs //
    /* !> ------------------------------------------------------------------------------------------------- <!
       !>  Méthode : Matrice                                                                                <!
       !>  Description : Constructeur par défaut.                                                           <!
       !> ------------------------------------------------------------------------------------------------- <! */
    template<class T>
    Matrice<T>::Matrice() :
        _nbrLig(0), _nbrCol(0), _nbrElt(0)
    {
        _matrice = NULL;
    }
     
    /* !> ------------------------------------------------------------------------------------------------- <!
       !>  Méthode : Matrice                                                                                <!
       !>  Paramètres d'entrées :                                                                           <!
       !>      - inNbrLig : nombre de ligne de la matrice.                                                  <!
       !>      - inNbrCol : nombre de colonne de la matrice.                                                <!
       !>  Description : Initialise in matrice de taille [inNbrLig x inNbrCol].                             <!
       !> ------------------------------------------------------------------------------------------------- <! */
    template<class T>
    Matrice<T>::Matrice(unsigned int inNbrLig, unsigned int inNbrCol) :
        _nbrLig(inNbrLig), _nbrCol(inNbrCol), _nbrElt(inNbrLig * inNbrCol)
    {
        try {
            _matrice = new T[_nbrElt];
        }
        catch (std::bad_alloc & ba) {
            std::cerr << "bad_alloc caught : " << ba.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    /* !> ------------------------------------------------------------------------------------------------- <!
       !>  Méthode : Matrice                                                                                <!
       !>  Paramètres d'entrées :                                                                           <!
       !>      - inMatrice : Matrice à recopier.                                                            <!
       !>  Description : Constructeur par recopie.                                                          <!
       !> ------------------------------------------------------------------------------------------------- <! */
    template<class T>
    Matrice<T>::Matrice(const Matrice<T> & inMatrice) {
        _nbrLig = inMatrice.getNbrLig();
        _nbrCol = inMatrice.getNbrCol();
        _nbrElt = inMatrice.getNbrElt();
     
        try {
            _matrice = new T[_nbrElt];
            for (unsigned int i = 0; i < _nbrElt; ++i) {
                _matrice[i] = inMatrice(i);
            }
        }
        catch (std::bad_alloc & ba) {
            std::cerr << "bad_alloc caught : " << ba.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    /* !> ------------------------------------------------------------------------------------------------- <!
       !>  Méthode : Matrice                                                                                <!
       !>  Paramètres d'entrées :                                                                           <!
       !>      - m_string : Chaîne de caractères représentant la matrice à construire.                      <!
       !>  Description : Construit une matrice à partir d'une chaîne de caractères.                         <!
       !> ------------------------------------------------------------------------------------------------- <! */
    template<class T>
    Matrice<T>::Matrice(std::string m_string) {
        m_string.erase(std::remove(m_string.begin(), m_string.end(), ' ' ), m_string.end());
        m_string.erase(std::remove(m_string.begin(), m_string.end(), '    '), m_string.end());
        m_string.erase(std::remove(m_string.begin(), m_string.end(), '[' ), m_string.end());
        m_string.erase(m_string.end() - 1);
        std::vector<std::string> list_ligne = func_split_string(m_string, ']');
     
        _nbrLig = list_ligne.size();
        _nbrCol = func_split_string(list_ligne[0], ',').size();
        _nbrElt = _nbrLig * _nbrCol;
     
        try {
            _matrice = new T[_nbrElt];
            for (unsigned int i = 0; i < _nbrLig; ++i) {
                std::vector<std::string> list_string = func_split_string(list_ligne[i], ',');
                for (unsigned int j = 0; j < _nbrCol; ++j) {
                    (*this)(i, j) = from_string<T>(list_string[j]);
                }
            }
        }
        catch (std::bad_alloc & ba) {
            std::cerr << "bad_alloc caught : " << ba.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    // ------------------------------------------------------------------------------------- // Destructeur //
    template<class T>
    Matrice<T>::~Matrice() {
        delete[] _matrice;
    }
     
    // ---------------------------------------------------------------------------------------- // Méthodes //
    template<class T>
    T Matrice<T>::operator()(unsigned int i, unsigned int j) const {
        try {
            return _matrice[_nbrCol * i + j];
        }
        catch (std::out_of_range & oor) {
            std::cerr << "out of range : " << oor.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    template<class T>
    T & Matrice<T>::operator()(unsigned int i, unsigned int j) {
        try {
            return _matrice[_nbrCol * i + j];
        }
        catch (std::out_of_range & oor) {
            std::cerr << "out of range : " << oor.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    template<class T>
    T Matrice<T>::operator()(unsigned int indice) const {
        try {
            return _matrice[indice];
        }
        catch (std::out_of_range & oor) {
            std::cerr << "out of range : " << oor.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    template<class T>
    T & Matrice<T>::operator()(unsigned int indice) {
        try {
            return _matrice[indice];
        }
        catch (std::out_of_range & oor) {
            std::cerr << "out of range : " << oor.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    template<class T>
    void Matrice<T>::operator=(const Matrice<T> & inMatrice) {
        _nbrLig = inMatrice.getNbrLig();
        _nbrCol = inMatrice.getNbrCol();
        _nbrElt = inMatrice.getNbrElt();
     
        ~Matrice();
        try {
            _matrice = new T[_nbrElt];
            for (unsigned int i = 0; i < _nbrElt; ++i) {
                _matrice[i] = inMatrice(i);
            }
        }
        catch (std::bad_alloc & ba) {
            std::cerr << "bad_alloc caught : " << ba.what() << "\n";
            exit(EXIT_FAILURE);
        }
    }
     
    // ----------------------------------------------------------------------------------------- // Getters //
    template<class T> unsigned int Matrice<T>::getNbrLig() const { return _nbrLig; }
    template<class T> unsigned int Matrice<T>::getNbrCol() const { return _nbrCol; }
    template<class T> unsigned int Matrice<T>::getNbrElt() const { return _nbrElt; }
     
    // --------------------------------------------------------------------------------------- // Fonctions //
    template<class T>
    std::ostream & operator<<(std::ostream & os, Matrice<T> & M) {
        for (unsigned int i = 0; i < M.getNbrLig(); ++i) {
            for (unsigned int j = 0; j < M.getNbrCol(); ++j) {
                os << M(i, j) << " ";
            }
            os << "\n";
        }
        return os;
    }
     
    template<class T>
    T from_string(const std::string & str) {
        std::istringstream iss(str);
        T dest;
        iss >> dest;
        return dest;
    }
     
    // Addition de deux matrices
    template<class T>
    Matrice<T> operator+(const Matrice<T> & M1, const Matrice<T> & M2) {
        if (M1.getNbrLig() == M2.getNbrLig() && M1.getNbrCol() == M2.getNbrCol()) {
            unsigned int n = M1.getNbrLig(), m = M1.getNbrCol();
            Matrice<T> M3(n, m);
            for (unsigned int i = 0; i < n; ++i)
                for (unsigned int j = 0; j < m; ++j)
                    M3(i, j) = M1(i, j) + M2(i, j);
            return M3;
        }
        else {
            throw std::logic_error("Erreur lors de l'addition de deux matrices. Les dimensions doivent êtres\
                les mêmes");
            exit(EXIT_FAILURE);
        }
    }
     
    // Multiplication par un scalaire
    template<class T, class U>
    Matrice<T> operator*(const U & lambda, const Matrice<T> & M1) {
        unsigned int n = M1.getNbrLig(), m = M1.getNbrCol();
        Matrice<T> M3(n, m);
        for (unsigned int i = 0; i < n; ++i)
            for (unsigned int j = 0; j < m; ++j)
                M3(i, j) = lambda * M1(i, j);
        return M3;
    }
     
    // Multiplication de deux matrices
    template<class T>
    Matrice<T> operator*(const Matrice<T> & M1, const Matrice<T> & M2) {
        if (M1.getNbrCol() == M2.getNbrLig()) {
            unsigned int n1 = M1.getNbrLig(), m1 = M1.getNbrCol(),
                         n2 = M2.getNbrLig(), m2 = M2.getNbrCol();
            Matrice<T> M3(n1, m2);
            for (unsigned int i = 0; i < n1; ++i) {
                for (unsigned int j = 0; j < m2; ++j) {
                    T sum = 0;
                    for (unsigned int k = 0; k < m1; ++k) {
                        sum += M1(i, k) * M2(k, j);
                    }
                    M3(i, j) = sum;
                }
            }
            return M3;
        }
        else {
            throw std::logic_error("Erreur lors de la multiplication de deux matrices. Les dimensions\
                ne correspondent pas");
            exit(EXIT_FAILURE);
        }
    }
     
    // Fin //-------------------------------------------------------------------------------------------------
    Matrice.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // Entêtes //---------------------------------------------------------------------------------------------
    #include "Matrice.h"
     
    // --------------------------------------------------------------------------------------- // Fonctions //
    std::vector<std::string> func_split_string(const std::string & s, char delim) {
        std::vector<std::string> elems;
        std::stringstream ss(s);
        std::string item;
        while (std::getline(ss, item, delim)) {
            elems.push_back(item);
        }
        return elems;
    }
     
    // Fin //-------------------------------------------------------------------------------------------------
    main.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    // En-têtes //--------------------------------------------------------------------------------------------
    #include "Matrice.h"
    #include <iostream> // std::cout
     
    // ---------------------------------------------------------------------------------------- // Fonction //
    int main(int argc, char * argv[]) {
     
        Matrice<int> M(2, 2);
        M(0, 0) = 1; M(0, 1) =   2;
        M(1, 0) = 0; M(1, 1) = 128;
     
        Matrice<int> N("[1, 2,  0]\
                        [4, 3, -1]");
        Matrice<int> X("[5, 1]\
                        [2, 3]\
                        [3, 4]");
     
        Matrice<char> C("[a, b, c]\
                         [d, e, f]");
     
        std::cout << M << "\n";
        std::cout << N << "\n";
        std::cout << C << "\n";
        std::cout << 2 * (N * X);
     
        return 0;
    }
     
    // Fin //-------------------------------------------------------------------------------------------------
    Jdemande juste de jeter un oeil et de me dire ce qui peut être améliorer .

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2014
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 142
    Points : 109
    Points
    109
    Par défaut
    Pour l'affichage, un truc assez simple... tu te crées un tableau de string (S) contenant autant de caractères que de lignes et un tableau d'int (L) avec autant de cases.
    Pour chaque colonne :
    Tu lis l element d une ligne (l) donnée, tu le concatènes avec S[l] et tu enregistres le nombre de caractères dans L[l],tu en profites pour calculer le max de L (M).
    Puis tu refais une deuxième passe en rajoutant le nombre d'espaces qui va bien (M-L[l]+1 au moins) pour l alignement.

    A la fin t'affiches tous les elements du tableau S.

  5. #5
    Membre du Club
    Homme Profil pro
    C++
    Inscrit en
    Janvier 2013
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : C++

    Informations forums :
    Inscription : Janvier 2013
    Messages : 45
    Points : 44
    Points
    44
    Par défaut
    Pour la proposition de barygon : Pas besoin de deuxième passe, tu peux utiliser setw et setfill.

Discussions similaires

  1. classe matrice générique
    Par memoire.ph dans le forum Débuter
    Réponses: 4
    Dernier message: 06/05/2012, 19h46
  2. [Débutant] Création générique de matrices à partir de leur nom
    Par riri1483 dans le forum MATLAB
    Réponses: 2
    Dernier message: 10/06/2008, 18h07
  3. template matrice générique
    Par troussepoil dans le forum Langage
    Réponses: 7
    Dernier message: 12/03/2007, 11h45
  4. Gestion de matrice
    Par bzd dans le forum C
    Réponses: 4
    Dernier message: 12/08/2002, 18h19
  5. Comment définir le type matrice ?
    Par charly dans le forum Langage
    Réponses: 7
    Dernier message: 15/06/2002, 21h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo