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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#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 ;
static 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;
}
}
static int hash(const char *nom)
{
int som = 0;
int i;
for (i = 0; i < MAX; i++)
som += nom[i];
return som % MAX;
}
/* Recherche la position d'un nom dans la table */
static 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;
}
/* Ajout d'une fonction dans la table des symboles */
static 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;
}
static 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;
}
int main (void)
{
/* ajouter le code de test... */
return 0;
} |