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
|
#include "Matrice.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
Matrice::Matrice() {
tab = NULL;
dim_l = 0;
dim_c = 0;
alloc = false;
}
Matrice::~Matrice() {
if (alloc) {
for (int i = 0; i < dim_l; i++) {
delete [] tab[i];
}
delete [] tab;
}
alloc = false;//inutile
}
void Matrice::alloc_mat(int _dim_l, int _dim_c) {
if(!alloc && _dim_l*_dim_c!=0)
{
dim_l=_dim_l;
dim_c=_dim_c;
tab = new type_mat* [dim_l];
for(int i=0;i<dim_l;i++)
{
tab[i]=new type_mat [dim_c];
}
alloc=true;
}
else
{
dim_l=dim_c=0;
alloc=false;
}
}
bool Matrice::valid_index(int i, int j){
return i >= 0 && i < dim_l && j >= 0 && j < dim_c;
}
Matrice::Matrice(int _dim_l, int _dim_c) {
alloc_mat(_dim_l, _dim_c);
}
Matrice::Matrice(int _dim_l, int _dim_c, type_mat val) {
alloc_mat(_dim_l,_dim_c);
for (int i = 0; i < dim_l; i++) {
for (int j = 0; j < dim_c; j++) {
tab[i][j] = val;
}
}
}
Matrice::Matrice(const Matrice& source)
{
alloc = source.alloc;
dim_l = source.dim_l;
dim_c = source.dim_c;
tab = new type_mat *[dim_l];
for (int i=0; i<dim_l; i++)
{
tab[i] = new type_mat[dim_c];
for(int j=0; j<dim_c; j++)
tab[i][j] = source.tab[i][j];
}
return;
}
void Matrice::print() {
int i, j;
for (i = 0; i < dim_l; i++) {
for (j = 0; j < dim_c; j++) {
cout << " " << tab[i][j];
if (j == dim_c - 1) {
cout << "" << endl;
}
}
}
}
int Matrice::nlignes()// Renvoie le nombre de lignes.
{
return dim_l;
}
int Matrice::ncol()// Renvoie le nombre de colonnes
{
return dim_c;
}
type_mat Matrice::operator()(int i, int j){
if (valid_index(i, j)) {
return tab[i][j];
} else {
return 0;
}
}
type_mat* Matrice::operator[](int i) {
return tab[i];
}
bool Matrice::modify(int i, int j, type_mat val) {
bool rep;
if (valid_index(i, j)) {
tab[i][j] = val;
rep = true;
} else {
rep = false;
cout << " indice(" << i << "," << j
<< ") hors des dimensions de la matrice" << endl;
}
return rep;
}
Matrice& Matrice::operator=(const Matrice& p){
this->dim_l=p.dim_l;
this->dim_c=p.dim_c;
this->alloc=p.alloc;
if(this->alloc)
{
this->tab=new type_mat* [this->dim_l];
for(int i=0;i<(this->dim_l);i++)
{
this->tab[i]=new type_mat [this->dim_c];
for(int j=0;j<(this->dim_c);j++)
{
this->tab[i][j]=p.tab[i][j];
}
}
}
return *this;
} |
Partager