Commit f05507c0b4106f509f7f6a388faa5892fab01c65

Authored by Corto Callerisa
1 parent ad6428a9

ajout du fichier main, correcteur.c

Showing 1 changed file with 156 additions and 0 deletions   Show diff stats
correcteur.c 0 → 100644
... ... @@ -0,0 +1,156 @@
  1 +/**
  2 + * correcteur.c
  3 + *
  4 + *
  5 + */
  6 +
  7 +#include <ctype.h>
  8 +
  9 +#include <stdio.h>
  10 +#include <stdbool.h>
  11 +// Permet d'utiliser EXIT_FAILURE et non exit(1)
  12 +// EXIT_FAILURE est plus portable
  13 +#include <stdlib.h>
  14 +
  15 +#include "dictionnaire.h"
  16 +
  17 +// Choix du dictionnaire par default
  18 +#define DICTIONNAIRE_DEF "words"
  19 +
  20 +int main(int argc, char* argv[])
  21 +{
  22 + // Verification du nombre d'arguments
  23 + if (argc != 2 && argc != 3)
  24 + {
  25 + printf("Utilisation: correcteur [fichier_dictionnaire] texte\n");
  26 + return EXIT_FAILURE;
  27 + }
  28 +
  29 + // Determine le dictionnaire à utiliser
  30 + char* nom_dict = (argc == 3) ? argv[1] : DICTIONNAIRE_DEF;
  31 +
  32 + // Charge le dictionnaire
  33 + bool est_charge = importer_dict(nom_dict);
  34 +
  35 +
  36 + // Verification de l'importation du dictionnaire
  37 + if (!est_charge)
  38 + {
  39 + printf("Erreur lors de l'importation du dictionnaire %s.\n", nom_dict);
  40 + return EXIT_FAILURE;
  41 + }
  42 +
  43 +
  44 + // Ouverture du texte
  45 + char* text = (argc == 3) ? argv[2] : argv[1];
  46 + FILE* fp = fopen(text, "r");
  47 + // Vérifie si l'ouverture est possible
  48 + if (fp == NULL)
  49 + {
  50 + printf("Erreur lors de l'ouverture du fichier %s.\n", text);
  51 + decharger();
  52 + return 1;
  53 + }
  54 +
  55 + printf("\nMots malorthographiés\n\n");
  56 + int nb_malorthographie;
  57 + int nb_mots;
  58 + print_erreurs(fp, &nb_malorthographie, &nb_mots);
  59 +
  60 + // Vérifie que la lecture s'est terminée sans erreurs
  61 + if (ferror(fp))
  62 + {
  63 + fclose(fp);
  64 + printf("Erreur lors de la lecture du fichier %s.\n", text);
  65 + decharger();
  66 + return 1;
  67 + }
  68 +
  69 + // Fermeture du fichier
  70 + fclose(fp);
  71 +
  72 +
  73 +
  74 + if (!decharger())
  75 + {
  76 + printf("Erreur lors de la liberation memoire%s.\n", nom_dict);
  77 + return 1;
  78 + }
  79 +
  80 + // On affiche les informations sur les mots
  81 + printf("\nMots malorthographiés: %d\n", nb_malorthographie);
  82 + printf("Mots dans le dictionnaire: %d\n", taille_dic());
  83 + printf("Mots dans le texte: %d\n", nb_mots);
  84 +
  85 + return 0;
  86 +}
  87 +
  88 +void print_erreurs(FILE *fp, int *malorthographies, int *mots)
  89 +{
  90 + (*malorthographies) = 0;
  91 + (*mots) = 0;// initialisation des variables
  92 + int index = 0;
  93 + char mot[LONG_MAX+1];
  94 +
  95 + // Verification de l'orthographe
  96 + int nb_mots_ligne = 0;
  97 + for (int c = fgetc(fp); c != EOF; c = fgetc(fp))
  98 + {
  99 + // On ne traite que les caracteres et les apostrophes
  100 + if (isalpha(c) || (c == '\'' && index > 0))
  101 + {
  102 + // Ajout du caractere dans le mot temporaire en construction
  103 + mot[index] = c;
  104 + index++;
  105 +
  106 + // On annule la verification du mot s'il depasse la limite
  107 + if (index > LONG_MAX)
  108 + {
  109 + // On passe le reste du mot
  110 + while ((c = fgetc(fp)) != EOF && isalpha(c));
  111 +
  112 + // Reinitialisation du mot temporaire
  113 + index = 0;
  114 + }
  115 + }
  116 +
  117 + // On ignore les mots avec des chiffres
  118 + else if (isdigit(c))
  119 + {
  120 + // On passe le reste du mot
  121 + while ((c = fgetc(fp)) != EOF && isalnum(c));
  122 +
  123 + // Reinitialisation du mot temporaire
  124 + index = 0;
  125 + }
  126 +
  127 + // On prend un mot en entier
  128 + else if (index > 0)
  129 + {
  130 + // fin d'un mot
  131 + mot[index] = '\0';
  132 +
  133 + // On incremente le compteur
  134 + (*mots)++;
  135 +
  136 + // Vérifie si le mot est bien orthographie
  137 + bool malorthographie = !appartient(mot);
  138 +
  139 +
  140 + // On affiche les mots mal orthographies
  141 + if (malorthographie)
  142 + {
  143 + printf("%s\n", mot);
  144 + if (nb_mots_ligne == 2) {
  145 + printf("\n");
  146 + nb_mots_ligne = 0;
  147 + }
  148 + (*malorthographies)++;
  149 + }
  150 +
  151 + // On passe au mot suivant
  152 + index = 0;
  153 + }
  154 + }
  155 +}
  156 +
... ...