Blame view

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