Commit 4e905f5267f7563af17a35f226d1f86aba20e705
Merge branch 'master' of https://archives.plil.fr/pvernier/ima3_projet_pa_2019
Showing
3 changed files
with
68 additions
and
74 deletions
Show diff stats
projet0segfault.c
1 | 1 | #include <stdio.h> |
2 | 2 | #include <stdlib.h> |
3 | 3 | |
4 | -#define A 26 | |
4 | +#define A 27 | |
5 | 5 | |
6 | 6 | struct node { |
7 | 7 | char lettre; |
... | ... | @@ -13,111 +13,102 @@ struct cell { |
13 | 13 | struct cell* arbreSuivant; |
14 | 14 | }; |
15 | 15 | |
16 | +void lien_listeFils(struct cell** pL) { | |
17 | + struct cell* p; | |
18 | + p = malloc(sizeof(struct cell)); | |
19 | + | |
20 | + (*pL)->arbre->listeFils = p; | |
21 | +} | |
22 | + | |
16 | 23 | void initialisation_tab_arbre(struct node tab[]) { |
17 | - | |
18 | - for(int i = 0; i < A; i++) { | |
24 | + for(int i = 0; i < A-1; i++) { | |
19 | 25 | tab[i].lettre = 97+i; //ajout lettres minuscules |
20 | 26 | tab[i].listeFils = NULL; |
21 | 27 | } |
22 | - | |
23 | - tab[A].lettre = 39; | |
24 | - tab[A].listeFils = NULL; | |
25 | - | |
28 | + tab[A-1].lettre = 39; | |
29 | + tab[A-1].listeFils = NULL; | |
26 | 30 | } |
27 | 31 | |
28 | 32 | void ajout_tete(char elem, struct cell** pL) { |
29 | 33 | struct cell* p; |
30 | - | |
31 | 34 | p = malloc(sizeof(struct cell)); |
32 | - //p->arbre = malloc(sizeof(struct node)); | |
35 | + p->arbre = malloc(sizeof(struct node)); | |
33 | 36 | p->arbre->lettre = elem; |
34 | 37 | p->arbreSuivant = *pL; |
35 | 38 | *pL = p; |
36 | 39 | } |
37 | 40 | |
38 | -void lien_listeFils(struct cell** pL) { | |
39 | - struct cell* p; | |
40 | - p = malloc(sizeof(struct cell)); | |
41 | - | |
42 | - (*pL)->arbre->listeFils = p; | |
43 | -} | |
44 | - | |
45 | -struct cell* insertion(char elem, struct cell** pL) { | |
46 | - printf("insert\n"); | |
47 | - printf("*pL : %p\n", *pL); | |
48 | - printf("(*pL)->arbre : %p\n", &(*pL)->arbre); | |
49 | - if((*pL == NULL)||((*pL)->arbre->lettre > elem)) { | |
50 | - printf("condition1\n"); | |
51 | - //lien_listeFils(pL); | |
52 | - ajout_tete(elem, pL); | |
53 | - printf("return insertion : %p\n", (*pL)->arbre->listeFils); | |
54 | - return (*pL)->arbre->listeFils; | |
55 | - } | |
56 | - else if((*pL)->arbre->lettre == elem) { printf("condition2\n"); return (*pL)->arbre->listeFils;} | |
57 | - else {printf("esle\n"); insertion(elem, &(*pL)->arbreSuivant); } | |
58 | - | |
41 | +struct cell * insertion(char elem, struct cell** pL) { | |
42 | + if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) { | |
43 | + ajout_tete(elem, pL); | |
44 | + return (*pL); | |
45 | + } | |
46 | + else if((*pL)->arbre->lettre == elem) { | |
47 | + return (*pL); | |
48 | + } | |
49 | + else return insertion(elem, &(*pL)->arbreSuivant); | |
59 | 50 | } |
60 | 51 | |
61 | -/*void affiche_tab(struct node tab[]) { | |
62 | - for(int i = 0; i < 32; i++) { | |
63 | - printf("%c\n", tab[i].lettre); | |
64 | - } | |
65 | - }*/ | |
66 | 52 | |
67 | 53 | void lire_fichier(FILE* fd, struct node tab_arbre_prcp[]) { |
68 | - printf("entrer lire_fichier\n"); | |
54 | + | |
55 | + printf("lirefichier\n"); | |
69 | 56 | struct cell* localisationArbre; |
70 | 57 | char motLu[50]; |
71 | - int i = 0; | |
72 | - printf("addressentré fd: %p\n", fd ); | |
73 | - if(fd!=NULL) | |
74 | - { | |
75 | - printf("fd!=NULL\n" ); | |
76 | - while(fscanf(fd, "%s", motLu)==1) | |
77 | - { | |
78 | - printf("entrer while\n"); | |
79 | - if((motLu[i] >= 'a') && (motLu[i] <= 'z')) localisationArbre = tab_arbre_prcp[motLu[0]-97].listeFils; | |
80 | - | |
81 | - if(motLu[i] == 39) localisationArbre = tab_arbre_prcp[A].listeFils; //A = derniere case du tab | |
58 | + | |
59 | + while(fscanf(fd, "%s", motLu)==1) { | |
60 | + int i = 0; | |
61 | + printf("while lire fichier\n"); | |
62 | + if((motLu[i] >= 'a') && (motLu[i] <= 'z')) { | |
63 | + | |
64 | + printf("if((motLu[i] >= 'a') && (motLu[i] <= 'z'))\n"); | |
65 | + localisationArbre = tab_arbre_prcp[motLu[0]-97].listeFils; | |
66 | + } | |
67 | + | |
68 | + if(motLu[i] == 39) { | |
69 | + | |
70 | + printf("if(motLu[i] == 39)"); | |
71 | + localisationArbre = tab_arbre_prcp[A].listeFils; //A = derniere case du tab | |
72 | + } | |
82 | 73 | |
83 | - printf("avant while : localisation : %p\n", localisationArbre); | |
84 | - while(motLu[i] != '\0') | |
85 | - { | |
86 | - i += 1; | |
87 | - printf("lettre lue : %c address : %p\n", motLu[i], localisationArbre); | |
88 | - localisationArbre = insertion(motLu[i], &localisationArbre); | |
89 | - printf("localisation apres : %p\n", localisationArbre); | |
90 | - printf("\n"); | |
91 | - } | |
92 | - printf("\n"); | |
93 | - } | |
94 | - fclose(fd); | |
95 | - printf("fin lire fichier\n"); | |
96 | - } | |
74 | + printf("avant while : localisation : %p\n", localisationArbre); | |
75 | + while(motLu[i] != '\0') { | |
76 | + | |
77 | + i += 1; | |
78 | + printf("lettre lue : %c address : %p\n", motLu[i], localisationArbre); | |
79 | + localisationArbre = insertion(motLu[i], &localisationArbre); | |
80 | + printf("localisation apres : %p\n", localisationArbre); | |
81 | + printf("\n"); | |
82 | + } | |
83 | + } | |
84 | + printf("\n"); | |
85 | + fclose(fd); | |
86 | + | |
87 | + printf("fin lire fichier\n"); | |
88 | + | |
97 | 89 | } |
98 | 90 | |
99 | 91 | |
100 | 92 | int main(int argc, char* argv[]) { |
101 | 93 | FILE* fd; |
102 | - fd=NULL; | |
103 | - | |
104 | - struct node tab_arbre[A+1]; | |
105 | - //struct node Arbre; | |
106 | - //char lettre; | |
107 | - printf("etape1\n"); | |
108 | - | |
109 | - if(argc>1) fd = fopen(argv[1], "r"); | |
94 | + struct node tab_arbre[A]; | |
110 | 95 | |
111 | - printf("etape2\n"); | |
112 | - initialisation_tab_arbre(tab_arbre); | |
96 | + if(argc>1) fd = fopen(argv[1], "r"); | |
97 | + else fd = NULL; | |
98 | + if (fd == NULL) { | |
99 | + printf("Error : couldn't open file\n"); | |
100 | + return 1; | |
101 | + } | |
102 | + | |
113 | 103 | |
114 | - printf("tab_arbre[0].listeFils: %p \n", tab_arbre[0].listeFils); | |
104 | + initialisation_tab_arbre(tab_arbre); | |
115 | 105 | |
116 | - | |
106 | + printf("avant lire fichier\n"); | |
117 | 107 | lire_fichier(fd, tab_arbre); |
118 | - printf("tab_arbre[0].listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->lettre); | |
108 | + //printf("tab_arbre[0].listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->lettre); | |
119 | 109 | //scanf("%c", &lettre); |
120 | 110 | //insertion(lettre, &(Arbre.listeFils)); |
121 | 111 | //printf("lettre : %c\n", Arbre.listeFils->arbre->lettre); |
112 | + printf("avant return 0\n"); | |
122 | 113 | return 0; |
123 | 114 | } | ... | ... |
No preview for this file type