(*********** TYPE ET DONNNES *********)
type arbre = VIDE | F of char * int | N of char * int * arbre * arbre;;
let foi=float_of_int;;
let ioc=int_of_char;;
(*********** FONCTIONS AUXILIAIRES *********)
(* a: caractère p:poids *)
let rec insere a p arb = match arb with
VIDE -> F (a, p)
| F (b, pb) -> if p > pb then N (b, pb, VIDE, F (a, p))
else N (a, p, arb, VIDE)
| N (b, pb, g, d) -> if p <= pb then N (a, p, arb, VIDE)
else N (b, pb, g, insere a p d)
;;
let reel_de_chiffre t = foi (ioc t - 48);;
(******************* FONCTIONS ****************)
let rec etat_init l arb = match l with
[] -> arb
| t :: q -> match t with
`+` | `-` -> etat_op q (insere t 0 arb)
| `0`..`9` -> etat_chiffre q (insere t 2 arb)
| _ -> VIDE
and etat_chiffre l arb = match l with
[] -> arb
| t :: q -> match t with
`+` | `-` -> etat_op q (insere t 0 arb)
| `*` | `/` -> etat_op q (insere t 1 arb)
| _ -> VIDE
and etat_op l arb = match l with
[] -> arb
| t :: q -> match t with
`0`..`9` -> etat_chiffre q (insere t 2 arb)
| _ -> VIDE
;;
let traite l = etat_init l VIDE;;
let rec calcul arb = match arb with
VIDE -> 0.
| F (a, p) -> (reel_de_chiffre a)
| N (a, p, g, d) -> if (ioc a) = 43 then (calcul g) +. (calcul d)
else if (ioc a) = 45 then (calcul g) -. (calcul d)
else if (ioc a) = 42 then (calcul g) *. (calcul d)
else (calcul g) /. (calcul d)
;;
C'est beau du Caml 