Newer
Older
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
#include <stdexcept>
#include <iostream>
#include <vector>
using namespace std;
/** Infrastructure minimale de test **/
#define CHECK(test) if (!(test)) cerr << "Test failed in file " << __FILE__ << " line " << __LINE__ << ": " #test << endl
using Matrice = vector<vector<int>>;
Matrice matriceVide (0);
Matrice matriceCarrée = { { 1, 4, 9 },
{ 4, 11, 6 },
{ 9, 12, 7 }};
Matrice matriceSymétrique = { { 1, 2, 3 },
{ 2, 11, 4 },
{ 3, 4, 0 }};
Matrice matriceCreuse = { { 0, 0 },
{ 5, 0 },
{ 0, 2 } };
Matrice matriceRectangulaire={ { 1, 2, 3 },
{ 9, 12, 7 }};
Matrice matriceProduit = { { 10, 6 },
{ 60, 14 }};
/// BEGIN initBizzare
Matrice matriceBizzare = { { 1, 2, 3 , 4},
{ 2, 11, 4 , 5},
{ 3, 4, 100000} };
/// END initBizzare
/// BEGIN estSymétrique
/** Teste si une matrice est symétrique
* @param t une matrice carrée
* @return true si t[i][j] == t[j][i] pour tout i,j, false sinon
**/
bool estSymétrique(Matrice t) {
// Remplacez cette ligne et la suivante par le code adéquat
throw runtime_error("Fonction estSymétrique non implantée ligne 40");
}
/// END estSymétrique
/// BEGIN estSymétriqueTest
void estSymétriqueTest() {
CHECK( estSymétrique(matriceVide) );
CHECK( not estSymétrique(matriceCarrée) );
CHECK( estSymétrique(matriceSymétrique) );
CHECK( not estSymétrique(matriceProduit) );
}
/// END estSymétriqueTest
/// BEGIN estCarréeSymétrique
/** Teste si une matrice est symétrique
* @param t une matrice
* @return true si la matrice est carrée et symétrique
* cad t[i][j] == t[j][i] pour tout i,j, false sinon
**/
bool estCarréeSymétrique(Matrice t) {
// Remplacez cette ligne et la suivante par le code adéquat
throw runtime_error("code non implanté ligne 62");
}
/// END estCarréeSymétrique
/// BEGIN estCarréeSymétriqueTest
void estCarréeSymétriqueTest() {
CHECK( estCarréeSymétrique(matriceVide) );
CHECK( not estCarréeSymétrique(matriceCarrée) );
CHECK( estCarréeSymétrique(matriceSymétrique) );
CHECK( not estCarréeSymétrique(matriceRectangulaire) );
CHECK( not estCarréeSymétrique(matriceBizzare) );
}
/// END estCarréeSymétriqueTest
/// BEGIN somme
/** somme deux matrices dont les dimensions sont identiques
* @param t1 une matrice
* @param t2 une matrice
* @return la matrice t1 + t2
**/
Matrice somme(Matrice t1, Matrice t2) {
// Remplacez cette ligne et la suivante par le code adéquat
throw runtime_error("code non implanté ligne 87");
}
/// END somme
void sommeTest() {
CHECK( somme(matriceCarrée, matriceSymétrique) ==
Matrice({{ 2, 6, 12 }, { 6, 22, 10 }, { 12, 16, 7 }}) );
}
/// BEGIN produit
/** produit de deux matrices dont les dimensions sont compatibles
* @param t1 une matrice
* @param t2 une matrice
* @return la matrice t1 * t2 (produit matriciel)
**/
Matrice produit(Matrice t1, Matrice t2) {
// Remplacez cette ligne et la suivante par le code adéquat
throw runtime_error("code non implanté ligne 105");
}
/// END produit
void produitTest() {
CHECK( produit(matriceCarrée, matriceCarrée) == Matrice({{ 98, 156, 96 }, { 102, 209, 144 }, { 120, 252, 202 }}) );
CHECK( produit(matriceRectangulaire, matriceCreuse) == matriceProduit );
}
int main() {
cout << "Lancement des tests. Si tout marche bien, rien ne devrait s'afficher ci dessous."
<< endl;
// Lance tous les tests
estSymétriqueTest();
estCarréeSymétriqueTest();
sommeTest();
produitTest();
}