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
| #include <stdio.h>
#include <stdlib.h>
#include "read.h"
#include <string.h>
#include <assert.h>
#define TRUE 1
#define FALSE 0
l_word create_word(char *mi){
l_word m;
m = malloc(sizeof(l_word));
m->mot = mi;
m->next = NULL;
return m;
}
l_word init_chaine(){
l_word p;
p = malloc(sizeof(t_word));
p->mot = NULL;
p->next = NULL;
return p;
}
liste init_liste(){
liste p;
p.first = NULL;
p.last = NULL;
p.qte = 0;
return p;
}
void maj_liste(liste p, l_word i){ // meta jour la liste
if(p.first==NULL){
p.first = i;
}
int f = p.qte;
f = f+1;
p.qte = f;
p.last = i;
}
l_word last_elt(l_word l){// renvoi le dernier elt de la liste
l_word p; l_word j;
p = malloc(sizeof(t_word));
j = malloc(sizeof(t_word));
p = l;
while(p != NULL){
j = p;
p = p->next;
}
return j;
}
l_word add_chain(liste lis,l_word li, l_word e){ // ajoute un mot dans une celule qui sera ajouter a la liste
l_word l;
l = malloc(sizeof(l_word));
l = li;
if(li == NULL){
l = e;
maj_liste(lis,li);
return li;
} else {
l = last_elt(li);
l->next = e;
maj_liste(lis,li);
return li;
}
}
char *tab_char(char c){ // inser un caracter dans un tableau
char *tab = malloc(28*sizeof(char));
int n = 0;
while(n<28){
tab[n]=c;
n++;
}
return tab;
}
int if_char(char c){ //renvoi vrai quand char = A-Z ou a-z
int r = TRUE;
if((c<65&&c>90)||(c<97&&c>122)){
r = FALSE;
}
return r;
}
l_word read(FILE * f, liste ll, l_word li) { // lit le fichier et charge les mot dans la liste l_word et met a jour liste ll
printf("on est dans read");
l_word l = li;
char c =' '; int n =0;
char *tab = malloc(28*sizeof(char));
if(f==NULL){
printf("il ya un souci avec le fichier");
return NULL;
}
while(c != EOF){
c = fgetc(f);
if((if_char(c)==FALSE)||(n>28)){
add_chain(ll, l, (create_word(tab)));
n = 0;
tab = "";
}
tab[n] = c;
n++;
}
return li;
}
void affich(l_word r){ // affiche le contenu de la liste l_word
l_word e = malloc(sizeof(t_word));
e = r; int t=1;
while(e!=NULL){
printf("\n pos:%d contien: %s\n",t,e->mot);
e=e->next;
t++;
}
if(e==NULL){
printf("\n fin de la liste \n");
}
}
int main(){
printf("on est dans main");
FILE* fic = NULL;
fic = fopen("test.txt","r");
assert(fic!=NULL);
printf("afic ");
liste li = init_liste();
l_word l = init_chaine();
read(fic,li,l);
affich(l);
fclose(fic);
return EXIT_SUCCESS;
} |
Partager