######################################################################################## # SUJET 1 ######################################################################################## typedef enum {false = 0, true = 1} tbool; ######################################################################################## # SUJET 2 ######################################################################################## #include #include #include #include tbool EstPalindrome(char *phrase) { uint n = strlen(phrase); uint i = 0; while ((i < n/2) && (tolower(phrase[i]) == tolower(phrase[n-(i+1)]))) i++; return (i >= n/2); } int main(int argc, char **argv) { if (argc < 2) { printf("Syntaxe: %s \n", argv[0]); return 1; } // Joindre les arguments de la ligne de commande en une seule chaîne size_t n = 0; for (int i = 1; i < argc; i++) n += strlen(argv[i]); char *phrase = malloc(1 + n * sizeof(phrase[0])); if (phrase == NULL) return 1; phrase[0] = '\0'; for (size_t i = 1; i < argc; i++) strcat(phrase, argv[i]); if (EstPalindrome(phrase)) { printf("\"%s\" est un palindrome.\n", phrase); } else { printf("\"%s\" n'est pas un palindrome.\n", phrase); } free(phrase); return 0; } ######################################################################################## # SUJET 3 ######################################################################################## #include #include typedef unsigned int uint; typedef enum {false=0,true=1} tbool; // Fonction élémentaire pour Syracuse // Avec l'opérateur ternaire ? uint Syracuse(uint n) { return (n % 2) ? (3 * n + 1) : (n >> 1); } // RAS void Vol(uint u, uint *temps, uint *plafond, tbool afficher) { *temps = 0; *plafond = u; while (u != 1) { if (afficher) printf("%u %u\n",*temps,u); u = Syracuse(u); if (u > *plafond) *plafond = u; (*temps)++; } if (afficher) printf("%u %u\n",*temps,u); } // RAS void GenererGraphes(uint n, const char *datat, const char *datap) { FILE *ft = NULL; FILE *fp = NULL; if ((ft = fopen(datat, "w")) == NULL) { fprintf(stderr, "Problème ouverture fichier %s \n", datat); exit(1); } if ((fp = fopen(datap, "w")) == NULL) { fprintf(stderr, "Problème ouverture fichier %s \n", datap); fclose(ft); // Fermer le fichier déjà ouvert exit(1); } for (uint u = 1; u < n; u++) { uint temps, plafond; Vol(u, &temps, &plafond, false); fprintf(ft, "%u %u\n", u, temps); fprintf(fp, "%u %u\n", u, plafond); } fclose(ft); fclose(fp); } // RAS int main(int argc, char *argv[]){ if (argc < 2){ printf("Syntaxe: %s \n", argv[0]); return 0; } char *endptr; uint u = strtoul(argv[1],&endptr,10); if (*endptr != '\0') { printf("Entrée %s invalide\n", argv[1]); return 1; } uint plafond = 0; uint temps = 0; Vol(u,&temps,&plafond,true); printf("Temps et plafond du vol u = %u : %u | %u\n",u, temps, plafond); GenererGraphes(u,"datat.txt","datap.txt"); return 0; }