Blame view

projet0segfault.c 2.56 KB
893dbc98   tvieuble   update projet0seg...
1
2
3
  #include <stdio.h>
  #include <stdlib.h>
  
97749cb4   tvieuble   Correction segfault
4
  #define A 27
893dbc98   tvieuble   update projet0seg...
5
6
7
8
9
10
11
12
13
14
15
  
  struct node {
    char lettre;
    struct cell* listeFils;
  };
  
  struct cell {
    struct node* arbre;
    struct cell* arbreSuivant;
  };
  
97749cb4   tvieuble   Correction segfault
16
17
18
19
20
21
22
  void lien_listeFils(struct cell** pL) {
    struct cell* p;
    p = malloc(sizeof(struct cell));
   
    (*pL)->arbre->listeFils = p;
  }
  
893dbc98   tvieuble   update projet0seg...
23
  void initialisation_tab_arbre(struct node tab[]) {
97749cb4   tvieuble   Correction segfault
24
    for(int i = 0; i < A-1; i++) {
6c8b6d76   pvernier   le debut des prob...
25
26
      tab[i].lettre = 97+i; //ajout lettres minuscules
      tab[i].listeFils = NULL;
893dbc98   tvieuble   update projet0seg...
27
    }
97749cb4   tvieuble   Correction segfault
28
29
    tab[A-1].lettre = 39;
    tab[A-1].listeFils = NULL;
893dbc98   tvieuble   update projet0seg...
30
31
32
33
  }
  
  void ajout_tete(char elem, struct cell** pL) {
    struct cell* p;
893dbc98   tvieuble   update projet0seg...
34
    p = malloc(sizeof(struct cell));
893dbc98   tvieuble   update projet0seg...
35
    p->arbre = malloc(sizeof(struct node));
893dbc98   tvieuble   update projet0seg...
36
37
38
39
40
    p->arbre->lettre = elem;
    p->arbreSuivant = *pL;
    *pL = p;
  }
  
4e905f52   pvernier   Merge branch 'mas...
41
  struct cell * insertion(char elem, struct cell** pL) {
97749cb4   tvieuble   Correction segfault
42
43
  	if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) {
  		ajout_tete(elem, pL);
4e905f52   pvernier   Merge branch 'mas...
44
  		return (*pL);
97749cb4   tvieuble   Correction segfault
45
46
  	}
  	else if((*pL)->arbre->lettre == elem) {
4e905f52   pvernier   Merge branch 'mas...
47
  		return (*pL);
97749cb4   tvieuble   Correction segfault
48
  	}
4e905f52   pvernier   Merge branch 'mas...
49
  	else return insertion(elem, &(*pL)->arbreSuivant);
893dbc98   tvieuble   update projet0seg...
50
51
  }
  
893dbc98   tvieuble   update projet0seg...
52
53
  
  void lire_fichier(FILE* fd, struct node tab_arbre_prcp[]) {
97749cb4   tvieuble   Correction segfault
54
55
  
    printf("lirefichier\n");
893dbc98   tvieuble   update projet0seg...
56
57
    struct cell* localisationArbre;
    char motLu[50];
4e905f52   pvernier   Merge branch 'mas...
58
    
97749cb4   tvieuble   Correction segfault
59
  		while(fscanf(fd, "%s", motLu)==1) {
4e905f52   pvernier   Merge branch 'mas...
60
61
62
63
64
65
        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;
97749cb4   tvieuble   Correction segfault
66
67
  			}
  					  	
4e905f52   pvernier   Merge branch 'mas...
68
69
70
71
  			if(motLu[i] == 39) {
  		
  		 		printf("if(motLu[i] == 39)");
  				localisationArbre = tab_arbre_prcp[A].listeFils; //A = derniere case du tab
97749cb4   tvieuble   Correction segfault
72
  			}
893dbc98   tvieuble   update projet0seg...
73
  
4e905f52   pvernier   Merge branch 'mas...
74
  			printf("avant while : localisation : %p\n", localisationArbre);
97749cb4   tvieuble   Correction segfault
75
76
77
78
  			while(motLu[i] != '\0') {
  					      
  		    	i += 1;
  				printf("lettre lue : %c address : %p\n", motLu[i], localisationArbre);
4e905f52   pvernier   Merge branch 'mas...
79
80
  				localisationArbre = insertion(motLu[i], &localisationArbre);
  				printf("localisation apres : %p\n", localisationArbre);
97749cb4   tvieuble   Correction segfault
81
82
83
84
85
  				printf("\n");
  			}
  		}
  		printf("\n");
  		fclose(fd);
893dbc98   tvieuble   update projet0seg...
86
  
97749cb4   tvieuble   Correction segfault
87
88
  	    printf("fin lire fichier\n");
  	
893dbc98   tvieuble   update projet0seg...
89
90
91
92
93
  }
  
  
  int main(int argc, char* argv[]) {
    FILE* fd;
893dbc98   tvieuble   update projet0seg...
94
    struct node tab_arbre[A];
893dbc98   tvieuble   update projet0seg...
95
    
97749cb4   tvieuble   Correction segfault
96
97
98
99
100
101
    if(argc>1) fd = fopen(argv[1], "r");
    else fd = NULL;
    if (fd == NULL) {
          printf("Error : couldn't open file\n");
          return 1;
      }
4e905f52   pvernier   Merge branch 'mas...
102
  
6c8b6d76   pvernier   le debut des prob...
103
    
4e905f52   pvernier   Merge branch 'mas...
104
    initialisation_tab_arbre(tab_arbre);  
6c8b6d76   pvernier   le debut des prob...
105
    
97749cb4   tvieuble   Correction segfault
106
    printf("avant lire fichier\n");
893dbc98   tvieuble   update projet0seg...
107
    lire_fichier(fd, tab_arbre);
4e905f52   pvernier   Merge branch 'mas...
108
    //printf("tab_arbre[0].listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->lettre);
893dbc98   tvieuble   update projet0seg...
109
110
111
    //scanf("%c", &lettre);
    //insertion(lettre, &(Arbre.listeFils));
    //printf("lettre : %c\n", Arbre.listeFils->arbre->lettre);
4e905f52   pvernier   Merge branch 'mas...
112
    printf("avant return 0\n");
893dbc98   tvieuble   update projet0seg...
113
114
    return 0;
  }