TD2 terminé

This commit is contained in:
rabot
2026-10-14 12:43:59 +02:00
parent cecf7379f6
commit 826cde779c
3 changed files with 105 additions and 17 deletions
+29 -17
View File
@@ -108,30 +108,42 @@ public class Bibliotheque {
return resultats;
}
/* Exercice J6 */
/* Exercice J6 - M = 3 : 1 + for + if (avant refactorisation : M = 8). */
public int importer(List<String> lignes) {
int importes = 0;
for (String ligne : lignes) {
if (ligne.isBlank() || ligne.startsWith("#"))
continue;
String[] champs = ligne.split(";");
if (champs.length != 4) {
System.err.println("Ligne ignorée : " + ligne);
continue;
}
try {
int annee = Integer.parseInt(champs[3].trim());
if (annee > 0 && annee <= 2026) {
livres.add(new Livre(champs[0], champs[1], champs[2], annee));
importes++;
}
} catch (NumberFormatException e) {
System.err.println("Année invalide : " + champs[3]);
}
if (importerLigne(ligne))
importes++;
}
return importes;
}
/* M = 4 : 1 + 2 if + ||. */
private boolean importerLigne(String ligne) {
if (ligne.isBlank() || ligne.startsWith("#"))
return false;
String[] champs = ligne.split(";");
if (champs.length != 4) {
System.err.println("Ligne ignorée : " + ligne);
return false;
}
return importerLivre(champs);
}
/* M = 4 : 1 + if + || + catch. */
private boolean importerLivre(String[] champs) {
try {
int annee = Integer.parseInt(champs[3].trim());
if (annee <= 0 || annee > 2026)
return false;
livres.add(new Livre(champs[0], champs[1], champs[2], annee));
return true;
} catch (NumberFormatException e) {
System.err.println("Année invalide : " + champs[3]);
return false;
}
}
public static void main(String[] args) {
Bibliotheque b = new Bibliotheque();
b.importer(List.of("# titre;auteur;genre;annee",
+76
View File
@@ -0,0 +1,76 @@
# Compte rendu du TD de complexité cyclomatique
Le but du TD est de calculer la complexité cyclomatique des fonctions en C, Java et Python. On utilise **M = nombre de décisions + 1**. Les fonctions `main` et les constructeurs ne sont pas analysés.
## Calculs
Classement du cours : simple de 1 à 10, modéré de 11 à 20, complexe de 21 à 50 et très risqué au-delà de 50. Dans les tableaux, ET et OU désignent les opérateurs logiques.
### C
| Fonction | Détail du calcul | M | Classement |
| --- | --- | --- | --- |
| C1 `carre` | 1, aucune décision | 1 | Simple |
| C2 `moyenne` | 1 + 1 for + 1 ternaire | 3 | Simple |
| C3 `est_bissextile` | 1 + 1 ET + 1 OU | 3 | Simple |
| C4 `nom_jour` | 1 + 7 case | 8 | Simple |
| C5 `compter_mots` | 1 + 1 while + 2 if + 4 OU | 8 | Simple |
| C6 `date_valide` | 1 + 4 if + 6 OU | 11 | Modéré |
### Java
| Méthode | Détail du calcul | M | Classement |
| --- | --- | --- | --- |
| J1 `nombreDeLivres` | 1, aucune décision | 1 | Simple |
| J2 `estDisponible` | 1 + 2 ET | 3 | Simple |
| J3 `peutEmprunter` | 1 + 6 if + 1 ET + 1 ternaire | 9 | Simple |
| J4 `calculerPenalite` | 1 + 5 if + 1 for + 1 OU + 1 ternaire | 9 | Simple |
| J5 `rechercher` | 1 + 5 if + 1 for + 2 ET + 2 OU | 11 | Modéré |
| J6 `importer` avant refactorisation | 1 + 3 if + 1 for + 1 ET + 1 OU + 1 catch | 8 | Simple |
| J6 `importer` après refactorisation | 1 + 1 for + 1 if | 3 | Simple |
| `importerLigne` | 1 + 2 if + 1 OU | 4 | Simple |
| `importerLivre` | 1 + 1 if + 1 OU + 1 catch | 4 | Simple |
### Python
| Fonction | Détail du calcul | M | Classement |
| --- | --- | --- | --- |
| P1 `celsius_vers_fahrenheit` | 1, aucune décision | 1 | Simple |
| P2 `signe` | 1 + 1 if + 1 elif | 3 | Simple |
| P3 `notes_valides` | 1 + 1 for + 1 if | 3 | Simple |
| P4 `mention` | 1 + 1 for + 1 if | 3 | Simple |
| P5 `analyser_mot_de_passe` | 1 + 10 if/elif + 2 for + 2 ET + 1 ternaire | 16 | Modéré |
## Graphes
![Graphes des fonctions C2, C4, J2, J6, P2 et P3](TP%20DEV5.1_V3.drawio.png)
Avec **M = E − N + 2P**, E est le nombre d'arêtes, N le nombre de nœuds et P vaut 1 pour chaque fonction.
| Fonction | E | N | Calcul de M |
| --- | --- | --- | --- |
| C2 | 8 | 7 | 8 − 7 + 2 = 3 |
| C4 | 16 | 10 | 16 − 10 + 2 = 8 |
| J2, avec la sortie commune | 6 | 5 | 6 − 5 + 2 = 3 |
| J6, avant refactorisation | 22 | 16 | 22 − 16 + 2 = 8 |
| P2, avec deux tests et trois retours | 7 | 6 | 7 − 6 + 2 = 3 |
| P3 | 5 | 4 | 5 − 4 + 2 = 3 |
## Cas particuliers
- Les opérateurs `&&`, `||`, `and` et `or` ajoutent chacun une décision, même sans `if`, car ils peuvent éviter l'évaluation de la suite.
- Un `switch` ajoute une décision par `case`. Le `default` n'ajoute rien.
- La compréhension de P3 contient une boucle et un filtre. Les comparaisons chaînées Python sont comptées ici comme un seul test.
- Un `catch` et un ternaire ajoutent chacun une décision.
- `else`, `return`, `continue` et `break` n'ajoutent pas de décision.
## Refactorisation de importer
La méthode a été séparée en trois parties : `importer` parcourt les lignes, `importerLigne` vérifie leur format et `importerLivre` vérifie l'année puis ajoute le livre.
La méthode `importer` passe de **M = 8 à M = 3**. Les deux méthodes auxiliaires ont chacune **M = 4**. Les décisions sont réparties entre des méthodes plus courtes et plus faciles à lire.
Les deux versions compilent. Les imports ont donné les mêmes résultats et messages d'erreur sur les cas testés : lignes vides, commentaires, formats incorrects, années invalides et livres valides.
* Adrien Rabot
* Théo Anastasio
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB