Vous êtes sur la page 1sur 7

#include "stdafx.h" #include "Matrice.h" // important et au dbut de la classe #include "Vecteur.h" #include <malloc.

h> //#include <iostream> using std::cout; using std::cin; using std::endl; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Vecteur::Vecteur() { dim = 3; tab = new double[3]; for(int i = 0; i < 3; i++) tab[i] = 0; } Vecteur::Vecteur(int dim) { this->dim = dim; tab = new double[dim]; for(int i = 0; i < dim; i++) tab[i] = 0; } Vecteur::Vecteur(double* tableau) { dim = _msize(tableau)/sizeof(double); tab = new double[dim]; for(int i = 0; i < dim; i++) tab[i] = tableau[i]; } Vecteur::~Vecteur() { if(tab) delete[] tab; } // constructeur par recopie Vecteur::Vecteur(const Vecteur& vet) { dim = vet.dim; tab = new double[dim]; for(int i=0; i<dim; i++) tab[i] = vet.tab[i]; } // Mthode permettant l'affichage des lments du tableau tab void Vecteur::affiche()

{ for(int i = 0; i < dim; i++) cout<<tab[i]<<'\t'; cout<<endl; } double* Vecteur::get_tab() { return tab; } void Vecteur::setTab(double* vetTab) { dim = _msize(vetTab)/sizeof(double); if(tab) delete[] tab; tab = new double[dim]; for(int i = 0; i < dim; i++) tab[i] = vetTab[i]; } void Vecteur::saisiVect() { if(tab) delete[] tab; cout<<"entrer la taille du nouveau vecteur:"<<endl; cin >> dim; tab = new double[dim]; for(int i = 0; i < dim; i++) { cout<<"entrer tab["<<i<<"] = "; cin >>tab[i]; cout<<endl; } } Vecteur prod(const Vecteur& vet, const Matrice& m) { int ligne = m.getNbLigne(); int colonne = m.getNbCoLonne(); if(colonne != vet.dim) return Vecteur(1); double** temp = m.getTabMat(); Vecteur result(ligne); for(int i = 0; i < ligne; i++){ for(int j = 0; j < colonne; j++){ result.tab[i] += temp[i][j] * vet.tab[j]; } } return result; } void prod2(const Vecteur& vect, const Matrice& mat) {

cout<<"le nom de la matrice est: " <<mat.nom<<" et le premier lment du vecteur est: "; cout<<vect.tab[0]<<endl; }

//Classe Matrice ice.cpp: implementation of the Matrice class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Matrice.h" #include "Vecteur.h" #include "AutreVecteur.h" #include <cmath> #include <malloc.h> using std::cout; using std::cin; using std::endl; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Matrice::Matrice() { nom = "Matrice par defaut"; nbLigne = 10; nbColonne = 10; tabMat = new double*[nbLigne]; // il faut initailiser les pointeurs de tab for(int i = 0; i < nbLigne; i++){ tabMat[i] = new double[nbColonne]; for(int j = 0; j < nbColonne; j++){ tabMat[i][j] = 0; } } } Matrice::Matrice(int n, int m, string nom) { this->nom = nom; nbLigne = n; nbColonne = m; tabMat = new double*[nbLigne]; for(int i = 0; i < nbLigne; i++){ tabMat[i] = new double[nbColonne]; for(int k = 0; k < nbColonne; k++) tabMat[i][k] = 0; } }

Matrice::Matrice(double**tab, string nomMat) { nom = nomMat; cout<<"recuperer nbLigne et nbColonne"<<endl; nbColonne = _msize(tab[0])/sizeof(double); nbLigne = _msize(tab)/_msize(tab[0]); for(int i = 0; i < nbLigne; i++){ for(int j = 0; j < nbColonne; j++){ tabMat[i][j] = tab[i][j]; } } } Matrice::~Matrice() { if(tabMat){ for(int i = 0; i < nbLigne; i++) if(tabMat[i]) delete[] tabMat[i]; delete[] tabMat; } } Matrice::Matrice(const Matrice& mat) { nom = mat.nom; nbLigne = mat.nbLigne; nbColonne = mat.nbColonne; tabMat = new double*[nbLigne]; for(int i = 0; i < nbLigne; i++){ tabMat[i] = new double[nbColonne]; for(int j = 0; j < nbColonne; j++){ tabMat[i][j] = mat.tabMat[i][j]; } } } void Matrice::affiche() const { for(int i = 0; i < nbLigne; i++){ for(int j = 0; j < nbColonne; j++){ cout<<tabMat[i][j]<<'\t'; } cout<<endl; } cout<<"avec le nom de la matrice est: "<<nom<<endl;; } string Matrice::getNom() const { return nom; }

void Matrice::setNom(const string& str) { nom = str; } int Matrice::getNbLigne()const { return nbLigne; } int Matrice::getNbCoLonne()const { return nbColonne; } double** Matrice::getTabMat()const { return tabMat; } void Matrice::saisiMatrice() { cout << "entrer le nom de la nouvelle matrice"<<endl; cin >> nom; if(tabMat){ for(int i = 0; i < nbLigne; i++) if(tabMat[i]) delete[] tabMat[i]; delete[] tabMat; } cout<<"entrer le nombre de ligne de la nouvelle matrice:"<<endl; cin >> nbLigne; cout<<"entrer le nombre de colonne de la nouvelle matrice:"<<endl; cin >> nbColonne; tabMat = new double*[nbLigne]; //for(int k = 0; k < nbLigne; k++) tabMat[k] = new double[nbColonne]; for(int i = 0; i < nbLigne; i++) { tabMat[i] = new double[nbColonne]; for(int j = 0; j < nbColonne; j++) { cout<<"entrer tab["<<i<<", "<<j<<"] = "; cin >>tabMat[i][j]; cout<<endl; } } }

Vecteur Matrice::produit(Vecteur& vect) { Vecteur result(nbLigne); for(int i = 0; i < nbLigne; i++){ int som = 0; for(int j = 0; j < nbColonne; j++){ som += tabMat[i][j] * vect.tab[j]; } result.tab[i] = som; } return result; } void modifNom(Matrice& mat, string modNom) { if( (modNom.compare(mat.nom) != 0)) mat.nom = modNom; }

Vous aimerez peut-être aussi