Advertisement
yannick_degardin

Ocaml TD7

Nov 12th, 2020 (edited)
2,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 10.76 KB | None | 0 0
  1. (* TD7 exo 9*)
  2.  
  3. #use "APutil.ml";;
  4.  
  5. let simul(t : float) : int * float =
  6.     let nb : int ref = ref 0
  7.     and newt : float ref = ref t
  8.     and var : float ref = ref 0.0 in
  9.    
  10.     if t < 10.0 || t > 50.0
  11.     then failwith "erreur simul : taille initiale non conforme"
  12.     else
  13.     (
  14.         while (!newt < 2.0 *. t)
  15.         do
  16.             nb := !nb + 1 ;
  17.             var := 0.5 *. (float_of_int(rand_int(80, 120)) /. 100.0) ;
  18.             newt := !newt +. !var ;
  19.             if !nb mod 10 = 0
  20.             then
  21.                 (
  22.                 print_string("au siècle ") ;9
  23.                 print_int(!nb) ;
  24.                 print_string(", ");
  25.                 print_string("la nouvelle taille est ");
  26.                 print_float(!newt) ;
  27.                 print_string( "cm") ;
  28.                 print_newline()
  29.                 )
  30.             else ()
  31.         done ;
  32.     (!nb, !newt)
  33.     ) ;
  34. ;;
  35.  
  36. (* exo 10 *)
  37. let generate_point(n : int) : int * int =
  38.     (rand_int(-n, n-1), rand_int(-n, n-1))
  39. ;;
  40.  
  41. let is_inside_circle((x, y), n : (int * int) * int) : bool =
  42.     (x * x + y * y) < n * n
  43. ;;
  44.  
  45. let compute_pi(nb, n : int * int) : float =
  46.     let incircle : int ref = ref 0 in
  47.     (
  48.     for i = 1 to nb
  49.     do
  50.         if is_inside_circle(generate_point(n), n)
  51.         then incircle := !incircle + 1
  52.         else ()
  53.     done ;
  54.     4.0 *. (float_of_int(!incircle) /. float_of_int(nb))
  55.     )
  56. ;;
  57.  
  58. compute_pi(100000, 10000);;
  59.  
  60. (* exo 11 *)
  61. (* premiere version *)
  62. let get_answer() : char =
  63.     let answ : char ref = ref 'a' and thend : bool ref = ref false in
  64.     (
  65.     while not(!thend)
  66.     do
  67.         print_string("saisissez votre reponse : ") ;
  68.         answ := read_char() ;
  69.         if !answ <> '>' && !answ <> '<' && !answ <> '='
  70.         then
  71.             (
  72.             print_string("erreur de saisie ; merci de saisir <, > ou =") ;
  73.             print_newline()
  74.             )
  75.         else thend := true
  76.     done;
  77.     !answ
  78.     )
  79. ;;
  80.  
  81. let guess_number() : int =
  82.     let thend : bool ref = ref false
  83.     and min : int ref = ref 1
  84.     and max : int ref = ref 200
  85.     and answ : char ref = ref 'a'
  86.     and prop : int ref = ref 0
  87.     in
  88.     (
  89.     while not(!thend)
  90.     do
  91.         prop := rand_int(!min, !max) ;
  92.         print_string("valeur proposee : ") ;
  93.         print_int(!prop) ;
  94.         print_newline() ;
  95.         answ := get_answer() ;
  96.         if !answ = '<'
  97.         then max := !prop - 1
  98.         else
  99.             if !answ = '>'
  100.             then min := !prop + 1
  101.             else
  102.                 if !answ = '='
  103.                 then
  104.                 (
  105.                     thend := true ;
  106.                     print_string("merci") ;
  107.                     print_newline()
  108.                 )
  109.                 else ()
  110.     done ;
  111.     !prop
  112.     )
  113. ;;
  114. (* variante avec controle *)
  115. let guess_number() : int =
  116.     let thend : bool ref = ref false
  117.     and min : int ref = ref 1
  118.     and max : int ref = ref 200
  119.     and answ : char ref = ref 'a'
  120.     and prop : int ref = ref 0
  121.     in
  122.     (
  123.     while not(!thend)
  124.     do
  125.         if !min > !max
  126.         then
  127.         (
  128.             thend := true ;
  129.             prop := 0 ;
  130.             print_string("tricheur !") ;
  131.             print_newline()
  132.         )
  133.     else
  134. (* c'est inchange par rapport a la version precedente *)
  135. ;;
  136.  
  137. (*  -------------- exo de TP ---------------------*)
  138. (* ex12 factoriel *)
  139.  
  140. let fact(n : int) : int =
  141.   let res : int ref = ref 1 in
  142.   if n < 0
  143.   then failwith "erreur fact ; parametre negatif"
  144.   else
  145.     (
  146.       for i = 1 to n
  147.       do res := !res * i
  148.       done ;
  149.       !res
  150.     )
  151. ;;
  152.  
  153.  
  154.  
  155. (* ex13 triplet *)
  156.  
  157. let get_letter() : char =
  158.   let res : char ref = ref 'a' and thend : bool ref = ref false in
  159.   (
  160.     while not(!thend)
  161.     do
  162.       print_string("saisissez une lettre : ") ;
  163.       res := read_char() ;
  164.       thend := (!res >= 'a' && !res <= 'z’) || (!res >= 'A' && !res <= 'Z’)
  165.     done ;
  166.     !res
  167.   )
  168. ;;
  169.  
  170. let get_char(min, max : char * char) : char =
  171.   let res : char ref = ref 'a' and thend : bool ref = ref false in
  172.   (
  173.     while not(!thend)
  174.     do
  175.       print_string("saisissez un caractère compris entre ") ;
  176.       print_char(min) ;
  177.       print_string(" et ") ;
  178.       print_char(max) ;
  179.       print_string(" : ") ;
  180.       res := read_char() ;
  181.       thend := !res >= min && !res <= max
  182.     done ;
  183.     !res
  184.   )
  185. ;;
  186.  
  187. let get_code() : char * char * char =
  188.   let c1 : char = get_letter() and c2 : char = get_char(0’, '9)
  189.       and c3 : char ref = ref 'a' in
  190.   (
  191.     if (c1 >= 'a' && c1 <= 'z’)
  192.     then c3 := get_char(’A’, 'Z’)
  193.     else c3 := get_char(’a’, 'z’) ;
  194.     (c1, c2, !c3)
  195.   )
  196. ;;
  197.  
  198. (* ex14 *)
  199. let print_letter() : unit =
  200.   let res : char ref = ref 'a' in
  201.   (
  202.     for i = 0 to 25
  203.     do
  204.       print_char(char_of_int(int_of_char( !res) + i)) ;
  205.       print_char(' ')
  206.     done ;
  207.     print_newline() ;
  208.     res := 'A' ;
  209.     for i = 25 downto 0
  210.     do
  211.       print_char(char_of_int(int_of_char( !res) + i)) ;
  212.       print_char(' ')
  213.     done ;
  214.     print_newline()
  215.   );
  216. ;;
  217.  
  218.  
  219.  
  220. (* ex15 *)
  221. let print_multable(n : int) : unit =
  222.   for i = 1 to 10
  223.   do
  224.     print_int(i) ;
  225.     print_string(" * ") ;
  226.     print_int(n) ;
  227.     print_string(" = ") ;
  228.     print_int(i * n) ;
  229.     print_newline()
  230.   done ;
  231. ;;
  232.  
  233.  
  234. let print_multables(p : int) : unit =
  235.   for i = 1 to p
  236.   do
  237.     print_multable(i) ;
  238.     print_newline()
  239.   done ;
  240. ;;
  241.  
  242.  
  243. let get_int() : int =
  244.   let v : int ref = ref 0
  245.   and thend : bool ref = ref false
  246.   in
  247.   (
  248.     while not( !thend)
  249.     do
  250.       print_string("saisissez un entier strictement positif : ") ;
  251.       v := read_int() ;
  252.       thend := (1 <= !v)
  253.     done ;
  254.     !v
  255.   )
  256. ;;
  257.  
  258. let get_print_multables() : unit =
  259.   let p : int = get_int()
  260.   in
  261.   print_multables(p)
  262. ;;
  263.  
  264. get_print_multables();;
  265.  
  266.  
  267. (* exo 16 *)
  268. let syracuse(a : int) : bool =
  269.   let v : int ref = ref a in
  270.   (
  271.     while not(!v = 1)
  272.     do
  273.       if !v mod 2 = 0
  274.       then v := !v / 2
  275.       else v := 3 * !v + 1
  276.     done ;
  277.     true
  278.   )
  279. ;;
  280.  
  281. let syracuse_bis(a : int) :unit =
  282.   let v : int ref = ref a in
  283.   (
  284.     while not(!v = 1)
  285.     do
  286.       if !v mod 2 = 0
  287.       then v := !v / 2
  288.       else v := 3 * !v + 1;
  289.      
  290.       print_int(!v);
  291.       print_string(" ");
  292.     done ;
  293.   )
  294. ;;
  295. syracuse_bis(17);;
  296.  
  297. (* exo 17 *)
  298. let print_mult3() : unit =
  299.   (
  300.     for i = 1 to 100
  301.     do
  302.       print_int(3 * i) ;
  303.       print_string(" ")
  304.     done ;
  305.     print_newline()
  306.   )
  307. ;;
  308.  
  309.  
  310. let print_mult3bis() : unit =
  311.   let v : int ref = ref 3
  312.   and compteur : int ref = ref 1 in
  313.   (
  314.     while !compteur <= 100
  315.     do
  316.       print_int(!v) ;
  317.       print_string(" ") ;
  318.       v := !v + 3;
  319.       compteur := !compteur +1;
  320.     done ;
  321.     print_newline()
  322.   )
  323. ;;
  324.  print_mult3bis();;
  325.  
  326. let print_mult3inf100() : unit =
  327.   let v : int ref = ref 3 in
  328.   (
  329.     while !v < 100
  330.     do
  331.       print_int(!v) ;
  332.       print_string(" ") ;
  333.       v := !v + 3
  334.     done ;
  335.     print_newline()
  336.   )
  337. ;;
  338.  
  339.  
  340. (* exo en autonomie *)
  341. (* exo 18 *)
  342. let classmarks() : int * int * int * int =
  343.     let nb : int ref = ref 0
  344.     and v : int ref = ref 0
  345.     and min : int ref = ref 20
  346.     and max : int ref = ref 0
  347.     and sum : int ref = ref 0
  348.     in
  349.     (
  350.     while !v <> -1
  351.     do
  352.         print_string("saisissez une note : ") ;
  353.         v := read_int() ;
  354.         if !v >= 0 && !v <= 20
  355.         then
  356.             (
  357.             nb := !nb + 1 ;
  358.             if !v < !min
  359.             then min := !v ;
  360.             if !v > !max
  361.             then max := !v ;
  362.             sum := !sum + !v
  363.             )
  364.         else ()
  365.     done ;
  366.     (!nb, !max, !min, !sum / !nb)
  367.     )
  368. ;;
  369.  
  370. (* exo 19 *)
  371. type t_cow = {prod : float ; milk : float ; us : float} ;;
  372.  
  373. (* simulation de la production d'une vache *)
  374. let simulcow() : t_cow =
  375.     let cowprod : float ref = ref 0.0
  376.     and cowmilk : float ref = ref 0.0
  377.     and cowus : float ref = ref 0.0
  378.     in
  379.     (
  380.     cowprod := float_of_int(rand_int(18, 22)) ;
  381.     if rand_int(1, 120) <= 10
  382.     then cowmilk := !cowprod /. 2.0
  383.     else cowmilk := !cowprod ;
  384.     if rand_int(1, 100) <= 12
  385.     then cowus := 0.0
  386.     else cowus := !cowmilk ;
  387.     {prod = !cowprod ; milk = !cowmilk ; us = !cowus}
  388. )
  389. ;;
  390. (* simulation de la production de n vaches *)
  391. let simulncow(n : int) : t_cow =
  392.     let cowprod : float ref = ref 0.0
  393.     and cowmilk : float ref = ref 0.0
  394.     and cowus : float ref = ref 0.0
  395.     and cow : t_cow ref = ref ({prod = 0.0 ; milk = 0.0 ; us = 0.0})
  396.     in
  397.     (
  398.     for j = 1 to n
  399.     do
  400.         cow := simulcow() ;
  401.         cowprod := !cowprod +. (!cow).prod ;
  402.         cowmilk := !cowmilk +. (!cow).milk ;
  403.         cowus := !cowus +. (!cow).us
  404.     done ;
  405.     {prod = !cowprod ; milk = !cowmilk ; us = !cowus}
  406.     )
  407. ;;
  408.  
  409. (* simulation de la production de n vaches sur 30 jours *)
  410. let simul30(n : int) : t_cow =
  411.     let cowprod : float ref = ref 0.0
  412.     and cowmilk : float ref = ref 0.0
  413.     and cowus : float ref = ref 0.0
  414.     and cow : t_cow ref = ref ({prod = 0.0 ; milk = 0.0 ; us = 0.0})
  415.     in
  416.     (
  417.     for i = 1 to 30
  418.     do
  419.         cow := simulncow(n) ;
  420.         cowprod := !cowprod +. (!cow).prod ;
  421.         cowmilk := !cowmilk +. (!cow).milk ;
  422.         cowus := !cowus +. (!cow).us
  423.     done ;
  424.     {prod = !cowprod ; milk = !cowmilk ; us = !cowus}
  425.     )
  426. ;;
  427. (* saisie d'un nombre positif *)
  428. let get_intpos() : int =
  429.     let v : int ref = ref (-1) in
  430.     (
  431.     while !v < 0
  432.     do
  433.         print_string(" saisissez un nombre positif : ") ;
  434.         v := read_int()
  435.     done ;
  436.     !v
  437.     )
  438. ;;
  439. (* calcul du temps pour produire un certain nombre de reblochons *)
  440. (* avec le lait de n vaches *)
  441. let calcreb(n : int) : int =
  442.     let nbreb : int = get_intpos()
  443.     and cowus : float ref = ref 0.0
  444.     and nbday : int ref = ref 0
  445.     and cow : t_cow ref = ref ({prod = 0.0 ; milk = 0.0 ; us = 0.0})
  446.     in
  447.         let req : float = float_of_int(nbreb * 4)
  448.         in
  449.         (
  450.         while !cowus < req
  451.         do
  452.             nbday := !nbday + 1 ;
  453.             cow := simulncow(n) ;
  454.             cowus := !cowus +. (!cow).us
  455.         done ;
  456.         !nbday ;
  457.         )
  458. ;;
  459.  
  460. (* exo 20 *)
  461. #use "APutil.ml" ;;
  462. open_graph(1000, 700) ;;
  463.  
  464. (* type pour representer des points *)
  465. type t_point = {x : int ; y : int} ;;
  466.  
  467. (* fonctions de l'exercice 4, modifiee *)
  468. let generate_point(n : int) : t_point =
  469.     {x = rand_int(-n, n-1) ; y = rand_int(-n, n-1)}
  470. ;;
  471. let is_inside_circle(p, n : t_point * int) : bool =
  472. (p.x * p.x + p.y * p.y) < n * n
  473. ;;
  474.  
  475. let compute_pi(nb, n : int * int) : float =
  476.     let incircle : int ref = ref 0
  477.     and p : t_point ref = ref({x = 0 ; y = 0})
  478.     in
  479.     (
  480.         draw_rect(20, 20, 2 * n, 2 * n) ;
  481.         draw_circle(n + 20, n + 20, n) ;
  482.         for i = 1 to nb
  483.         do
  484.             p := generate_point(n) ;
  485.             if is_inside_circle(!p, n)
  486.             then
  487.                 (
  488.                 set_color(red) ;
  489.                 incircle := !incircle + 1
  490.                 )
  491.             else set_color(green) ;
  492.             plot((!p).x + n + 20, (!p).y + n + 20)
  493.         done ;
  494.         4.0 *. (float_of_int(!incircle) /. float_of_int(nb))
  495.     )
  496. ;;
  497.  
  498. (* exo 21 *)
  499.  
  500. let draw_sin(n : int) : unit =
  501.     let pi : float = 3.141592653589793 in
  502.     let ang : float ref = ref 0.0
  503.     and x : float ref = ref 0.0
  504.     and y : float ref = ref 0.0
  505.     in
  506.         let dang : float = pi /. float_of_int(n)
  507.         in
  508.         (
  509.         for i = 1 to n
  510.         do
  511.             ang := !ang +. dang ;
  512.             x := cos(!ang) ;
  513.             y := sin(!ang)
  514.         done
  515.         )
  516. ;;
  517.  
  518. let draw_sin(n : int) : unit =
  519.     let pi : float = 3.141592653589793
  520.     and dx : int = 200
  521.     and dy : int = 200
  522.     in
  523.         let ang : float ref = ref 0.0
  524.         and y : float ref = ref 0.0
  525.         in
  526.             let dang : float = (2.0 *. pi) /. float_of_int(n)
  527.             in
  528.             (
  529.             moveto(dx + int_of_float(100.0 *. !ang), dy + int_of_float(100.0 *. sin(!ang)));
  530.             for i = 1 to n
  531.             do
  532.                 ang := !ang +. dang ;
  533.                 y := sin(!ang) ;
  534.                 lineto(dx + int_of_float(100.0 *. !ang), dy + int_of_float(100.0 *. !y))
  535.             done
  536.             )
  537. ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement