TP5 + 1/2 questions supplémentaires

Intersection ne fonctionne pas encore
master
Geoffrey Frogeye 2017-03-15 12:10:20 +01:00
padre 5dfd8e74ea
commit 46b1ecc22c
Se han modificado 6 ficheros con 335 adiciones y 1 borrados

Ver fichero

@ -78,7 +78,7 @@ void desallouer3(Cellule** l) {
if (*l == NULL) {
return;
}
desallouer(&(*l)->suiv);
desallouer3(&(*l)->suiv);
free(*l);
}

2
TP5/.gitignore vendido Normal file
Ver fichero

@ -0,0 +1,2 @@
questions
supplementaires

9
TP5/Makefile Normal file
Ver fichero

@ -0,0 +1,9 @@
all: $(patsubst %.c,%,$(shell ls *.c))
%: %.c
clang -Wall -Wextra $< -o $@
.PHONY: all clean
clean:
rm *.exe

27
TP5/exemple.txt Normal file
Ver fichero

@ -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

128
TP5/questions.c Normal file
Ver fichero

@ -0,0 +1,128 @@
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
// 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);
}

168
TP5/supplementaires.c Normal file
Ver fichero

@ -0,0 +1,168 @@
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
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');
}