projet0segfault.c
2.56 KB
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
#include <stdio.h>
#include <stdlib.h>
#define A 27
struct node {
char lettre;
struct cell* listeFils;
};
struct cell {
struct node* arbre;
struct cell* arbreSuivant;
};
void lien_listeFils(struct cell** pL) {
struct cell* p;
p = malloc(sizeof(struct cell));
(*pL)->arbre->listeFils = p;
}
void initialisation_tab_arbre(struct node tab[]) {
for(int i = 0; i < A-1; i++) {
tab[i].lettre = 97+i; //ajout lettres minuscules
tab[i].listeFils = NULL;
}
tab[A-1].lettre = 39;
tab[A-1].listeFils = NULL;
}
void ajout_tete(char elem, struct cell** pL) {
struct cell* p;
p = malloc(sizeof(struct cell));
p->arbre = malloc(sizeof(struct node));
p->arbre->lettre = elem;
p->arbreSuivant = *pL;
*pL = p;
}
struct cell * insertion(char elem, struct cell** pL) {
if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) {
ajout_tete(elem, pL);
return (*pL);
}
else if((*pL)->arbre->lettre == elem) {
return (*pL);
}
else return insertion(elem, &(*pL)->arbreSuivant);
}
void lire_fichier(FILE* fd, struct node tab_arbre_prcp[]) {
printf("lirefichier\n");
struct cell* localisationArbre;
char motLu[50];
while(fscanf(fd, "%s", motLu)==1) {
int i = 0;
printf("while lire fichier\n");
if((motLu[i] >= 'a') && (motLu[i] <= 'z')) {
printf("if((motLu[i] >= 'a') && (motLu[i] <= 'z'))\n");
localisationArbre = tab_arbre_prcp[motLu[0]-97].listeFils;
}
if(motLu[i] == 39) {
printf("if(motLu[i] == 39)");
localisationArbre = tab_arbre_prcp[A].listeFils; //A = derniere case du tab
}
printf("avant while : localisation : %p\n", localisationArbre);
while(motLu[i] != '\0') {
i += 1;
printf("lettre lue : %c address : %p\n", motLu[i], localisationArbre);
localisationArbre = insertion(motLu[i], &localisationArbre);
printf("localisation apres : %p\n", localisationArbre);
printf("\n");
}
}
printf("\n");
fclose(fd);
printf("fin lire fichier\n");
}
int main(int argc, char* argv[]) {
FILE* fd;
struct node tab_arbre[A];
if(argc>1) fd = fopen(argv[1], "r");
else fd = NULL;
if (fd == NULL) {
printf("Error : couldn't open file\n");
return 1;
}
initialisation_tab_arbre(tab_arbre);
printf("avant lire fichier\n");
lire_fichier(fd, tab_arbre);
//printf("tab_arbre[0].listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->lettre);
//scanf("%c", &lettre);
//insertion(lettre, &(Arbre.listeFils));
//printf("lettre : %c\n", Arbre.listeFils->arbre->lettre);
printf("avant return 0\n");
return 0;
}