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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define lg_max 100
typedef struct noeud
{
char *mot;
int nb_occ;
int *numeroligne;
struct noeud *gauche;
struct noeud *droit;
} Noeud;
typedef Noeud *Adr_noeud;
Adr_noeud inser_arbre(Adr_noeud rac, char *un_mot, int *numero)
{
int comp;
if (rac==NULL)
{
rac = (Adr_noeud) malloc(sizeof(Noeud));
rac->mot = (char *) malloc(strlen(un_mot)+1);
rac->nb_occ = 1;
rac->numeroligne = 0;
strcpy(rac->mot,un_mot);
rac->gauche = rac->droit = NULL;
}
else if ((comp = strcmp(un_mot, rac->mot)) == 0)
{
rac->nb_occ++;
rac->numeroligne = numero; // je recupere ma ligne
}
else if (comp < 0) rac->gauche = inser_arbre(rac->gauche, un_mot);
else rac->droit = inser_arbre(rac->droit, un_mot);
return rac;
}
void arbre_vers_sortie(Adr_noeud rac)
{
if (rac != NULL)
{
arbre_vers_sortie(rac->gauche);
printf(" %s (%d fois)\n", rac->mot, rac->nb_occ);
arbre_vers_sortie( rac->droit);
}
}
int main(int argc, char *argv[])
{
char ligne[lg_max];
char sep[]= " \n'";
char *ptr;
int *numligne;
Noeud * rac;
rac = NULL;
FILE * file = NULL;
file = fopen(argv[1],"r");
while (fgets(ligne,lg_max,file))
{
ptr = strtok(ligne,sep);
while ( ptr != NULL)
{
printf("token %s\n",ptr);
rac = inser_arbre(rac,ptr,numligne); //puis je passer ma ligne en parametre ?
ptr = strtok(NULL, sep);
}
numl++;
}
fclose(file);
printf("\n");
arbre_vers_sortie(rac);
system("PAUSE");
return 0;
} |
Partager