Bonjour à tous!
Donc voila comme le souligne le sujet, je souhaite creer une classe matrice (pour l instant c'est plutot un vecteur). J'obtiens quelques bugs non desirés.
Je pense que je dois pas coder de la meilleur façon qui soit puisque ayant débuté il y a quelques jours dans la manipulation du C++, j'ai tendance à faire des choses qui ressemble plus à du C qu'autre chose ( et j'ai bien lu les tutoriaux donc pas de panique).
Je souhaite inclure des operateurs banales tells que la soustraction et l'addition entre deux matrices mais cela ne semble pas marcher.
Si je prend par exemple deux vecteurs A (1,1,1) et B (1,1,1) alors l'addition me donne (211152 2 0) et la soustraction (211152 0 0).
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 #include <cstdlib> #include <iostream> #include <new> using namespace std; class Matrice { int *Table; int Dimension; public: // Header of the constructor of the class "macierz" Matrice(int=3); // Header of the destructor of the class "macierz" ~Matrice(); int D() const { return Dimension; } int& D() { return Dimension; } // Header of the constructor of copy of the class "macierz" Matrice::Matrice(const Matrice&); // Header of the Overloading Constructors of the class "macierz" Matrice operator+(const Matrice&) const; Matrice operator-(const Matrice&) const; int& operator[](int dim) { return Table[dim]; } friend ostream& operator<<(ostream& o, Matrice&); };
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
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 #include "Macierz.h" // Definition of the constructor of the class "macierz" Matrice::Matrice(int a) { Table= new int[this->Dimension=a]; } // Definition of the destructor of the class "macierz" Matrice::~Matrice() { delete[] Table; } // Definition of the constructor of copy of the class "macierz" Matrice::Matrice(const Matrice& MatriceCopy) { int i; Dimension =MatriceCopy.Dimension; Table= new int[Dimension]; for(i=0; i<Dimension-1; i++) { Table[i] = MatriceCopy.Table[i]; } } // Definition of the Overloading Constructors "+" of the class "macierz" Matrice Matrice::operator+(const Matrice &M) const { int i; if (M.Dimension == Dimension) { Matrice Y(Dimension); for(i=0; i<M.Dimension; i++){ Y.Table[i]= M.Table[i] + Table[i] ; } return Y; } else cout << " Impossible" << endl ; } Matrice Matrice::operator-(const Matrice &M) const { int i; if (M.Dimension == Dimension) { Matrice Y(this->Dimension); for(i=0; i<M.Dimension; i++){ Y.Table[i]= M.Table[i] - Table[i] ; } return Y; } else cout << " Impossible" << endl ; } ostream& operator<<(ostream& o, Matrice &M) { int i; for(i=0; i<=M.Dimension-1; i++){ o<<"\t"; o<<M.Table[i]<<"\t"; o<<endl; } o<<endl; return o; }
Je vous remercie de votre aide.
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 #include "Macierz.h" using namespace std; int main(int argc, char *argv[]) { Matrice A(3); Matrice C(3); A[0]=A[1]=A[2]= 1; Matrice B(3); B[0]=B[1]=B[2]= 1; cout <<A << endl; cout <<B << endl; C = A+B; cout << C << endl; system("PAUSE"); return EXIT_SUCCESS; }
Nico
Partager