TP6 n ary tree: add the corrections
[Algorithmic_C.git] / TP6 / arbres / arbre_n_aire_c2 / arbre_n_aire_correction.c
diff --git a/TP6/arbres/arbre_n_aire_c2/arbre_n_aire_correction.c b/TP6/arbres/arbre_n_aire_c2/arbre_n_aire_correction.c
new file mode 100644 (file)
index 0000000..71cb523
--- /dev/null
@@ -0,0 +1,304 @@
+/*****************************************************************************/
+/*                          arbre_n_aire.c                                   */
+/*               Representation de mots sous forme d'arbre n-aire            */
+/*****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#define N 30
+
+
+typedef struct noeud {
+    char lettre;
+    struct noeud *fils;
+    struct noeud *frere;
+} NOEUD;
+
+
+/* Recherche d'un mot dans l'arbre *****************************************/
+NOEUD *cherche(NOEUD * p, char *mot, int i)
+{
+    if (p == NULL)
+       return NULL;
+    if (p->lettre == mot[i]) {
+       if (mot[i])
+           return cherche(p->fils, mot, i + 1);
+       else
+           return p;
+    } else if (p->lettre > mot[i])
+       return NULL;
+    else
+       return cherche(p->frere, mot, i);
+}
+
+/***************************************************************************/
+NOEUD *recherche(NOEUD * p, char *mot)
+{
+    return cherche(p, mot, 0);
+}
+
+
+/* Création d'une fin de mot - liste chaînée suivant le lien fils **********/
+NOEUD *insere_fin(char *mot, int i)
+{
+    NOEUD *p = (NOEUD *) malloc(sizeof(NOEUD));
+
+    if (p == NULL)
+       exit(-1);
+    p->lettre = mot[i];
+    p->fils = NULL;
+    p->frere = NULL;
+    if (mot[i])
+       p->fils = insere_fin(mot, i + 1);
+    return p;
+}
+
+
+/* Insertion d'un mot dans l'arbre ******************************************/
+/* on respecte l'ordre lexicographique des frères **************************/
+NOEUD *insere(NOEUD * p, char *mot, int i)
+{
+    NOEUD *p1;
+
+    if (p == NULL)
+       p = insere_fin(mot, i);
+    else if (p->lettre == mot[i])
+       if (mot[i] != '\0')     /* if (mot[i]) */
+           p->fils = insere(p->fils, mot, i + 1);
+       else
+           printf("Le mot est deja dans le dictionnaire\n");
+    else if (p->lettre > mot[i]) {
+       p1 = insere_fin(mot, i);
+       p1->frere = p;
+       p = p1;
+    } else
+       p->frere = insere(p->frere, mot, i);
+    return p;
+}
+
+
+/*****************************************************************************/
+/* Affichage par ordre alphabétique de tous les mots stockés dans l'arbre    */
+void affiche_fich(FILE * fp, NOEUD * p, char *mot, int i)
+{
+    int j;
+
+    if (p) {
+       mot[i] = p->lettre;
+       if (!mot[i]) {
+           for (j = 0; j < i; j++)
+               fprintf(fp, "%c", mot[j]);
+           fprintf(fp, "\n");
+       };
+       affiche_fich(fp, p->fils, mot, i + 1);
+       affiche_fich(fp, p->frere, mot, i);
+    }
+}
+
+
+/* Affichage par ordre alphabétique de tous les mots stockés dans l'arbre    */
+/* parcours en profondeur d'abord, prefixe (idem si infixe, voir la remarque plus loin) */
+void affiche(NOEUD * p, char *mot, int i)
+{
+    int j;
+
+    if (p) {
+       mot[i] = p->lettre;
+       if (mot[i] == '\0')
+           printf("%s\n", mot);        /* ici ou apres l'appel sur le fils ('\0' n'a pas de fils , donc l'appel sur le fils n'affichera rien */
+       affiche(p->fils, mot, i + 1);
+       affiche(p->frere, mot, i);
+    }
+}
+
+
+/*****************************************************************************/
+/* Visualisation de l'arbre n-aire - parcour "frère - racine - fils"         */
+void affiche_arbre(NOEUD * p, int prof)
+{
+    int i;
+
+    if (p) {
+       affiche_arbre(p->frere, prof);
+       for (i = 0; i < prof; i++)
+           printf(" ");
+       printf("%c\n", p->lettre);
+       affiche_arbre(p->fils, prof + 1);
+    }
+}
+
+
+/*****************************************************************************/
+/* Attention à ne supprimer que la fin du mot qui n'est partagée par aucun 
+   autre mot de l'arbre                                                      */
+NOEUD *supprime(NOEUD * p, char *mot, int i)
+{
+    NOEUD *p1;
+
+    if (p)
+       if (p->lettre < mot[i])
+           p->frere = supprime(p->frere, mot, i);
+       else if (p->lettre == mot[i]) {
+           p->fils = supprime(p->fils, mot, i + 1);
+           if (!p->fils) {
+               p1 = p;
+               p = p->frere;
+               free(p1);
+           }
+       } else
+           printf("%s n'est pas present\n", mot);
+    return p;
+}
+
+/*****************************************************************************/
+NOEUD *charge_dico(char *nom_fichier, int *nb_mots)
+{
+    NOEUD *p;
+    FILE *fp;
+    char mot[N];
+    int i;
+
+    p = NULL;
+    fp = fopen(nom_fichier, "rt");
+    if (fp == NULL)
+       exit(-1);
+    fscanf(fp, "%d", nb_mots); /* sur la 1ère ligne du fichier, le nombre de mots */
+    for (i = 0; i < *nb_mots; i++) {
+       fscanf(fp, "%s", mot);
+       p = insere(p, mot, 0);
+    }
+    fclose(fp);
+    return p;
+}
+
+
+/*****************************************************************************/
+void sauve_dico(NOEUD * p, char *nom_fichier, int nb_mots)
+{
+    FILE *fp;
+    char mot[N];
+
+    fp = fopen(nom_fichier, "wt");
+    if (fp == NULL)
+       exit(-1);
+    fprintf(fp, "%d\n", nb_mots);
+    affiche_fich(fp, p, mot, 0);
+    fclose(fp);
+}
+
+
+/*****************************************************************************/
+char EstSeparateur(char c)
+{
+    return (((c == '\'') || (c == '"') || (c == ',') || (c == ';')
+            || (c == '.') || (c == '\n') || (c == '?') || (c == '!')
+            || (c == ':') || (c == ' ') || (c == '\t') || (c == '-')
+            || (c == '*') || (c == '+') || (c == '=') || (c == '\b')
+            || (c == '{') || (c == '}') || (c == '(') || (c == ')')));
+}
+
+
+/****************************************************************************/
+/*  Lit un mot a partir du fichier fd.
+  texte == <separateur>* <mot> <separateur>*
+  mot = <caractere>*.
+  Le mot est retourné sous une forme minuscule.
+  Quant on a atteint la fin de fichier, la fonction retourne 0             */
+int Lectmot(FILE * fp, char *mot)
+{
+    int i = 0;
+    char c;
+
+    /* Lecture des separateurs */
+    while (((c = (char) fgetc(fp)) != (char) EOF) && (EstSeparateur(c)));
+
+    /* Debut du mot */
+    while (c != (char) EOF) {
+       if (!EstSeparateur(c))
+           mot[i++] = tolower(c);
+       else
+           break;
+       c = (char) fgetc(fp);
+    }
+    mot[i] = '\0';
+    return (i);
+}
+
+
+/****************************************************************************/
+
+/* voici un autre programme principal pour procéder à une vérification
+   orthographique interactive d'un texte.
+
+Un exemple d'utilisation :
+ ./arbre_n_aire dictionnaire.txt mondico.txt letexte.txt motsinconnus.txt
+
+Les rguments de la ligne de commande sont :
+   argv[1] est le nom du dictionnaire d'entree
+   argv[2] est le nom du dictionnaire apres insertion des nouveaux mots
+   argv[3] est le texte a verifier
+   argv[4] est un fichier contenant la liste des mots inconnus du texte     
+*/
+
+int main(int argc, char *argv[])
+{
+    char mot[N];
+    char rep;
+    NOEUD *rac;
+    FILE *fentree, *fsortie;
+    int nb_mots;
+
+    if (argc != 5) {
+       printf
+           ("Usage: <prog> <dico_entree> <dico_sortie> <texte> <mots_inconnus>\n");
+       exit(-1);
+    };
+
+    rac = charge_dico(argv[1], &nb_mots);
+
+    /* pour afficher le dictionnaire charge (attention le dictionnaire peut etre volumineux */
+    /* affiche_fich(stdout,rac,mot,0); */
+    /* affiche_arbre(rac,0); */
+
+    fentree = fopen(argv[3], "rt");
+    if (fentree == NULL)
+       exit(-1);
+    fsortie = fopen(argv[4], "wt");
+    if (fsortie == NULL)
+       exit(-1);
+
+    while (Lectmot(fentree, mot) > 0) {
+       if (!cherche(rac, mot, 0)) {
+           printf
+               ("Le mot <%s> n'est pas dans le dictionnaire - Dois-je l'inserer (o/n) ? ",
+                mot);
+           scanf("%c", &rep);
+           getchar();
+           if (rep == 'o') {
+               rac = insere(rac, mot, 0);
+               nb_mots++;
+           } else
+               fprintf(fsortie, "%s\n", mot);
+       }
+    }
+
+    fclose(fentree);
+    fclose(fsortie);
+
+    sauve_dico(rac, argv[2], nb_mots);
+
+/* pour tester la suppression */
+    do {
+       printf("Entrez un mot a supprimer (stop pour arreter) : ");
+       scanf("%s", mot);
+       if (strcmp(mot, "stop")) {
+           rac = supprime(rac, mot, 0);
+           affiche_fich(stdout, rac, mot, 0);
+       }
+    } while (strcmp(mot, "stop"));
+}
+
+/****************************************************************************/