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
|
// matrice.h
#include <iostream>
#include <iomanip>
using namespace std;
class matrice{
friend ostream& operator<<( ostream& flux, matrice &m);
friend matrice operator+(const matrice &m1, const matrice &m2);
public :
matrice() { _li=0; _col=0; _val = 0;};
matrice(int li, int col);
matrice(const matrice &m); // cons de copie
~matrice(){_li=0; _col=0; delete[] _val; };
float& operator()(int li, int col); // setter
float operator()(int li, int col) const; // getter
matrice& operator=(const matrice &m);
int lignes(){ return _li;};
int colonnes(){ return _col;};
private :
int _li, _col;
float *_val;
};
#endif
// matrice.cpp
/*
* matrice.cpp
* temp
*
* Created by Pierre Tritsch on 20/05/13.
* Copyright 2013 -. All rights reserved.
*
*/
#include "matrice.h"
matrice::matrice(int li , int col){
_li = li;
_col= col;
_val = new float[_li * _col];
}
matrice::matrice(const matrice &m){
_li = m._li;
_col = m._col;
int n = _li*_col;
_val = new float[n];
for(int i=0; i<n; i++)
_val[i] = m._val[i];
}
ostream& operator<<( ostream& flux, matrice &m){
for(int i=0; i< m._li; i++)
for(int j = 0; j< m._col; j++)
{
if(j==0) flux << endl;
flux << setw(10) << m(i,j);
}
return flux;
}
float& matrice::operator()(int li, int col){
return(_val[li*_col + col]);
}
float matrice::operator()(int li, int col) const {
return(_val[li*_col + col]);
}
matrice operator+(const matrice &m1, const matrice &m2){
matrice temp(m1);
int n = temp._li * temp._col;
for(int i = 0; i<n; i++)
temp._val[i] = temp._val[i] + m1._val[i];
return temp;
}
matrice &matrice::operator=(const matrice &m){
if(this !=&m)
{
delete [] _val;
_li = m._li;
_col = m._col;
int n = _li*_col;
_val = new float[n];
for(int i = 0; i<n; i++)
_val[i] = m._val[i];
}
return *this;
}
// main
#include <iostream>
#include "matrice.h"
using namespace std;
int main()
{
matrice m(3,3);
matrice n;
matrice p(3,3);
for(int i = 0; i<3; i++)
for(int j=0; j<3; j++)
m(i,j) = i*3 + j;
cout << "m : " << endl;
cout << m << endl;
n = m;
p = m + n;
cout << n << endl;
cout << p << endl;
return 0;
} |
Partager