Files
2026_TD2_DEV51_Qualite_Algo/exercices.c
T

92 lines
1.9 KiB
C

/*
* R5.A.04 - Qualité Algorithmique
* TD Complexité cyclomatique - Partie C
*
* Pour chaque fonction, calculez la complexité cyclomatique M.
*/
#include <stdio.h>
/* 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;
}