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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #include <stdio.h>
#include <float.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
enum Type {Operateur, Constante, Variable};
union Info
{
double cte;
char *nom;
char op;
};
struct noeud{
enum Type type;
union Info info;
struct noeud *gauche;
struct noeud *droit;
};
typedef struct noeud Noeud;
typedef struct noeud * Expression;
typedef struct _Symbole{
int type;
char *nom;
double val;
int nb_parametres;
char *parametres;
Expression e;
} Symbole;
typedef struct _Environnement{
unsigned taille;
Symbole tab[MAX];
} Environnement;
/* Initialisation de la table d'environnement */
void InitEnvironnement(Environnement *Env)
{
Env->tab[0].nom = "MIN";
Env->tab[0].val = DBL_MIN;
Env->tab[1].nom = "MAX" ;
Env->tab[1].val = DBL_MAX;
Env->taille = 2U;
}
/* Recherche la position d'un nom dans la table */
unsigned int RecherchePos(Environnement *Env,const char *nom)
{
unsigned pos ;
for(pos = 0U; pos<Env->taille; ++pos)
if(Env->tab[pos].nom != NULL && (!(strcmp(nom,Env->tab[pos].nom))))
return pos;
return MAX;
}
/* Recherche si une variable appartient à la table et donne sa valeur
si c'est le cas */
int RechercheVariable(Environnement *Env,const char *nom, int *type,
double *valeur)
{
unsigned pos = RecherchePos(Env,nom);
/* la variable n'a pas ete trouvee */
if (pos == MAX) return 1;
/* trouvee */
*type = Env->tab[pos].type;
*valeur = Env->tab[pos].val;
return 0;
}
/* Recherche si une fonction appartient à la table et donne son nombre
de parametres, ses parametres et sa definition si c'est le cas */
int RechercheFonction(Environnement *Env,const char *nom, int *type,
int *nb_parametres, char **parametres, Expression *e)
{
unsigned pos = RecherchePos(Env,nom);
/* la variable n'a pas ete trouvee */
if (pos == MAX) return 1;
/* trouvee */
*type = Env->tab[pos].type;
*nb_parametres = Env->tab[pos].nb_parametres;
*parametres = Env->tab[pos].parametres;
*e = Env->tab[pos].e;
return 0;
}
/* Ajout d'une variable dans la table des symboles */
int AddVariable(Environnement *Env,const char *nom, int type, double
val)
{
unsigned pos = RecherchePos(Env,nom);
if(pos == MAX)/* pas trouve */
pos = Env->taille;
Env->tab[pos].nom = (char*)malloc(strlen(nom)+1);
if(Env->tab[pos].nom == NULL)
{
fprintf(stderr,"erreur allocation\n");
return 1 ;
}
strcpy(Env->tab[pos].nom,nom);
Env->tab[pos].type = type;
Env->tab[pos].val = val;
return 0;
}
/* Ajout d'une fonction dans la table des symboles */
int AddFonction(Environnement *Env,const char *nom, int type, int
nb_parametres, const char *parametres, Expression e)
{
unsigned pos = RecherchePos(Env,nom);
if(pos==MAX)/* pas trouve */
pos=Env->taille;
Env->tab[pos].nom = (char*)malloc(strlen(nom)+1);
if(Env->tab[pos].nom == NULL)
{
fprintf(stderr,"erreur allocation\n");
return 1 ;
}
strcpy(Env->tab[pos].nom,nom);
Env->tab[pos].type = type;
Env->tab[pos].nb_parametres = nb_parametres;
Env->tab[pos].parametres = (char*)malloc(strlen(parametres)+1);
if(Env->tab[pos].parametres == NULL)
{
fprintf(stderr,"erreur allocation\n");
return 1 ;
}
strcpy(Env->tab[pos].parametres,parametres);
Env->tab[pos].e = e;
return 0;
}
void SuppEnv(Environnement *Env)
{
unsigned pos;
for(pos = 0; pos<Env->taille; pos++)
free(Env->tab[pos].nom);
}
int main(void)
{
int i;
Environnement env;
InitEnvironnement(&env);
AddVariable(&env,"test",0,125.58);
for(i=0;i<env.taille;i++)
{
printf("%s = % lf",env.tab[i].nom,env.tab[i].val);
printf("\n");
}
return 0;
} |
Partager