From 8a59a181a18d9a6bf0b9f7d618a165f28f139898 Mon Sep 17 00:00:00 2001 From: HORVILLE Ewen Date: Tue, 28 Sep 2021 15:59:07 +0200 Subject: [PATCH] TP07 Boucles et Boucles (suite) --- APL1.1/TP07/bornes.c | 20 ++++++++++++ APL1.1/TP07/devinettes.c | 26 ++++++++++++++++ APL1.1/TP07/division.c | 26 ++++++++++++++++ APL1.1/TP07/facteurs.c | 40 ++++++++++++++++++++++++ APL1.1/TP07/figures.c | 63 ++++++++++++++++++++++++++++++++++++++ APL1.1/TP07/filtrage_bac.c | 18 +++++++++++ APL1.1/TP07/premier.c | 22 +++++++++++++ APL1.1/TP07/progression.c | 27 ++++++++++++++++ APL1.1/TP07/sequence.c | 16 ++++++++++ APL1.1/TP07/table.c | 15 +++++++++ APL1.1/TP07/table_3.c | 33 ++++++++++++++++++++ APL1.1/TP07/tables.c | 17 ++++++++++ 12 files changed, 323 insertions(+) create mode 100644 APL1.1/TP07/bornes.c create mode 100644 APL1.1/TP07/devinettes.c create mode 100644 APL1.1/TP07/division.c create mode 100644 APL1.1/TP07/facteurs.c create mode 100644 APL1.1/TP07/figures.c create mode 100644 APL1.1/TP07/filtrage_bac.c create mode 100644 APL1.1/TP07/premier.c create mode 100644 APL1.1/TP07/progression.c create mode 100644 APL1.1/TP07/sequence.c create mode 100644 APL1.1/TP07/table.c create mode 100644 APL1.1/TP07/table_3.c create mode 100644 APL1.1/TP07/tables.c diff --git a/APL1.1/TP07/bornes.c b/APL1.1/TP07/bornes.c new file mode 100644 index 0000000..6dbc5db --- /dev/null +++ b/APL1.1/TP07/bornes.c @@ -0,0 +1,20 @@ +#include +#include + +int main(void) { + + int arg, min, max; + printf("Entrez des valeurs (-1 pour arreter) : "); + scanf("%d", &arg); + min = arg; + max = arg; + + while(arg != -1) { + min = arg < min ? arg : min; + max = arg > max ? arg : max; + scanf("%d", &arg); + } + + printf("Minimum : %d\nMaximum : %d\n", min, max); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/APL1.1/TP07/devinettes.c b/APL1.1/TP07/devinettes.c new file mode 100644 index 0000000..9354a2a --- /dev/null +++ b/APL1.1/TP07/devinettes.c @@ -0,0 +1,26 @@ +#include +#include +#include + +int main(int argc, char * argv[]) { + srand(time(NULL)); + int ans = rand() % 100; + int guess; + + for (int attempt = 0; attempt < 5; attempt++) { + printf("%d essai(s) restant(s)\n", 5 - attempt); + scanf("%d", &guess); + + if (guess == ans) { + printf("Bravo, vous avez trouvé !\n"); + return EXIT_SUCCESS; + } + + if (guess > ans) printf("La réponse est - grande.\n"); + else printf("La réponse est + grande.\n"); + } + + printf("Dommage, la bonne réponse était %d.\n", ans); + + return EXIT_SUCCESS; +} diff --git a/APL1.1/TP07/division.c b/APL1.1/TP07/division.c new file mode 100644 index 0000000..a024e10 --- /dev/null +++ b/APL1.1/TP07/division.c @@ -0,0 +1,26 @@ +#include +#include + +int main(void) { + int arg1, arg2, arg3; + + printf("Veuillez entrer un entier positif : "); + scanf("%d", &arg1); + arg3 = arg1; + + printf("Veuillez entrer un entier supérieur à 0 : "); + scanf("%d", &arg2); + + int quotient, reste; + quotient = 0; + + while ( arg1 >= arg2 ) { + quotient++; + arg1 -= arg2; + } + + reste = arg1; + printf("La division euclidienne de %d par %d est : \n", arg3, arg2); + printf("Quotient : %d\n", quotient); + printf("Reste : %d\n", reste); +} diff --git a/APL1.1/TP07/facteurs.c b/APL1.1/TP07/facteurs.c new file mode 100644 index 0000000..fa1f623 --- /dev/null +++ b/APL1.1/TP07/facteurs.c @@ -0,0 +1,40 @@ +#include +#include + +int prime(int value) { + for (int diviseur = 2; diviseur < value; diviseur++) { + if (value == 1 ) { + return 0; + } else if (value % diviseur == 0 && diviseur != value) { + return 0; + } else if (value == 2) return 1; + } + + return 1; +} + +int main(int argc, char * argv[]) { + int value, remainder; + + printf("Veuillez donner un entier naturel : "); + scanf("%d", &value); + remainder = value; + + printf("%d = ", value); + + while (!prime(remainder)) { + int factor; + + for (int i = 2; i < remainder; i++) { + if (prime(i) && remainder % i == 0) { + factor = i; + remainder = remainder / factor; + break; + } + } + + printf("%d*", factor); + } + printf("%d\n", remainder); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/APL1.1/TP07/figures.c b/APL1.1/TP07/figures.c new file mode 100644 index 0000000..cd3ca49 --- /dev/null +++ b/APL1.1/TP07/figures.c @@ -0,0 +1,63 @@ +#include +#include +#include + +int sq(int x) { + return x*x; +} + +int getmaxsq(int value) { + int max_square = 0; + + for (int i = 0; i <= value; i++) { + if ( value - sq(i) >= 0) max_square = i; + else break; + } + + return max_square; +} + +void printr(int value, int v1, int v2, int v3, int v4) { + printf("%d = %d + %d + %d + %d\n", value, sq(v1), sq(v2), sq(v3), sq(v4)); +} + +int main(int argc, char * argv[]) { + int value; + + printf("Veuillez donner un entier naturel : "); + scanf("%d", &value); + + int ms1 = getmaxsq(value); + for (int i1 = ms1; i1 > 0 && sq(i1)*4 >= value && value - sq(i1) < sq(i1+1); i1--) { + int val1 = value - sq(i1); + + if (val1 == 0) printr(value, i1, 0, 0, 0); + else { + int ms2 = getmaxsq(val1); + for (int i2 = ms2; i2 > 0 && sq(i2)*4 >= val1 && val1 - sq(i2) < sq(i2+1); i2--) { + int val2 = val1 - sq(i2); + + if (val2 == 0) printr(value, i1, i2, 0, 0); + else { + int ms3 = getmaxsq(val2); + for (int i3 = ms3; i3 > 0 && sq(i3)*4 >= val2 && val2 - sq(i3) < sq(i3+1); i3--) { + int val3 = val2 - sq(i3); + + if (val3 == 0) printr(value, i1, i2, i3, 0); + else { + int ms4 = getmaxsq(val3); + for (int i4 = ms4; i4 > 0 && sq(i4)*4 >= val3 && val3 - sq(i4) < sq(i4+1); i4--) { + int val4 = val3 - sq(i4); + + if (val4 == 0) printr(value, i1, i2, i3, i4); + } + } + } + } + } + } + } + + return EXIT_SUCCESS; +} + diff --git a/APL1.1/TP07/filtrage_bac.c b/APL1.1/TP07/filtrage_bac.c new file mode 100644 index 0000000..78bb672 --- /dev/null +++ b/APL1.1/TP07/filtrage_bac.c @@ -0,0 +1,18 @@ +#include +#include + +int main(int argc, char * argv[]) { + int note = 0; + + do { + printf("Veuillez donner votre note du bac : "); + scanf("%d", ¬e); + } while ( note < 10 || note > 20); + + if (note >= 16) printf("Vous avez eu mention 'Très Bien'.\n"); + else if (note >= 14) printf("Vous avez eu mention 'Bien'.\n"); + else if (note >= 12) printf("Vous avez eu mention 'Assez Bien'.\n"); + else printf("Vous n'avez pas eu de mention.\n"); + + return EXIT_SUCCESS; +} diff --git a/APL1.1/TP07/premier.c b/APL1.1/TP07/premier.c new file mode 100644 index 0000000..ed6317b --- /dev/null +++ b/APL1.1/TP07/premier.c @@ -0,0 +1,22 @@ +#include +#include + +int main(int argc, char * argv[]) { + int entier; + + printf("Veuillez donner un entier naturel : "); + scanf("%d", &entier); + + for (int diviseur = 2; diviseur < entier; diviseur++) { + if (entier == 1 ) { + printf("Cet entier n'est pas premier.\n"); + return EXIT_SUCCESS; + } else if (entier % diviseur == 0 && diviseur != entier) { + printf("Cet entier n'est pas premier.\n"); + return EXIT_SUCCESS; + } + } + + printf("Cet entier est premier.\n"); + return EXIT_SUCCESS; +} diff --git a/APL1.1/TP07/progression.c b/APL1.1/TP07/progression.c new file mode 100644 index 0000000..a5822ff --- /dev/null +++ b/APL1.1/TP07/progression.c @@ -0,0 +1,27 @@ +#include +#include + +int main(int argc, char * argv[]) { + int desired_index, value; + + printf("Saisissez le numéro de terme souhaité : "); + scanf("%d", &desired_index); + + int index_m2 = 0; + int index_m1 = 1; + + if (desired_index < 2 && desired_index >= 0 ) value = desired_index; + else if (desired_index > 1) { + for (int index = 2; index <= desired_index; index++) { + value = index_m2 + index_m1; + index_m2 = index_m1; + index_m1 = value; + } + } else { + printf("Valeur invalide.\n"); + return EXIT_SUCCESS; + } + + printf("Le %dème terme de la suite est : %d\n", desired_index, value); + return EXIT_SUCCESS; +} diff --git a/APL1.1/TP07/sequence.c b/APL1.1/TP07/sequence.c new file mode 100644 index 0000000..e1c2db5 --- /dev/null +++ b/APL1.1/TP07/sequence.c @@ -0,0 +1,16 @@ +#include +#include + +int main(int argc, char * argv[]) { + int arg1, arg2; + printf("Veuillez donner deux entiers séparés d'un espace : "); + scanf("%d %d", &arg1, &arg2); + + printf("Les nombres compris entre %d et %d sont :\n", arg1, arg2); + for (; arg1 <= arg2; arg1++) { + printf("%d ", arg1); + } + printf("\n"); + + return EXIT_SUCCESS; +} diff --git a/APL1.1/TP07/table.c b/APL1.1/TP07/table.c new file mode 100644 index 0000000..b106665 --- /dev/null +++ b/APL1.1/TP07/table.c @@ -0,0 +1,15 @@ +#include +#include + +int main(int argc, char * argv[]) { + int table; + + printf("Veuillez donner un entier : "); + scanf("%d", &table); + + for (int i = 1; i <= 10; i++) { + printf("%d * %2d = %3d\n", table, i, table * i); + } + + return EXIT_SUCCESS; +} diff --git a/APL1.1/TP07/table_3.c b/APL1.1/TP07/table_3.c new file mode 100644 index 0000000..eec329f --- /dev/null +++ b/APL1.1/TP07/table_3.c @@ -0,0 +1,33 @@ +#include +#include + +#define TABLE_WIDTH 15 +#define TABLE_HEIGHT 15 + +int main(int argc, char * argv[]) { + + //En-tête de la table sur toute la ligne. + printf(" X | "); + for (int col = 0; col <= TABLE_WIDTH; col++) { + printf(" %3d ", col); + } + printf("\n"); + + //Séparation de la table sur toute la ligne. + printf("-----+"); + for (int col = 0; col <= TABLE_WIDTH; col++) { + printf("-----", col); + } + printf("\n"); + + //Affichage de la table de multiplication + en-tête + for (int lin = 0; lin <= TABLE_HEIGHT; lin++) { + printf(" %3d | ", lin); + for (int col = 0; col <= TABLE_WIDTH; col++) { + printf(" %3d ", lin * col); + } + printf("\n"); + } + + return EXIT_SUCCESS; +} diff --git a/APL1.1/TP07/tables.c b/APL1.1/TP07/tables.c new file mode 100644 index 0000000..769d3a2 --- /dev/null +++ b/APL1.1/TP07/tables.c @@ -0,0 +1,17 @@ +#include +#include + +int main(void) { + int table; + + while(1) { + printf("Veuillez donner un entier : "); + scanf("%d", &table); + + if (table == -1) return EXIT_SUCCESS; + + for (int i = 1; i <= 10; i++) { + printf("%d * %d = %d\n", i, table, i*table); + } + } +}