open Poly (* L'anneau des flottants... *) module Float = struct type t = float let zero = 0. let unit = 1. let symm x = -. x let plus = ( +. ) let mult = ( *. ) let equal = ( = ) let tostring = string_of_float end;; module P = Make (Float) (struct let name = "X" end);; (* construction d'un polynome: le coefficients en X^k est représenté par un flottant à l'indice k du tableau *) let poly t = Array.fold_left (fun r (s, k) -> P.plus (P.monome (float_of_string s) k) r) P.zero (Array.mapi (fun i x -> (x, i)) t) ;; (* il faut enlever le premier argument qui est le nom de la commande *) let suffix t k = Array.sub t k (Array.length t - k);; let main () = let echo, k = if Array.length Sys.argv > 1 && Sys.argv.(1) = "-v" then true, 2 else false, 1 in let t = suffix Sys.argv k in let p = poly t in let print_eval x = if echo then Printf.printf "P(%f) = " x; print_float (P.eval p x); print_newline() in if echo then Printf.printf "P = %s\n" (P.tostring p); while true do print_eval (float_of_string (read_line())) done ;; try main() with End_of_file -> ();;