Commit 893dbc9885d10c9c600b31259552c7e38f6a6114
1 parent
8920d8f1
update projet0segfault.c
Showing
1 changed file
with
108 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,108 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | + | |
4 | +#define A 26 | |
5 | + | |
6 | +struct node { | |
7 | + char lettre; | |
8 | + struct cell* listeFils; | |
9 | +}; | |
10 | + | |
11 | +struct cell { | |
12 | + struct node* arbre; | |
13 | + struct cell* arbreSuivant; | |
14 | +}; | |
15 | + | |
16 | +void initialisation_tab_arbre(struct node tab[]) { | |
17 | + for(int i = 0; i < A; i++) { | |
18 | + tab[i].lettre = 97+i; //ajout lettres minuscules | |
19 | + } | |
20 | + tab[A].lettre = 39; | |
21 | + /*for(int i = 0; i < 8; i++) { | |
22 | + tab[i+26].lettre = 130+i; //ajout caractères spéciaux | |
23 | + }*/ | |
24 | +} | |
25 | + | |
26 | +void ajout_tete(char elem, struct cell** pL) { | |
27 | + struct cell* p; | |
28 | + p = malloc(sizeof(struct cell)); | |
29 | + p->arbre = malloc(sizeof(struct node)); | |
30 | + p->arbre->lettre = elem; | |
31 | + p->arbreSuivant = *pL; | |
32 | + *pL = p; | |
33 | +} | |
34 | + | |
35 | +void lien_listeFils(struct cell** pL) { | |
36 | + struct cell* p; | |
37 | + p = malloc(sizeof(struct cell)); | |
38 | + | |
39 | + (*pL)->arbre->listeFils = p; | |
40 | +} | |
41 | + | |
42 | +struct cell* insertion(char elem, struct cell** pL) { | |
43 | + printf("insert\n"); | |
44 | + printf("*pL : %d\n", *pL); | |
45 | + if((*pL == NULL)||((*pL)->arbre->lettre > elem)) { | |
46 | + printf("condition1\n"); | |
47 | + lien_listeFils(pL); | |
48 | + ajout_tete(elem, pL); | |
49 | + printf("return insertion : %d\n", (*pL)->arbre->listeFils); | |
50 | + return (*pL)->arbre->listeFils; | |
51 | + } | |
52 | + else if((*pL)->arbre->lettre == elem) { printf("condition2\n"); return (*pL)->arbre->listeFils;} | |
53 | + else {printf("esle\n"); insertion(elem, &(*pL)->arbreSuivant); } | |
54 | +} | |
55 | + | |
56 | +/*void affiche_tab(struct node tab[]) { | |
57 | + for(int i = 0; i < 32; i++) { | |
58 | + printf("%c\n", tab[i].lettre); | |
59 | + } | |
60 | + }*/ | |
61 | + | |
62 | +void lire_fichier(FILE* fd, struct node tab_arbre_prcp[]) { | |
63 | + struct cell* localisationArbre; | |
64 | + char motLu[50]; | |
65 | + int i = 0; | |
66 | + if(fd!=NULL) | |
67 | + { | |
68 | + while(fscanf(fd, "%s", motLu)==1) | |
69 | + { | |
70 | + if((motLu[i] >= 'a') && (motLu[i] <= 'z')) localisationArbre = tab_arbre_prcp[motLu[0]-97].listeFils; | |
71 | + | |
72 | + if(motLu[i] == 39) localisationArbre = tab_arbre_prcp[A].listeFils; //A = derniere case du tab | |
73 | + | |
74 | + printf("avant while : localisation : %d\n", localisationArbre); | |
75 | + while(motLu[i] != '\0') | |
76 | + { | |
77 | + i += 1; | |
78 | + printf("lettre lue : %c address : %d\n", motLu[i], localisationArbre); | |
79 | + localisationArbre = insertion(motLu[i], &localisationArbre); | |
80 | + printf("localisation apres : %d\n", localisationArbre); | |
81 | + printf("\n"); | |
82 | + } | |
83 | + printf("\n"); | |
84 | + } | |
85 | + fclose(fd); | |
86 | + printf("fin lire fichier\n"); | |
87 | + } | |
88 | +} | |
89 | + | |
90 | + | |
91 | +int main(int argc, char* argv[]) { | |
92 | + FILE* fd; | |
93 | + | |
94 | + struct node tab_arbre[A]; | |
95 | + struct node Arbre; | |
96 | + char lettre; | |
97 | + | |
98 | + if(argc>1) fd = fopen(argv[1], "r"); | |
99 | + | |
100 | + Arbre.listeFils = NULL; | |
101 | + initialisation_tab_arbre(tab_arbre); | |
102 | + lire_fichier(fd, tab_arbre); | |
103 | + printf("tab_arbre[0].listeFils->arbre->lettre : %c\n", tab_arbre[3].listeFils->arbre->lettre); | |
104 | + //scanf("%c", &lettre); | |
105 | + //insertion(lettre, &(Arbre.listeFils)); | |
106 | + //printf("lettre : %c\n", Arbre.listeFils->arbre->lettre); | |
107 | + return 0; | |
108 | +} | ... | ... |