diff --git a/TP10/.gitignore b/TP10/.gitignore new file mode 100644 index 0000000..a606f56 --- /dev/null +++ b/TP10/.gitignore @@ -0,0 +1,3 @@ +exo1 +exo2 +exo3 diff --git a/TP10/Makefile b/TP10/Makefile new file mode 100644 index 0000000..95c1138 --- /dev/null +++ b/TP10/Makefile @@ -0,0 +1,9 @@ +all: $(patsubst %.c,%,$(shell ls *.c)) + +%: %.c + clang -Wall -Wextra $< -o $@ -g + +.PHONY: all clean + +clean: + rm *.exe diff --git a/TP10/exo1.c b/TP10/exo1.c new file mode 100644 index 0000000..e995ede --- /dev/null +++ b/TP10/exo1.c @@ -0,0 +1,30 @@ +#include +#include + +// 2.1.1 +// Signature : int f(int) +int fois_deux(int a) { + return a*2; +} + +// 2.1.2 +void appliquer_tableau(int f(int), int t[], int size) { + int i; + for (i = 0; i < size; i++) { + t[i] = f(t[i]); + } +} + +// 2.1.3 +#define SIZE 5 + +int main() { + int t[SIZE] = {1, 4, 7, 9, 3}; + int i; + + appliquer_tableau(fois_deux, t, SIZE); + + for (i = 0; i < SIZE; i++) { + printf("t[%d] = %d\n", i, t[i]); + } +} diff --git a/TP10/exo2.c b/TP10/exo2.c new file mode 100644 index 0000000..1976e15 --- /dev/null +++ b/TP10/exo2.c @@ -0,0 +1,79 @@ +#include +#include + + +// 2.2.1 +void triBulleOld(int tab[], int size){ + int i, j, tmp; + for(i = size - 1; i > 0; i--){ + for(j = 0; j < i; j++){ + // 2.2.2 + // Il faut modifier le < en > pour faire un tri décroissant + if (tab[j+1] < tab[j]){ + tmp = tab[j+1]; + tab[j+1] = tab[j]; + tab[j] = tmp; + } + } + } +} + +// 2.2.3 +int superieur(int a, int b) { + if (a > b) { + return 1; + } else if (a == b) { + return 0; + } else { + return -1; + } +} + +// 2.2.4 +int inferieur(int a, int b) { + if (a < b) { + return 1; + } else if (a == b) { + return 0; + } else { + return -1; + } +} + +// 2.2.5 +void triBulle(int tab[], int size, int compare(int, int)) { + int i, j, tmp; + for(i = size - 1; i > 0; i--){ + for(j = 0; j < i; j++){ + // 2.2.2 + // Il faut modifier le < en > pour faire un tri décroissant + if (compare(tab[j+1], tab[j]) == 1){ + tmp = tab[j+1]; + tab[j+1] = tab[j]; + tab[j] = tmp; + } + } + } +} + +#define SIZE 5 + +void afficherTableau(int t[], int size) { + int i; + for (i = 0; i < size; i++) { + printf("t[%d] = %d\n", i, t[i]); + } +} + +int main() { + int t[SIZE] = {1, 4, 7, 9, 3}; + + printf("Tri avec inferieur\n"); + triBulle(t, SIZE, inferieur); + afficherTableau(t, SIZE); + + printf("Tri avec superieur\n"); + triBulle(t, SIZE, superieur); + afficherTableau(t, SIZE); + +} diff --git a/TP10/exo3.c b/TP10/exo3.c new file mode 100644 index 0000000..8b3a392 --- /dev/null +++ b/TP10/exo3.c @@ -0,0 +1,72 @@ +#include +#include +#include + +// 3.1.1 +// À mon avis les void* correspondent à n'importe quel type + +// 3.1.2 +int superieur(const void* A, const void* B) { + int a = *(int*) A; + int b = *(int*) B; + if (a > b) { + return 1; + } else if (a == b) { + return 0; + } else { + return -1; + } +} + +int inferieur(const void* A, const void* B) { + int a = *(int*) A; + int b = *(int*) B; + if (a < b) { + return 1; + } else if (a == b) { + return 0; + } else { + return -1; + } +} + +#define SIZE 5 +#define STR_SIZE 255 + +void afficherTableau(int t[], int size) { + int i; + for (i = 0; i < size; i++) { + printf("t[%d] = %d\n", i, t[i]); + } +} + +int cmpstr(const void* A, const void* B) { + const char* a = (const char*)A; + const char* b = (const char*)B; + return strcmp(a, b); +} + + +int main() { + int t[SIZE] = {1, 4, 7, 9, 3}; + + printf("Tri avec inferieur\n"); + qsort(t, SIZE, sizeof(int), inferieur); + afficherTableau(t, SIZE); + + printf("Tri avec superieur\n"); + qsort(t, SIZE, sizeof(int), superieur); + afficherTableau(t, SIZE); + + char c[SIZE][STR_SIZE] = {"Bonjour,\0", "comment\0", "allez\0", "vous\0", "?\0"}; + + /* int (compare(const void*, const void*)) = strcmp; */ + printf("Tri avec strcmp\n"); + qsort(c, SIZE, sizeof(char) * STR_SIZE, cmpstr); + + int i; + for (i = 0; i < SIZE; i++) { + printf("t[%d] = %s\n", i, c[i]); + } + +}