bonjour, voila qui peut m'aider !
marice.h
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 #include <iostream> using namespace std; class Matrice { private: typedef double *ligne; ligne *lignes; unsigned int n; // Nombre de lignes (1er paramètre) unsigned int m; // Nombre de colonnes (2ème paramètre) void echange(double &a, double &b) const; Matrice supligne(const Matrice &mat, unsigned int lig, unsigned int col) const; double det(const Matrice &mat) const; float expo(unsigned int n) const; public: //les constructeurs/destructeur Matrice(); Matrice(unsigned int nl, unsigned int nc); Matrice(unsigned int nl, unsigned int nc, double valeur); Matrice(const Matrice &source); ~Matrice(void); //les operateurs Matrice &operator=(const Matrice &mat); double &operator()(unsigned int i, unsigned int j); Matrice operator+(const Matrice &mat) const; Matrice operator-(const Matrice &mat) const; Matrice operator*(const Matrice &mat) const; Matrice operator*(const double nb) const; friend Matrice operator*(const double nb,const Matrice &mat); Matrice & operator*=(const Matrice &mat); Matrice & operator*=(const double nb); Matrice & operator+=(const Matrice &mat); Matrice & operator-=(const Matrice &mat); Matrice & operator/=(const double); Matrice operator/(const double) const; //accesseur et manipulations Matrice t(); double trace() const; double det() const; Matrice co() const; Matrice inverse() const; int dim() const; unsigned int nb_colones() const; unsigned int nb_lignes() const; friend ostream & operator<<(ostream &os,const Matrice &mat); };
les erreurs !
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 #include "matrice.h" void test_constructeurs() { Matrice m1(3,2,4); Matrice m2(3,8,3); Matrice m3(2,2,9); Matrice m4(m1); Matrice m5; Matrice m6(2,2); cout << "Test des constructeurs :" << endl; cout << "========================" << endl << endl; cout << "Matrice m1(3,2,4)" << endl; cout << m1 << endl; cout << "Matrice m2(3,8,3)" << endl; cout << m2 << endl; cout << "Matrice m3(2,2,9)" << endl; cout << m3<< endl; cout << "Matrice m4(m1)" << endl; cout << m4 << endl; cout << "Matrice m5" << endl; cout << m5 << endl; cout << "Matrice m6(2,2)" << endl; cout << m6 << endl; } void test_operateurs() { Matrice m1(3,2,4); Matrice m2(3,8,3); Matrice m3(2,2,9); Matrice m4; cout << "Test des operateurs :" << endl; cout << "=====================" << endl << endl; cout << "m1+m2" << endl << (m1+m2) << endl; cout << "m1-m2" << endl << (m1-m2) << endl; { cout << "m1*m3" << endl << (m1*m3) << endl; } cout << "m1*2" << endl << (m1*2) << endl; cout << "2*m1" << endl << (2*m1) << endl; cout << "m4=m3" << endl << (m1=m3) << endl; { cout << "m1*=m3" << endl << (m1*=m3) << endl; } cout << "m1*=2" << endl << (m1*=2) << endl; cout << "m1/=2" << endl << (m1/=2) << endl; cout << "m1+=m2" << endl << (m1+=m2) << endl; cout << "m1-=m2" << endl << (m1-=m2) << endl; } void test_manip() { Matrice m1(3,2,4); Matrice m2(3,8,3); Matrice m3(2,2,9); cout << "Test des Manipulations :" << endl; cout << "========================" << endl << endl; cout << "m1 : " << endl << m1 << endl << "transposee de m1" << endl << m1.t() << endl; { cout << "trace de m3 : " << endl << m3.trace() << endl; } { cout << "det(m3) : " << endl << m3.det() << endl; } m3(0,1)=2; m3(1,1)=-3; { cout << "co(m3) : " << endl << m3.co() << endl; } { cout << "inverse(m3) : " << endl << m3.inverse() << endl; } cout << "m3 possede " << m3.nb_lignes() << " lignes et " << m3.nb_colones() << " colones" << endl; } int main() { test_constructeurs(); test_operateurs(); test_manip(); Matrice m4(4,4,0); m4(0,0)=0;m4(0,1)=1;m4(0,2)=2;m4(0,3)=3; m4(1,0)=4;m4(1,1)=5;m4(1,2)=5;m4(1,3)=6; m4(2,0)=7;m4(2,1)=8;m4(2,2)=9;m4(2,3)=0; m4(3,0)=4;m4(3,1)=3;m4(3,2)=2;m4(3,3)=1; //on peut combiner les opérateurs : cout << endl << (((m4=(m4*=m4))*m4)/=2)-m4 << endl; cout << ((((m4=(m4*=m4))*m4)/=2)-m4).inverse() << endl; cout << "det(t(A)) == det(A)" << endl; cout << endl << m4.t().det() << endl; cout << m4.det() << endl; return 0; }
matrice.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 matrice.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\projet2c\matrice.cpp(320) : error C2248: 'n' : cannot access private member declared in class 'Matrice' c:\program files\microsoft visual studio\myprojects\projet2c\matrice.h(11) : see declaration of 'n' C:\Program Files\Microsoft Visual Studio\MyProjects\projet2c\matrice.cpp(322) : error C2248: 'm' : cannot access private member declared in class 'Matrice' c:\program files\microsoft visual studio\myprojects\projet2c\matrice.h(12) : see declaration of 'm' C:\Program Files\Microsoft Visual Studio\MyProjects\projet2c\matrice.cpp(323) : error C2248: 'lignes' : cannot access private member declared in class 'Matrice' c:\program files\microsoft visual studio\myprojects\projet2c\matrice.h(10) : see declaration of 'lignes' Error executing cl.exe.
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381 // +--------------------------------------------------------------------------+ // | Fichier : matrix.cpp | // | Utilite : définition de la Matrice. | // | Auteurs : Nicolas AUNAI | // | Creation : 16.01.2003 | // | Remarques : aucune. | // +--------------------------------------------------------------------------+ #include "matrice.h" void Matrice::echange(double &a,double &b) const { a+=b; b=a-b; a-=b; } Matrice::Matrice() { n = 0; m = 0; lignes = NULL; } Matrice::Matrice(unsigned int nl, unsigned int nc) { n = nl; m = nc; lignes = new ligne[n]; // allocations for (unsigned int i=0; i<n; i++) { lignes[i] = new double[m]; for(unsigned int j=0; j<m; j ++) lignes[i][j] = 0; //initialisation à '0' } } Matrice::Matrice(const Matrice &source) { n = source.n; m = source.m; lignes = new ligne[n]; for(unsigned int i=0; i<n; i ++) { lignes[i] = new double[m]; for(unsigned int j=0; j<m; j ++) lignes[i][j] = source.lignes[i][j]; //recopie } } Matrice::Matrice(unsigned int nl, unsigned int nc, double valeur) { n = nl; m = nc; lignes = new ligne[n]; for(unsigned int i=0; i<n; i ++) { lignes[i] = new double[m]; for(unsigned int j=0; j<m; j ++) lignes[i][j] = valeur; //initialisation à 'valeur' } } Matrice::~Matrice() { for(unsigned int i=0; i<n; i ++) //destruction delete[] lignes[i]; delete[] lignes; } Matrice & Matrice::operator=(const Matrice &mat) { if(this != &mat) //assignation M = M impossible { if(mat.n != n || mat.m != m) // si tailles pas compatibles { for(unsigned int i=0; i<n; i++) // on détruit... delete[] lignes[i]; delete[] lignes; n = mat.n; m = mat.m, lignes = new ligne[n]; // on réalloue for(i=0; i<n; i++) lignes[i] = new double[m]; } for(unsigned int i=0; i<n; i ++) for(unsigned int j=0; j<m; j ++) lignes[i][j] = mat.lignes[i][j]; // et on copie... } return *this; } double & Matrice::operator()(unsigned int i, unsigned int j) { return lignes[i][j]; } Matrice Matrice::operator+(const Matrice &mat) const { Matrice tmp(n,m); for (unsigned int i=0; i<n; i++) // Double boucle. for (unsigned int j=0; j<m; j++) tmp.lignes[i][j] = lignes[i][j] + mat.lignes[i][j]; //addition return tmp; } Matrice Matrice::operator-(const Matrice &mat) const { Matrice tmp(n,m); for (unsigned int i=0; i<n; i++) // Double boucle. for (unsigned int j=0; j<m; j++) tmp.lignes[i][j] = lignes[i][j] - mat.lignes[i][j]; //soustraction return tmp; } Matrice Matrice::operator*(const double nb) const { Matrice tmp(n,m); for (unsigned int i=0; i<n; i++) // Double boucle. for (unsigned int j=0; j<m; j++) tmp.lignes[i][j] = lignes[i][j] * nb; // produit return tmp; } Matrice Matrice::operator*(const Matrice &mat) const { Matrice tmp(n,mat.m,0); if(m == mat.n) // si nb_colone == nb_ligne de l'autre matrice... { for(unsigned int i=0; i<n; i++) //pour chq ligne... for(unsigned int j=0; j<mat.m; j++) // pour chaque colone { for(unsigned int k=0; k<m; k++) // produit scalaire tmp.lignes[i][j] += lignes[i][k]*mat.lignes[k][j]; } } return tmp; } Matrice operator*(const double nb,const Matrice &mat) { return mat*nb; } Matrice & Matrice::operator*=(const double nb) { Matrice tmp(n,m,0); for(unsigned int i=0; i<n; i++) // Double boucle. for(unsigned int j=0; j<m; j++) tmp.lignes[i][j] = lignes[i][j]*nb; //produit *this = tmp; return *this; } Matrice & Matrice::operator*=(const Matrice &mat) { Matrice tmp(n,mat.m,0); if(m == mat.n) //si multiplication possible.... { for(unsigned int i=0; i<n; i++) for(unsigned int j=0; j<mat.m; j++) { for(unsigned int k=0; k<m; k++) tmp.lignes[i][j] += lignes[i][k]*mat.lignes[k][j]; } *this = tmp; } return *this; } Matrice & Matrice::operator+=(const Matrice &mat) { for (unsigned int i=0; i<n; i++) // Double boucle. for (unsigned int j=0; j<m; j++) lignes[i][j] += mat.lignes[i][j]; // += return *this; } Matrice & Matrice::operator-=(const Matrice &mat) { for (unsigned int i=0; i<n; i++) // Double boucle. for (unsigned int j=0; j<m; j++) lignes[i][j] -= mat.lignes[i][j]; //-= return *this; } Matrice Matrice::operator/(const double nb) const { Matrice tmp = *this; if(nb) { for(unsigned int i=0; i<n; i++) for(unsigned int j=0; j<n; j++) tmp.lignes[i][j]/=nb; } return tmp; } Matrice & Matrice::operator/=(const double nb) { if(nb) { for(unsigned int i=0; i<n; i++) for(unsigned int j=0; j<n; j++) lignes[i][j]/=nb; } return *this; } Matrice Matrice::t() { Matrice tmp(m,n); for(unsigned i=0; i<n; i++) for(unsigned j=0; j<m; j++) tmp.lignes[j][i]=lignes[i][j]; return tmp; } double Matrice::trace() const { double sum=0; if(n == m) // trace => matrice carrée { for(unsigned int i=0; i<n; i++) for(unsigned int j=0; j<m; j++) if(i == j) sum+= lignes[i][j]; } return sum; } Matrice Matrice::supligne(const Matrice &mat, unsigned int lig, unsigned int col) const { unsigned int dim = mat.n; unsigned int ld = 0,cd=0; Matrice dest(mat.n-1,mat.n-1,0); // -1 ligne et -1 colone for (unsigned int L=0; L<dim; L++) { cd = 0; // revient a colone 0 if(L != lig) // sauf ligne a virer { for(unsigned int c=0; c<dim; c++) if(c != col) // sauf colone a virer dest.lignes[ld][cd++]=mat.lignes[L][c]; // recopie ld++; } } return dest; } float Matrice::expo(unsigned int n) const { if(!(n%2)){return (1);} return (-1); } double Matrice::det(const Matrice &mat) const { Matrice m2(mat.n,mat.n,0); double x=0; if(mat.n==1) //final return mat.lignes[0][0]; for(unsigned int i=0; i<mat.n; i++) { m2 = supligne(mat,i,0); //extrait la sous matrice x=x+(expo(i)*mat.lignes[i][0]*det(m2)); } return x; } double Matrice::det()const { return det(*this); } ostream & operator<<(ostream &os,const Matrice &mat) { for(unsigned int i=0; i<mat.n; i++) { for(unsigned int j=0; j<mat.m; j++) os <<mat.lignes[i][j] << " "; os << endl; } return os; } Matrice Matrice::co() const { Matrice m2(n,m,0); Matrice ret(n,m,0); if (n==1) { ret.lignes[0][0]=1; } else { for (unsigned int i=0;i<n;i++) for (unsigned int j=0;j<n;j++) { m2 = supligne(*this,i,j);// sous_matrice ret.lignes[i][j]=expo(i+j)*m2.det(); } } return ret; } Matrice Matrice::inverse() const { Matrice inv(n,m,0); inv = (this->co().t())/(this->det()); return inv; } int Matrice::dim() const { return (n == m)?static_cast<int>(n):-1; } unsigned int Matrice::nb_colones() const { return m; } unsigned int Matrice::nb_lignes() const { return n; }
Partager