From de651f92e51754c41c7afa5830b2327e851a6dee Mon Sep 17 00:00:00 2001 From: HORVILLE Ewen Date: Tue, 9 Nov 2021 15:50:11 +0100 Subject: [PATCH] Progression TP01 --- APL1.2/TP01/palindromes.c | 21 ++++++++++++++++ APL1.2/TP01/precognition.c | 40 +++++++++++++++++++++++++++++++ APL1.2/TP01/{date.c => premier.c} | 0 APL1.2/TP01/singletons.c | 32 +++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 APL1.2/TP01/palindromes.c create mode 100644 APL1.2/TP01/precognition.c rename APL1.2/TP01/{date.c => premier.c} (100%) create mode 100644 APL1.2/TP01/singletons.c diff --git a/APL1.2/TP01/palindromes.c b/APL1.2/TP01/palindromes.c new file mode 100644 index 0000000..78b241e --- /dev/null +++ b/APL1.2/TP01/palindromes.c @@ -0,0 +1,21 @@ +#include +#include +#include + +char* inverse(const char* s) { + char* inv_str = (char*) calloc(strlen(s), sizeof(char)); + for (int i = strlen(s)-1; i >= 0; i--) { + inv_str[i] = s[strlen(s)-i-1]; + } + + return inv_str; +} + +int main(int argc, char * argv[]) { + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], inverse(argv[i])) == 0) printf("%s est palindrome.\n", argv[i]); + else printf("%s n'est pas palindrome.\n", argv[i]); + } + return EXIT_SUCCESS; +} + diff --git a/APL1.2/TP01/precognition.c b/APL1.2/TP01/precognition.c new file mode 100644 index 0000000..fff3135 --- /dev/null +++ b/APL1.2/TP01/precognition.c @@ -0,0 +1,40 @@ +#include +#include +#include + +int main(int argc, char * argv[]) { + printf("Veuillez donner vos valeurs : \n"); + double* list = (double*) calloc(1, sizeof(double)); + int size = 1; + + do { + char reel[50]; + scanf("%s", &reel); + if (reel[0] == 'q') break; + + int found = 0; + double var = strtod(reel, NULL); + for (int i = 0; i < size; i++) { + if (list[i] == var) found = 1; + } + + if (!found) { + list[size-1] = var; + + size++; + double* new_list = (double*) realloc(list, size * sizeof(double)); + list = new_list; + } + + } while(1); + + for (int i = 0; i < size-1; i++) { + printf("%f ", list[i]); + } + + printf("\n"); + free(list); + + return EXIT_SUCCESS; +} + diff --git a/APL1.2/TP01/date.c b/APL1.2/TP01/premier.c similarity index 100% rename from APL1.2/TP01/date.c rename to APL1.2/TP01/premier.c diff --git a/APL1.2/TP01/singletons.c b/APL1.2/TP01/singletons.c new file mode 100644 index 0000000..7b39e3d --- /dev/null +++ b/APL1.2/TP01/singletons.c @@ -0,0 +1,32 @@ +#include +#include +#include + +int main(int argc, char * argv[]) { + if (argc < 3) { + puts("Veuillez renseigner deux valeurs ou plus."); + return EXIT_FAILURE; + } + + double* list = (double*) calloc(argc-1, sizeof(double)); + + for (int i = 0; i < argc-1; i++) { + double val = strtod(argv[i+1], NULL); + int found = 0; + + for (int j = 0; j < i; j++) { + if (list[j] == val) found = 1; + } + + if (!found) { + list[i] = val; + printf("%.2f ", val); + } + } + + free(list); + printf("\n"); + + return EXIT_SUCCESS; +} +