open Printf
open List

let rec ler_floats n acc = 
  if n >= 0 then ler_floats (n-1) ((read_float ()) :: acc) else List.rev acc

let grau = let () = print_string "grau : "  in read_int () 
let coefs = let () = printf "polinĂ³mio P de grau %d : "  grau in ler_floats grau []
let x =  let () = print_string "valor de x : "  in read_float () 

let rec horner_list gr x lv acc =
    match lv with 
      [] -> acc
    | el::li  -> 
      horner_list gr x li (x *. acc +. el)


let () =
  let res = horner_list grau x coefs 0.0 in
  (printf "Com x = %f, P(x)=%.3f\n" x res)


let rec to_string grau = function 
    [] -> ""
  | [el] when el = 0. -> ""
  | [el] -> string_of_float el
  | el1::el2::li ->
    let inicio =
      if el1 = 0.  then ""
      else
        let coef = if el1 = 1. then "" else (string_of_float el1) in
        let gr = if grau = 0 then ""
                 else if grau = 1 then "x" else "x^"^(string_of_int grau) in
        coef^gr
    in
    let fim = if el2 = 0. then "" else " + " in
    inicio^fim^(to_string (grau-1) (el2::li))


let rec deriv_poli_aux n l acc = 
match l with 
  []  | [_]-> List.rev acc
| el::li -> let res = (el*.(float_of_int n)) in
            (deriv_poli_aux (n-1) li (res::acc))

let rec deriv_poli l = (deriv_poli_aux (length l - 1) l [])

let () =
  let der = (deriv_poli coefs) in
  print_endline ("Derivada em x: "^(to_string (length der - 1) der))

This document was generated using caml2html