From f7764aa296897817daf939722be1ff9a4780ef49 Mon Sep 17 00:00:00 2001 From: Simoes Lukas Date: Tue, 10 Dec 2024 12:52:05 +0100 Subject: [PATCH] Ajout TPS + Entrainements --- .../controle_machine_2_A/Makefile | 10 ++ .../controle_machine_2_A/carre.c | 50 +++++++++ .../Entrainements/controle_machine_2_A/ldiv.c | 9 ++ .../controle_machine_2_A/lightness.c | 12 +++ .../controle_machine_2_A/lightness.h | 6 ++ .../controle_machine_2_A/suite.c | 39 +++++++ .../controle_machine_2_B/Makefile | 11 ++ .../controle_machine_2_B/carre.c | 48 +++++++++ .../controle_machine_2_B/decomposition.c | 11 ++ .../Entrainements/controle_machine_2_B/hue.c | 12 +++ .../Entrainements/controle_machine_2_B/hue.h | 6 ++ DEV1.1/TP19/TP19_reponses.txt | 76 ++++++++++++- DEV1.1/TP19/tests.c | 33 +++--- DEV1.1/TP25/TP25_reponses.txt | 80 ++++++++++++++ DEV1.1/TP25/test.c | 100 +++++++++++++++--- DEV1.1/TP27/TP27-reponses.txt | 55 ++++++++++ DEV1.1/TP27/test.c | 18 ++++ DEV1.1/TP28/TP28_reponses.txt | 8 ++ DEV1.1/TP28/test.c | 13 +++ DEV1.1/controle_machine_2_B/decomposition.c | 10 ++ 20 files changed, 577 insertions(+), 30 deletions(-) create mode 100644 DEV1.1/Entrainements/controle_machine_2_A/Makefile create mode 100644 DEV1.1/Entrainements/controle_machine_2_A/carre.c create mode 100644 DEV1.1/Entrainements/controle_machine_2_A/ldiv.c create mode 100644 DEV1.1/Entrainements/controle_machine_2_A/lightness.c create mode 100644 DEV1.1/Entrainements/controle_machine_2_A/lightness.h create mode 100644 DEV1.1/Entrainements/controle_machine_2_A/suite.c create mode 100644 DEV1.1/Entrainements/controle_machine_2_B/Makefile create mode 100644 DEV1.1/Entrainements/controle_machine_2_B/carre.c create mode 100644 DEV1.1/Entrainements/controle_machine_2_B/decomposition.c create mode 100644 DEV1.1/Entrainements/controle_machine_2_B/hue.c create mode 100644 DEV1.1/Entrainements/controle_machine_2_B/hue.h create mode 100644 DEV1.1/TP27/TP27-reponses.txt create mode 100644 DEV1.1/TP27/test.c create mode 100644 DEV1.1/TP28/TP28_reponses.txt create mode 100644 DEV1.1/TP28/test.c create mode 100644 DEV1.1/controle_machine_2_B/decomposition.c diff --git a/DEV1.1/Entrainements/controle_machine_2_A/Makefile b/DEV1.1/Entrainements/controle_machine_2_A/Makefile new file mode 100644 index 0000000..5eeac3e --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_A/Makefile @@ -0,0 +1,10 @@ +complet : carre.o lightness.o + gcc -ansi -pedantic -o carre.o lightness.o +carre.o : carre.c + gcc -ansi -pedantic -c carre.c +lightness.o : lightness.c lightness.h + gcc -ansi -pedantic -c lightness.c +clean : + rm -f carre.o lightness.o +run : complet + ./complet \ No newline at end of file diff --git a/DEV1.1/Entrainements/controle_machine_2_A/carre.c b/DEV1.1/Entrainements/controle_machine_2_A/carre.c new file mode 100644 index 0000000..96a0f23 --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_A/carre.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include"lightness.h" + +#define LIGHT 0 +#define DARK 1 +#define RED 1 +#define GREEN 2 +#define BLUE 4 +#define LIGHT_RED 217 +#define DARK_RED 124 +#define LIGHT_GREEN 157 +#define DARK_GREEN 34 +#define LIGHT_BLUE 147 +#define DARK_BLUE 19 + +int hue(void) { + int choice = rand()%3; + if (choice == 0) { + return RED; + } else if (choice == 1) { + return GREEN; + } else /* if (choice == 2) */ { + return BLUE; + } +} + +int main(void) { + int l, c, v; + + srand(time(NULL)); + l = lightness(); + c = hue(); + + if (c == RED) { + v = (l == LIGHT) ? LIGHT_RED : DARK_RED; + } else if (c == GREEN) { + v = (l == LIGHT) ? LIGHT_GREEN : DARK_GREEN; + } else /* if (c == BLUE) */ { + v = (l == LIGHT) ? LIGHT_BLUE : DARK_BLUE; + } + + printf("┏━━━━┓\n"); + printf("┃\33[48;5;%dm \33[m┃\n", v); + printf("┃\33[48;5;%dm \33[m┃\n", v); + printf("┗━━━━┛\n"); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/DEV1.1/Entrainements/controle_machine_2_A/ldiv.c b/DEV1.1/Entrainements/controle_machine_2_A/ldiv.c new file mode 100644 index 0000000..b962391 --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_A/ldiv.c @@ -0,0 +1,9 @@ +# include +# include + +int main(int argc, char** argv) { + ldiv_t quotient = ldiv(strtol(argv[1], NULL, 10), strtol(argv[2], NULL, 10)); + printf("quotient : %ld\n", quotient.quot); + printf("reste : %ld\n", quotient.rem); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/DEV1.1/Entrainements/controle_machine_2_A/lightness.c b/DEV1.1/Entrainements/controle_machine_2_A/lightness.c new file mode 100644 index 0000000..16b4557 --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_A/lightness.c @@ -0,0 +1,12 @@ +#include +#include +#include +#include "carre.h" + +int lightness(void) { + if (time(NULL)%2) { + return LIGHT; + } else { + return DARK; + } +} \ No newline at end of file diff --git a/DEV1.1/Entrainements/controle_machine_2_A/lightness.h b/DEV1.1/Entrainements/controle_machine_2_A/lightness.h new file mode 100644 index 0000000..378120a --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_A/lightness.h @@ -0,0 +1,6 @@ +#ifndef LIGHTNESS_H +#define LIGHTNESS_H + +int lightness(void); + +#endif \ No newline at end of file diff --git a/DEV1.1/Entrainements/controle_machine_2_A/suite.c b/DEV1.1/Entrainements/controle_machine_2_A/suite.c new file mode 100644 index 0000000..2e45d29 --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_A/suite.c @@ -0,0 +1,39 @@ +# include +# include + + +int* suite(int n) { + int* p = NULL; + int compteur = 0; + p = (int*) malloc(sizeof(int)); + if (p) { + p[0] = n; + while (!n%2) { + compteur++; + p = (int*) realloc(p, sizeof(int)); + n = n/2; + p[compteur] = n; + } + return p; + } + else { + printf("Espace mémoire insuffisant."); + } +} + +void afficher_tableau(int* t) { + int i; + size_t max = *(&t + 1) - t; + printf("%lu", max); + for (i = 0; i != max ; i++) { + printf("| %d", t[i]); + } + printf("\n"); +} + + +int main(void) { + int tab[5] = {2, 5, 8, 9, 1}; + afficher_tableau(tab); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/DEV1.1/Entrainements/controle_machine_2_B/Makefile b/DEV1.1/Entrainements/controle_machine_2_B/Makefile new file mode 100644 index 0000000..26976a9 --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_B/Makefile @@ -0,0 +1,11 @@ +carre: carre.o hue.o + gcc -ansi -pedantic -o carre carre.o hue.o + +carre.o : carre.c + gcc -ansi -pedantic -c carre.c + +hue.o : hue.c hue.h + gcc -ansi -pedantic -c hue.c + +run : carre + ./carre diff --git a/DEV1.1/Entrainements/controle_machine_2_B/carre.c b/DEV1.1/Entrainements/controle_machine_2_B/carre.c new file mode 100644 index 0000000..323bf50 --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_B/carre.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include "hue.h" + +#define LIGHT 0 +#define DARK 1 +#define RED 1 +#define GREEN 2 +#define BLUE 4 +#define LIGHT_RED 217 +#define DARK_RED 124 +#define LIGHT_GREEN 157 +#define DARK_GREEN 34 +#define LIGHT_BLUE 147 +#define DARK_BLUE 19 + +int lightness(void) { + if (time(NULL)%2) { + return LIGHT; + } else { + return DARK; + } +} + + +int main(void) { + int l, c, v; + + srand(time(NULL)); + l = lightness(); + c = hue(); + + if (c == RED) { + v = (l == LIGHT) ? LIGHT_RED : DARK_RED; + } else if (c == GREEN) { + v = (l == LIGHT) ? LIGHT_GREEN : DARK_GREEN; + } else /* if (c == BLUE) */ { + v = (l == LIGHT) ? LIGHT_BLUE : DARK_BLUE; + } + + printf("┏━━━━┓\n"); + printf("┃\33[48;5;%dm \33[m┃\n", v); + printf("┃\33[48;5;%dm \33[m┃\n", v); + printf("┗━━━━┛\n"); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/DEV1.1/Entrainements/controle_machine_2_B/decomposition.c b/DEV1.1/Entrainements/controle_machine_2_B/decomposition.c new file mode 100644 index 0000000..7b22908 --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_B/decomposition.c @@ -0,0 +1,11 @@ +# include +# include +# include + +int main(int argc, char** argv) { + double partie_entiere; + modf(strtod(argv[1], NULL), &partie_entiere); + printf("partie entière : %f\npartie décimale : %f\n", partie_entiere, (strtod(argv[1],NULL) - partie_entiere)); + return EXIT_SUCCESS; +} + diff --git a/DEV1.1/Entrainements/controle_machine_2_B/hue.c b/DEV1.1/Entrainements/controle_machine_2_B/hue.c new file mode 100644 index 0000000..4504ca2 --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_B/hue.c @@ -0,0 +1,12 @@ +#include "carre.c" + +int hue(void) { + int choice = rand()%3; + if (choice == 0) { + return RED; + } else if (choice == 1) { + return GREEN; + } else /* if (choice == 2) */ { + return BLUE; + } +} \ No newline at end of file diff --git a/DEV1.1/Entrainements/controle_machine_2_B/hue.h b/DEV1.1/Entrainements/controle_machine_2_B/hue.h new file mode 100644 index 0000000..192b4aa --- /dev/null +++ b/DEV1.1/Entrainements/controle_machine_2_B/hue.h @@ -0,0 +1,6 @@ +#ifndef HUE +#define HUE + +int hue(void); + +#endif \ No newline at end of file diff --git a/DEV1.1/TP19/TP19_reponses.txt b/DEV1.1/TP19/TP19_reponses.txt index cd2d54a..284d912 100644 --- a/DEV1.1/TP19/TP19_reponses.txt +++ b/DEV1.1/TP19/TP19_reponses.txt @@ -1,4 +1,78 @@ ------- TP19 : Allocation dynamique -------- -1. +1. # include +# include + +int main(void) { + int taille_tab; + double* tab = (double*) malloc(3*sizeof(double)); + int* tab_compteurs = (int*) malloc(sizeof(int)); + int i; + int j; + printf("Combien de réels souhaitez-vous entrer ? "); + scanf("%d", &taille_tab); + getchar(); + tab = (double*) realloc(tab, taille_tab*sizeof(double)); + tab_compteurs = (int*) realloc(tab_compteurs, taille_tab*sizeof(int)); + for (i = 0; i != taille_tab; i++) { + printf("Entrez le %de réel : ", i + 1); + scanf("%lf", &tab[i]); + getchar(); + tab_compteurs[i] = 0; + } + + for (i = 0; i!=taille_tab; i++) { + for (j = 0; j != taille_tab; j++) { + if (tab[j] == tab[i]) { + tab_compteurs[i] += 1; + } + } + } + + for (i = 0; i!=taille_tab; i++) { + if (tab_compteurs[i] == 1) { + printf("| %lf ", tab[i]); + } + } + putchar('\n'); + return EXIT_SUCCESS; +} + +2. + +# include +# include +# include + +char* inverse(const char* s) { + int taille_s = strlen(s); + char* s_inverse = (char*) malloc(sizeof(char)); + int i; + int j = 0; + s_inverse = realloc(s_inverse, taille_s*sizeof(char)); + for (i = taille_s-1; i>0; i--) { + s_inverse[j] = s[i]; + j++; + } + s_inverse[j] = s[0]; + return s_inverse; +} + +int main(int argc, char** argv) { + int i; + for (i = 1; i != argc; i++){ + if (strcmp(argv[i], inverse(argv[i])) == 0) { + printf(argv[i]); + printf(" est un palindrome.\n"); + } + else { + printf(argv[i]); + printf(" n'est pas un palindrome.\n"); + } + } + return EXIT_SUCCESS; +} + + +3. diff --git a/DEV1.1/TP19/tests.c b/DEV1.1/TP19/tests.c index 89a8b4b..13f712c 100644 --- a/DEV1.1/TP19/tests.c +++ b/DEV1.1/TP19/tests.c @@ -2,31 +2,34 @@ # include int main(void) { - int taille_tab; + int taille_tab = 0; + double entree; double* tab = (double*) malloc(3*sizeof(double)); + int* tab_compteurs = (int*) malloc(sizeof(int)); int i; int j; - printf("Combien de réels souhaitez-vous entrer ? "); - scanf("%d", &taille_tab); - getchar(); - tab = (double*) realloc(tab, taille_tab*sizeof(double)); - for (i = 0; i != taille_tab; i++) { - printf("Entrez le %de réel : ", i + 1); - scanf("%lf", &tab[i]); + while( entree != (double) 'q') { + taille_tab++; + tab = (double*) realloc(tab, sizeof(double)); + tab_compteurs = (int*) realloc(tab_compteurs, sizeof(int)); + printf("Entrez le %de réel : ", taille_tab); + scanf("%lf", &entree); getchar(); + tab[taille_tab] = entree; + tab_compteurs[taille_tab] = 0; } - for (i = 0; i != (taille_tab - 1); i++) { - for (j = i; j != taille_tab; j++) { - if (tab[i] == tab[j]) { - tab[j] = 0.0; + for (i = 0; i!=taille_tab; i++) { + for (j = 0; j != taille_tab; j++) { + if (tab[j] == tab[i]) { + tab_compteurs[i] += 1; } } } - for (i = 0; i != taille_tab; i++) { - if (tab[i] != 0.0) { - printf("%.3f ", tab[i]); + for (i = 0; i!=taille_tab; i++) { + if (tab_compteurs[i] == 1) { + printf("| %lf ", tab[i]); } } putchar('\n'); diff --git a/DEV1.1/TP25/TP25_reponses.txt b/DEV1.1/TP25/TP25_reponses.txt index e69de29..0c273a9 100644 --- a/DEV1.1/TP25/TP25_reponses.txt +++ b/DEV1.1/TP25/TP25_reponses.txt @@ -0,0 +1,80 @@ +----- TP25 : Listes chaînées ----- + +1. + +# include +# include +# include + +struct mail { + unsigned short valeur; + struct mail* suivant; +}; + +typedef struct mail maillon; + +maillon* creation_liste(void) { + int i; + maillon* p; + maillon* premier; + maillon* s; + srand(time(NULL)); + premier = (maillon*) malloc(sizeof(maillon)); + if (!premier) { + printf("Erreur d'allocation mémoire. (1) \n"); + } + premier->valeur = (rand() % 889) + 111; + p = premier; + for (i = 0; i != 9; i++) { + s = (maillon*) malloc(sizeof(maillon)); + if (!s) { + printf("Erreur d'allocation mémoire. (2) \n"); + } + s->valeur = (rand() % 889) + 111; + p->suivant = s; + p = s; + } + return premier; +} + +void destruction_liste(maillon* liste) { + maillon* p = liste; + maillon* suivant; + while (p != NULL) { + suivant = p->suivant; + free(p); + p = suivant; + } + printf("Destruction terminée.\n"); +} + +void afficher_liste(maillon* liste) { + maillon* p; + for (p = liste; p != NULL; p = p->suivant) { + printf("| %d |", p->valeur); + } + putchar('\n'); +} + +unsigned short recherche_max(maillon* liste) { + unsigned short maximum = liste->valeur; + maillon* p; + for (p = liste; p != NULL; p = p->suivant) { + if (p->valeur > maximum) { + maximum = p->valeur; + } + } + return maximum; +} + +int main(void) { + maillon* liste = creation_liste(); + afficher_liste(liste); + printf("Maximum de la liste : %hu\n", recherche_max(liste)); + destruction_liste(liste); + return EXIT_SUCCESS; +} + + +2. + diff --git a/DEV1.1/TP25/test.c b/DEV1.1/TP25/test.c index 83652c9..b677eeb 100644 --- a/DEV1.1/TP25/test.c +++ b/DEV1.1/TP25/test.c @@ -1,28 +1,100 @@ # include # include +# include -struct maillon { - int valeur; - struct maillon* suivant; +struct mail { + unsigned short valeur; + struct mail* suivant; }; +typedef struct mail maillon; -void creation_liste(struct maillon* liste) { +maillon* creation_liste(void) { int i; - liste->valeur = (rand() % 888) + 111; - for (i = 0; i != 10; i++) { - struct maillon* prochain = (struct maillon*) malloc(sizeof(struct maillon*)); - if (prochain == NULL) { - printf("Erreur d'allocation mémoire.\n"); + maillon* p; + maillon* premier; + maillon* s; + srand(time(NULL)); + premier = (maillon*) malloc(sizeof(maillon)); + if (!premier) { + printf("Erreur d'allocation mémoire. (1) \n"); } - prochain->valeur = (rand() % 888) + 111; - liste->suivant = prochain; + premier->valeur = (rand() % 889) + 111; + p = premier; + for (i = 0; i != 9; i++) { + s = (maillon*) malloc(sizeof(maillon)); + if (!s) { + printf("Erreur d'allocation mémoire. (2) \n"); + } + s->valeur = (rand() % 889) + 111; + p->suivant = s; + p = s; } + return premier; } +void destruction_liste(maillon* liste) { + maillon* p = liste; + maillon* suivant; + while (p != NULL) { + suivant = p->suivant; + free(p); + p = suivant; + } + printf("Destruction terminée.\n"); +} + +void afficher_liste(maillon* liste) { + maillon* p; + for (p = liste; p != NULL; p = p->suivant) { + printf("| %d |", p->valeur); + } + putchar('\n'); +} + +unsigned short recherche_max(maillon* liste) { + unsigned short maximum = liste->valeur; + maillon* p; + for (p = liste; p != NULL; p = p->suivant) { + if (p->valeur > maximum) { + maximum = p->valeur; + } + } + return maximum; +} + +void retire_dernier(maillon* liste) { + maillon* p; + for (p = liste; p != NULL; p = p->suivant) { + if (p->suivant->suivant == NULL) { + /* Il faut faire pointer l'avant dernier maillon sur NULL */ + p->suivant = NULL; + free(p->suivant); + } + } + printf("Succès.\n"); +} + +maillon* permutation_liste(maillon* liste) { + maillon* p; + maillon* temp = liste; + for (p = liste; p->suivant != NULL; p = p->suivant) { + p = p->suivant; + } + /* TODO */ + liste->valeur = p->valeur; + liste->suivant = p; + return liste; +} + + int main(void) { - struct maillon* liste = NULL; - srand(time(NULL)); - creation_liste(liste); + maillon* liste = creation_liste(); + afficher_liste(liste); + printf("Maximum de la liste : %hu\n", recherche_max(liste)); + /*retire_dernier(liste);*/ + /*afficher_liste(liste);*/ + afficher_liste(permutation_liste(liste)); + destruction_liste(liste); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/DEV1.1/TP27/TP27-reponses.txt b/DEV1.1/TP27/TP27-reponses.txt new file mode 100644 index 0000000..684c42b --- /dev/null +++ b/DEV1.1/TP27/TP27-reponses.txt @@ -0,0 +1,55 @@ +----- TP27 : Récursivité ----- + +1. + + + +2. + +# include +# include + + +int fibonacci(int n) { + if (n == 0) { + return 0; + } + if (n == 1) { + return 1; + } + return (fibonacci(n-2) + fibonacci(n-1)); +} + +void affiche_fibonacci(int m) { + int i; + for (i = 0; i != m; i++) { + printf("%d\n", fibonacci(i)); + } +} + + +int main(void) { + /*printf("%d",fibonacci(7));*/ + affiche_fibonacci(15); +} + +3. + +# include +# include + +void affiche_tableau(double* t, int len) { + if (sizeof(double)*len == sizeof(double)) { + printf("%.2f\n", t[len-1]); + } + else { + printf("%.2lf, ", t[0]); + affiche_tableau(&t[1], len-1); + } +} + +int main(void) { + double t[5] = {1.25, 47.80, 77.01, 54.23, 895.14}; + affiche_tableau(t, 5); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/DEV1.1/TP27/test.c b/DEV1.1/TP27/test.c new file mode 100644 index 0000000..a628dc3 --- /dev/null +++ b/DEV1.1/TP27/test.c @@ -0,0 +1,18 @@ +# include +# include + +void affiche_tableau(double* t, int len) { + if (sizeof(double)*len == sizeof(double)) { + printf("%.2f\n", t[len-1]); + } + else { + printf("%.2lf, ", t[0]); + affiche_tableau(&t[1], len-1); + } +} + +int main(void) { + double t[5] = {1.25, 47.80, 77.01, 54.23, 895.14}; + affiche_tableau(t, 5); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/DEV1.1/TP28/TP28_reponses.txt b/DEV1.1/TP28/TP28_reponses.txt new file mode 100644 index 0000000..f33ad2b --- /dev/null +++ b/DEV1.1/TP28/TP28_reponses.txt @@ -0,0 +1,8 @@ +----- TP28 : Récursivité (suite) ----- + +1. + + + +2. + diff --git a/DEV1.1/TP28/test.c b/DEV1.1/TP28/test.c new file mode 100644 index 0000000..fb496e2 --- /dev/null +++ b/DEV1.1/TP28/test.c @@ -0,0 +1,13 @@ +# include +# include + +int f(int n) { + if (n>100) + return n-10; + else + return f(f(n+11)); +} + +int main(void) { + f(12); +} \ No newline at end of file diff --git a/DEV1.1/controle_machine_2_B/decomposition.c b/DEV1.1/controle_machine_2_B/decomposition.c new file mode 100644 index 0000000..0a65f4f --- /dev/null +++ b/DEV1.1/controle_machine_2_B/decomposition.c @@ -0,0 +1,10 @@ +# include +# include +# include + +int main(int argc, char** argv) { + int partie_entiere; + modf(argv[1], &partie_entiere); + printf("partie entière : %f\npartie décimale : %f", partie_entiere, (argv[1] - partie_entiere)); + return EXIT_SUCCESS; +} \ No newline at end of file