diff --git a/TP4/questions.c b/TP4/questions.c index dccb852..2a992cc 100644 --- a/TP4/questions.c +++ b/TP4/questions.c @@ -78,7 +78,7 @@ void desallouer3(Cellule** l) { if (*l == NULL) { return; } - desallouer(&(*l)->suiv); + desallouer3(&(*l)->suiv); free(*l); } diff --git a/TP5/.gitignore b/TP5/.gitignore new file mode 100644 index 0000000..6c2b59f --- /dev/null +++ b/TP5/.gitignore @@ -0,0 +1,2 @@ +questions +supplementaires diff --git a/TP5/Makefile b/TP5/Makefile new file mode 100644 index 0000000..a216135 --- /dev/null +++ b/TP5/Makefile @@ -0,0 +1,9 @@ +all: $(patsubst %.c,%,$(shell ls *.c)) + +%: %.c + clang -Wall -Wextra $< -o $@ + +.PHONY: all clean + +clean: + rm *.exe diff --git a/TP5/exemple.txt b/TP5/exemple.txt new file mode 100644 index 0000000..f6caf23 --- /dev/null +++ b/TP5/exemple.txt @@ -0,0 +1,27 @@ +4 +4 +-6 +3 +4 +-4 +-4 +8 +8 +-5 +-5 +7 +-4 +-6 +-6 +3 +-6 +2 +2 +3 +3 +4 +3 +4 +3 +1 +-8 diff --git a/TP5/questions.c b/TP5/questions.c new file mode 100644 index 0000000..6c3e90e --- /dev/null +++ b/TP5/questions.c @@ -0,0 +1,128 @@ +#include +#include +#include + +// 2.2.1 +struct Cellule { + struct Cellule* suiv; + int val; + int mult; +}; +typedef struct Cellule Cellule; + +void ajout_tete(Cellule** l, int val, int mult) { + Cellule* p; + p = malloc(sizeof(Cellule)); + p->val = val; + p->suiv = *l; + p->mult = mult; + *l = p; +} + +void imprimer(Cellule* l) { + while (l != NULL) { + printf("%d × %d\n", l->val, l->mult); + l = l->suiv; + } +} + +void supprimer_tete(Cellule** l) { + Cellule* p; + p = (*l)->suiv; + free(*l); + *l = p; +} + +// 2.2.2 +void inserer(Cellule** p, int val) { + Cellule* l = *p; + if (l == NULL) { + ajout_tete(p, val, 1); + } else if (l->val == val) { + l->mult++; + } else if (l->val > val) { + ajout_tete(p, val, 1); + } else { + inserer(&l->suiv, val); + } +} + +// 2.2.3 +Cellule* lire_fichier(FILE* fp) { + Cellule* l = NULL; + int val; + while (fscanf(fp, "%d", &val) != EOF) { + inserer(&l, val); + } + return l; +} + +// 2.3.1 +bool est_trie(Cellule* l) { + if (l == NULL || l->suiv == NULL) { + return true; + } + if (l->val < l->suiv->val) { + return est_trie(l->suiv); + } else { + return false; + } +} + +// 2.3.2 +void desallouer(Cellule* l) { + if (l == NULL) { + return; + } + desallouer(l->suiv); + free(l); +} + +// 2.3.3 +int multiplicite(Cellule* l, int val) { + if (l == NULL) { + return 0; + } + if (l->val == val) { + return l->mult; + } else { + return multiplicite(l->suiv, val); + } +} + +// 2.3.3 +int cardinal(Cellule* l) { + if (l == NULL) { + return 0; + } + return l->mult + cardinal(l->suiv); +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + printf("Usage : %s FICHIER_ENTREE\n", argv[0]); + return EXIT_FAILURE; + } + + FILE* fp; + fp = fopen(argv[1], "r"); + if (fp == NULL) { + printf("Impossible d'ouvrir le fichier %s.\n", argv[1]); + return EXIT_FAILURE; + } + + Cellule* l = lire_fichier(fp); + + fclose(fp); + + imprimer(l); + + printf("l est trié : %d\n", est_trie(l)); + + printf("multiplicité de 3 : %d\n", multiplicite(l, 3)); + + printf("cardinal de l : %d\n", cardinal(l)); + + desallouer(l); + +} diff --git a/TP5/supplementaires.c b/TP5/supplementaires.c new file mode 100644 index 0000000..0dfb278 --- /dev/null +++ b/TP5/supplementaires.c @@ -0,0 +1,168 @@ +#include +#include +#include + +struct Cellule { + struct Cellule* suiv; + int val; + int mult; +}; +typedef struct Cellule Cellule; + +void ajout_tete(Cellule** l, int val, int mult) { + Cellule* p; + p = malloc(sizeof(Cellule)); + p->val = val; + p->suiv = *l; + p->mult = mult; + *l = p; +} + +void imprimer(Cellule* l) { + while (l != NULL) { + printf("%d × %d\n", l->val, l->mult); + l = l->suiv; + } +} + +void supprimer_tete(Cellule** l) { + Cellule* p; + p = (*l)->suiv; + free(*l); + *l = p; +} + +void inserer(Cellule** p, int val) { + Cellule* l = *p; + if (l == NULL) { + ajout_tete(p, val, 1); + } else if (l->val == val) { + l->mult++; + } else if (l->val > val) { + ajout_tete(p, val, 1); + } else { + inserer(&l->suiv, val); + } +} + +bool est_trie(Cellule* l) { + if (l == NULL || l->suiv == NULL) { + return true; + } + if (l->val < l->suiv->val) { + return est_trie(l->suiv); + } else { + return false; + } +} + +void desallouer(Cellule* l) { + if (l == NULL) { + return; + } + desallouer(l->suiv); + free(l); +} + +int multiplicite(Cellule* l, int val) { + if (l == NULL) { + return 0; + } + if (l->val == val) { + return l->mult; + } else { + return multiplicite(l->suiv, val); + } +} + +int cardinal(Cellule* l) { + if (l == NULL) { + return 0; + } + return l->mult + cardinal(l->suiv); +} + +// Pour simplifier l'affichage +void details(Cellule *l, char nom) { + printf("\nListe %c :\n", nom); + imprimer(l); + if (est_trie(l)) { + printf("→ Est trié\n"); + } else { + printf("→ N'est pas triée\n"); + } +} + +Cellule* union_me(Cellule* j, Cellule* k) { + Cellule* u; + int i; + if (j == NULL) { + if (k == NULL) { + return NULL; + } else { + u = union_me(NULL, k->suiv); + for (i = 0; i < k->mult; i++) { + inserer(&u, k->val); + } + return u; + } + } else { + u = union_me(j->suiv, k); + for (i = 0; i < j->mult; i++) { + inserer(&u, j->val); + } + return u; + } +} + +int min(int a, int b) { + if (a > b) { + return b; + } else { + return a; + } +} + +Cellule* intersection_me(Cellule* j, Cellule* k) { + if (j == NULL || k == NULL) { + return NULL; + } else if (j->val == k->val) { + Cellule* i; + i = intersection_me(j->suiv, k->suiv); + inserer(&i, min(j->val, k->val)); + return i; + } else if (j->val < k->val) { + return intersection_me(j->suiv, k); + } else { + return intersection_me(j, k->suiv); + } +} + + +int main() { + Cellule* j = NULL; + inserer(&j, 3); + inserer(&j, 2); + inserer(&j, 3); + inserer(&j, 2); + inserer(&j, 1); + inserer(&j, 3); + details(j, 'j'); + + Cellule* k = NULL; + inserer(&k, 3); + inserer(&k, 3); + inserer(&k, 2); + inserer(&k, 5); + inserer(&k, 5); + inserer(&k, 5); + details(k, 'k'); + + Cellule* u = union_me(j, k); + details(u, 'u'); + + Cellule* i = intersection_me(j, k); + details(i, 'i'); + + +}