Bonjour,
voici différents code.
Est ce que vous pourriez me dire si les allocations,les affectations sont bien faites,les erreurs à corriger.
Merci d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #define MAX 50 #define LIBRE 0 #define OCCUPE 1 enum Type{Constante,Operateur}; union Info { double cte; char op; } typedef struct noeud { enum Type type; union Info info; struct noeud*gauche; struct noeud*droit; }Noeud,*Arbre ; typedef struct { char *param ; Arbre arbreparam ; }TableFonction ; typedef struct { char *nom ; int nb_param ; TableFonction *fct ; Arbre a ; int etat ; }Symbole ; typedef struct { Symbole tab[MAX]. }Table ;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 void Init(Table *table) { unsigned int i; for(i=0; i<MAX; i++) { table->tab[i].nom=NULL; table->tab[i].nb_param=0; table->tab[i].fct=NULL; table->tab[i].a=NULL; table->tab[i].etat=LIBRE; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 int hash(const char *nom) { int som=0; int i; for(i=0; i<MAX; i++) som+=nom[i]; return som%MAX; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 /* Recherche la position d'un nom dans la table */ unsigned int RecherchePos(Table *table,const char *nom) { unsigned int pos ; for(pos = 0U; pos<Env->taille; ++pos) if(Env->tab[pos].nom != NULL && (!(strcmp(nom,Env->ta[pos].nom)))) return pos; return MAX; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* Ajout d'une fonction dans la table des symboles */ int AddFonction(Table *table,const char *nom, int nb_parametres, char **tabparam, Arbre a) { unsigned pos = RecherchePos(table,nom); if(pos != MAX)/* deja present */ return 1; unsigned int indice = hash(nom); unsigned int marque = indice; if(table->tab[indice].etat == OCCUPE) { indice ++; while(table->tab[indice].etat ==OCCUPE && indice != marque) { if(indice < MAX) indice ++; else indice -=MAX; } } if(indice == marque) { fprintf(stderr,"la table est remplit\n"); return 1 ; } table->tab[indice].nom = (char*)malloc(strlen(nom)+1); if(table->tab[indice].nom == NULL) { fprintf(stderr,"erreur allocation\n"); return 1 ; } strcpy(table->tab[indice].nom,nom); table->tab[indice].nb_parametres = nb_parametres; int i ; table->tab[indice].fct = (TableFonction*)malloc(sizeof(TableFonction)*nb_param); if(table->tab[indice].fct == NULL) return 1; for(i=0; i<nb_param; i++) { table->tab[indice].fct[i].nom = malloc(strlen(tabparam[i])+1); if(table->tab[indice].fct[i].nom == NULL) return 1; strcpy(table->tab[indice].fct[i].nom,tabparam[i]); table->tab[indice].fct[i].arbreparam = NULL; } table->tab[indice].a = a; table->tab[indice].etat = OCCUPE; return 0; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 int RechercheFonction(Table *table,const char *nom,int *nb_param,char **tabparam,Arbre *a) { unsigned pos = RecherchePos(Env,nom); if(pos == MAX)/* pas trouve */ return 1 ; *nb_param = table->tab[indice].nb_param; tabparam = malloc(sizeof(*tabparam)*(*nb_param)); if(tabparam==NULL) return 1; int i; for(i=0; i<(*nb_param); i++) { tabparam[i] = malloc(sizeof(**tabparam)*(strlen(table->tab[pos].fct[i].nom)+1)); if(tabparam[i] == NULL) return 1; strcpy(tabparam[i],table->tab[pos].fct[i].nom); } *a = table->tab[pos].a; return 0; }
Partager