| 12
 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
 
 | /* programme écrit par Antoine Depreux
derniere version : 07/11/2010 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
 
#define TAILLE_MAX 50
 
int masque(char *motif, char *string);
 
int main(int argc, char *argv[]) {
 
    FILE * fichier = fopen(argv[1],"r");
 
    char chaine[TAILLE_MAX];
    char * Tab1[TAILLE_MAX];
    char motif[TAILLE_MAX];
    char string[TAILLE_MAX];
    char ** Tab2;
 
    int i=0, j, Compteur;
    int reponse=1;
 
    // erreur a l'ouverture
    if (fichier == NULL) {
        printf("Erreur a l'ouverture. Utilisation : entrer en argument le nom du fichier\n");
        return 1;
    }
 
    // lecture du fichier et copie dans Tab
    if (fichier != NULL) {
        while (fscanf(fichier,"%s", chaine) != EOF)  { // On lit le fichier tant qu'on ne recoit pas d'erreur
            Tab1[i]= (char *)malloc(sizeof(char)*(strlen(chaine)+1)); // allocation de memoire
            strcpy(Tab1[i], chaine); // copie du mot lu
            i++;
        }
 
        Compteur = i; // on recupere le nombre de mots total
 
        Tab2=malloc(Compteur); // allocation de memoire
 
        // copie dans le tableau de taille Compteur
        for(j=0; j<Compteur; j++) {
            Tab2[j]=(char *)malloc(sizeof(char)*(strlen(Tab1[j])+1));
            strcpy(Tab2[j], Tab1[j]);
        }
 
        free(Tab1);
        fclose(fichier);
    }
 
    // lectur du motif et appel a la fonction masque
    while(reponse==1) {
        printf("Entrez votre motif de recherche : "); // On demande le motif a l'utilisateur
        scanf("%s", motif);
 
        for (i=0; i<Compteur; i++) { // On affiche le mot s'il correspond au motif
            if(masque(motif, Tab2[i])==1) {
                printf("%s\n", Tab2[i]);
            }
        }
 
        printf("Recherche terminee.\nNouvelle recherche ? (1:oui, 0:non) ");
        scanf("%d",&reponse);
    }
 
}
 
// fonction masque qui retourne 1 si motif et string correspondent, 0 sinon
int masque(char *motif, char *string) {
 
    //printf("Appel de masque(%s, %s)\n", motif, string);
 
    char *cc, *mc;
 
    while ((*string) && (*motif != '*')) { // si le motif ne vaut pas * : on passe au caractere suivant
        if ((*motif != *string) && (*motif != '$')) return 0; // si le motif n'est pas bon et ne vaut pas $ : masque renvoie 0
 
        motif++;
        string++;
    }
 
    while (*string) {
        if (*motif == '*') { // le motif vaut * : on range dans mc le motif et dans cc le caractere suivant
            if (*(++motif) == 0) return 1; // on arrete le processus a la fin du motif
 
            cc = string+1;
            mc = motif;
        }
 
        else if ((*motif == *string) || (*motif == '$')) { // le motif est bon ou vaut $ : on compare le caractere suivant au motif suivant
            string++;
            motif++;
        }
 
        else { // le motif n'est pas bon, et ne vaut pas * ou $
            string = cc++;
            motif = mc;
        }
    }
 
    while (*motif == '*') { // si le motif est un *, on passe au motif suivant
        motif++;
    }
 
    if (*motif == 1) return 0; // on s'arrete avant la fin : masque renvoie 0
    else return 1;
} |