commit b3440d7b609bdf8a619f8b2b67d8e5af50e056ad Author: Maxime Menault Date: Tue Oct 13 20:33:39 2026 +0200 Téléverser les fichiers vers "/" diff --git a/Bibliotheque.java b/Bibliotheque.java new file mode 100644 index 0000000..85bd0db --- /dev/null +++ b/Bibliotheque.java @@ -0,0 +1,145 @@ +/* + * R5.A.04 - Qualité Algorithmique + * TD Complexité cyclomatique - Partie Java + * + * Pour chaque méthode, calculez la complexité cyclomatique M. + */ + +import java.util.ArrayList; +import java.util.List; + +public class Bibliotheque { + + static class Livre { + String titre, auteur, genre; + int annee; + boolean emprunte, reserve, abime; + + Livre(String titre, String auteur, String genre, int annee) { + this.titre = titre; + this.auteur = auteur; + this.genre = genre; + this.annee = annee; + } + } + + static class Adherent { + String nom; + boolean etudiant, cotisationAJour; + int nbEmprunts, joursDeRetardCumules; + + Adherent(String nom, boolean etudiant) { + this.nom = nom; + this.etudiant = etudiant; + this.cotisationAJour = true; + } + } + + private final List livres = new ArrayList<>(); + + /* Exercice J1 */ + public int nombreDeLivres() { + return livres.size(); + } + + /* Exercice J2 */ + public boolean estDisponible(Livre livre) { + return !livre.emprunte && !livre.reserve && !livre.abime; + } + + /* Exercice J3 */ + public String peutEmprunter(Adherent a, Livre l) { + if (!a.cotisationAJour) + return "Cotisation à renouveler"; + if (a.joursDeRetardCumules > 30) + return "Trop de retards"; + int maxEmprunts = a.etudiant ? 8 : 5; + if (a.nbEmprunts >= maxEmprunts) + return "Nombre maximum d'emprunts atteint"; + if (l.emprunte) + return "Livre déjà emprunté"; + else if (l.reserve && !a.etudiant) + return "Livre réservé"; + else if (l.abime) + return "Livre en réparation"; + return "OK"; + } + + /* Exercice J4 */ + public double calculerPenalite(int joursRetard, Adherent a, int nbLivres) { + if (joursRetard <= 0 || nbLivres <= 0) + return 0; + double tarif; + if (joursRetard <= 7) + tarif = 0.20; + else if (joursRetard <= 30) + tarif = 0.50; + else + tarif = 1.00; + double penalite = 0; + for (int i = 0; i < nbLivres; i++) { + penalite += joursRetard * tarif; + if (i >= 2) + penalite += 1.0; + } + if (a.etudiant) + penalite *= 0.5; + return penalite > 50 ? 50 : penalite; + } + + /* Exercice J5 */ + public List rechercher(String motCle, String genre, int anneeMin, boolean disponiblesSeulement) { + List resultats = new ArrayList<>(); + for (Livre l : livres) { + if (genre != null && !l.genre.equals(genre)) + continue; + if (l.annee < anneeMin) + continue; + if (disponiblesSeulement && !estDisponible(l)) + continue; + if (motCle == null || motCle.isEmpty()) { + resultats.add(l); + } else { + String m = motCle.toLowerCase(); + if (l.titre.toLowerCase().contains(m) || l.auteur.toLowerCase().contains(m)) + resultats.add(l); + } + } + return resultats; + } + + /* Exercice J6 */ + public int importer(List 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]); + } + } + return importes; + } + + public static void main(String[] args) { + Bibliotheque b = new Bibliotheque(); + b.importer(List.of("# titre;auteur;genre;annee", + "Le Petit Prince;Saint-Exupéry;conte;1943", + "Dune;Herbert;SF;1965", + "Fondation;Asimov;SF;abc")); + Adherent a = new Adherent("Maxime", true); + System.out.println(b.nombreDeLivres() + " " + b.rechercher("dune", "SF", 1900, true).size() + + " " + b.peutEmprunter(a, b.livres.get(0)) + " " + b.calculerPenalite(10, a, 3)); + } +} diff --git a/R5A04_Cyclomatique_MM.pptx b/R5A04_Cyclomatique_MM.pptx new file mode 100644 index 0000000..59e6718 Binary files /dev/null and b/R5A04_Cyclomatique_MM.pptx differ diff --git a/exercices.c b/exercices.c new file mode 100644 index 0000000..a604f21 --- /dev/null +++ b/exercices.c @@ -0,0 +1,91 @@ +/* + * R5.A.04 - Qualité Algorithmique + * TD Complexité cyclomatique - Partie C + * + * Pour chaque fonction, calculez la complexité cyclomatique M. + */ + +#include + +/* Exercice C1 */ +int carre(int x) +{ + return x * x; +} + +/* Exercice C2 */ +int moyenne(const int *t, int n) +{ + int somme = 0; + for (int i = 0; i < n; i++) + somme += t[i]; + return n > 0 ? somme / n : 0; +} + +/* Exercice C3 */ +int est_bissextile(int annee) +{ + return (annee % 4 == 0 && annee % 100 != 0) || annee % 400 == 0; +} + +/* Exercice C4 */ +const char *nom_jour(int jour) +{ + switch (jour) { + case 1: return "lundi"; + case 2: return "mardi"; + case 3: return "mercredi"; + case 4: return "jeudi"; + case 5: return "vendredi"; + case 6: return "samedi"; + case 7: return "dimanche"; + default: return "inconnu"; + } +} + +/* Exercice C5 */ +int compter_mots(const char *texte) +{ + int mots = 0; + int dans_mot = 0; + int i = 0; + while (texte[i] != '\0') { + char c = texte[i]; + int separateur = (c == ' ' || c == '\t' || c == '\n' || c == ',' || c == '.'); + if (separateur) { + dans_mot = 0; + } else if (!dans_mot) { + dans_mot = 1; + mots++; + } + i++; + } + return mots; +} + +/* Exercice C6 */ +int date_valide(int jour, int mois, int annee) +{ + int max; + if (annee < 1 || mois < 1 || mois > 12 || jour < 1) + return 0; + if (mois == 2) { + if (est_bissextile(annee)) + max = 29; + else + max = 28; + } else if (mois == 4 || mois == 6 || mois == 9 || mois == 11) { + max = 30; + } else { + max = 31; + } + return jour <= max; +} + +int main(void) +{ + int t[] = {12, 15, 9, 18}; + printf("%d %d %d %s %d %d\n", carre(4), moyenne(t, 4), est_bissextile(2024), + nom_jour(3), date_valide(29, 2, 2023), compter_mots("Bonjour, le monde.")); + return 0; +} diff --git a/exercices.py b/exercices.py new file mode 100644 index 0000000..691fb9b --- /dev/null +++ b/exercices.py @@ -0,0 +1,71 @@ +""" +R5.A.04 - Qualité Algorithmique +TD Complexité cyclomatique - Partie Python + +Pour chaque fonction, calculez la complexité cyclomatique M. +""" + + +# Exercice P1 +def celsius_vers_fahrenheit(celsius): + return celsius * 9 / 5 + 32 + + +# Exercice P2 +def signe(x): + if x > 0: + return 1 + elif x < 0: + return -1 + return 0 + + +# Exercice P3 +def notes_valides(notes): + return [n for n in notes if 0 <= n <= 20] + + +# Exercice P4 +def mention(moyenne): + mentions = {16: "Très bien", 14: "Bien", 12: "Assez bien", 10: "Passable"} + for seuil, libelle in mentions.items(): + if moyenne >= seuil: + return libelle + return "Ajourné" + + +# Exercice P5 +def analyser_mot_de_passe(mdp, login=None): + problemes = [] + if len(mdp) < 12: + problemes.append("trop court") + majuscule = minuscule = chiffre = special = False + for c in mdp: + if c.isupper(): + majuscule = True + elif c.islower(): + minuscule = True + elif c.isdigit(): + chiffre = True + else: + special = True + if not (majuscule and minuscule): + problemes.append("mélanger majuscules et minuscules") + if not chiffre: + problemes.append("ajouter un chiffre") + if not special: + problemes.append("ajouter un caractère spécial") + if login and login.lower() in mdp.lower(): + problemes.append("ne pas contenir le login") + for i in range(len(mdp) - 2): + if mdp[i] == mdp[i + 1] == mdp[i + 2]: + problemes.append("éviter 3 caractères identiques") + break + if not problemes: + return "fort", [] + return ("moyen" if len(problemes) <= 2 else "faible"), problemes + + +if __name__ == "__main__": + print(celsius_vers_fahrenheit(20), signe(-3), notes_valides([12, 25, -1, 8]), + mention(13.5), analyser_mot_de_passe("Motdepasse123!", "maxime"))