Advertisement
yannick_degardin

Ocaml TD8

Nov 26th, 2020 (edited)
1,959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 13.30 KB | None | 0 0
  1. (* AP1 TD8 *)
  2.  
  3. #use "APutil.ml";;
  4.  
  5. (* initialisation du tableau fortune *)
  6. let init(nbp : int) : int array =
  7.   let t : int array = arr_make(nbp, 1000) in
  8.   t
  9. ;;
  10.  
  11.  
  12. (* saisie controle d¡¯un entier dans l¡¯intervalle [a, b] *)
  13. let get_int(a, b : int * int) : int =
  14.   let v : int ref = ref 0  and thend : bool ref = ref false in
  15.   (
  16.     while not( !thend)
  17.     do
  18.       print_string("saisissez une valeur comprise entre ") ;
  19.       print_int(a);
  20.       print_string(" et ");
  21.       print_int(b);
  22.       print_string(" : ");
  23.       v := read_int();
  24.       thend := ( !v >= a && !v <= b)
  25.     done;
  26.     !v
  27.   )
  28. ;;
  29.  
  30. (* exemple d'utilisation -  valider la saisie par crtl-Entrée (mais pas la touche Entre du pavé numérique)*)
  31. let y : int = get_int(1,7);;
  32.  
  33. (* choix du numero joue *)
  34. let choice_number(t : int array) : unit =
  35.   let maxind : int = arr_len(t) - 1 in
  36.   for i = 0 to maxind
  37.   do
  38.     print_string(" joueur numero ") ;
  39.     print_int(i) ;
  40.     print_string(", saisissez le numero sur lequel vous misez") ;
  41.     print_newline() ;
  42.     t.(i) <- get_int(0, 36)
  43.   done
  44. ;;
  45.  
  46.  
  47. (* test de la fct choice_mumber *)
  48. let mytabmumber : int array = [|0;0;0;0;0|];;
  49. choice_number(mytabmumber);;
  50. mytabmumber;;
  51.  
  52.  
  53. (**** parenthese sur le fait de modifier un paremetre dans une fct ********)
  54. (* exemple d'une fct qui modifie son parametre *)
  55. let modifie( x  : int ref): unit =
  56.   x := 2* !x
  57. ;;
  58.  
  59. let yy: int ref = ref 77;;
  60. modifie(yy);;
  61. yy;;
  62.  
  63. (* le cas qui ne marche pas: le cast de y en int ref ne permet pas la modification de y *)
  64. let y: int = 7;;
  65. modifie( ref(y) );;
  66. y;;  (* et non: y reste inchangé*)
  67. (************************************************ fin de la parenthese ***)
  68. (* choix de la mise *)
  69. let choice_bet(t, f : int array * int array) : unit =
  70.   let maxind : int = arr_len(t) - 1 in
  71.   for i = 0 to maxind
  72.   do
  73.     print_string(" joueur numero ") ;
  74.     print_int(i) ;
  75.     print_string(", saisissez votre mise") ;
  76.     print_newline() ;
  77.     t.(i) <- get_int(1, f.(i))
  78.   done
  79. ;;
  80.  
  81. (* calcul des mises par numero *)
  82. let compute_bet_per_number(num, be, tot, totbet : int array * int array * int array * int ref) : unit =
  83.   let maxind : int = arr_len(num) - 1 in
  84.   (
  85.     for i = 0 to 36
  86.     do tot.(i) <- 0
  87.     done ;
  88.     for i = 0 to maxind
  89.     do
  90.       tot.(num.(i)) <- tot.(num.(i)) + be.(i) ;
  91.       totbet := !totbet + be.(i)
  92.     done
  93.   )
  94. ;;
  95.  
  96. (* calcul des gains *)
  97. let compute_gain(num, be, tot, totbet, gain, n : int array * int array * int array * int ref * int array * int) : unit =
  98.   let maxind : int = arr_len(num) - 1 and hasbet : bool ref = ref false in
  99.   (
  100.     for i = 0 to maxind
  101.     do
  102.       if num.(i) = n
  103.       then
  104.         (
  105.           hasbet := true ;
  106.           gain.(i) <- ( !totbet * be.(i)) / tot.(n)
  107.         )
  108.       else gain.(i) <- - be.(i)
  109.     done ;
  110.     if !hasbet
  111.     then totbet := 0
  112.     else ()
  113.   )
  114. ;;
  115.  
  116. (* fonction de simulation *)
  117. let simul(nb, nbPlayers : int * int) : int array =
  118.   let fortune : int array = init(nbPlayers)
  119.   and number : int array = arr_make(nbPlayers, 0)
  120.   and bet : int array = arr_make(nbPlayers, 0)
  121.   and total : int array = arr_make(37, 0)
  122.   and totalbet : int ref = ref 0
  123.   and gain : int array = arr_make(nbPlayers, 0)
  124.   and n : int ref = ref 0
  125.   and maxind : int = nbPlayers - 1
  126.   in
  127.   (
  128.     for i = 1 to nb
  129.     do
  130.       choice_number(number);
  131.       choice_bet(bet, fortune);
  132.       compute_bet_per_number(number, bet, total, totalbet);
  133.       n := rand_int(0, 36);
  134.       print_string("numero gagnant : ");
  135.       print_int( !n);
  136.       print_newline();
  137.       compute_gain(number, bet, total, totalbet, gain, !n);
  138.       for i = 0 to maxind
  139.       do fortune.(i) <- fortune.(i) + gain.(i)
  140.       done
  141.     done ;
  142.     fortune
  143.   )
  144. ;;
  145.  
  146.  
  147.  
  148. (* exo 25 ------------------------------------------------------------------------------*)
  149.  
  150. (* definition des types et creation du tableau *)
  151. type t_date = {d : int ; m : int ; y : int} ;;
  152. type t_member = {name : string ; birth : t_date} ;;
  153.  
  154. (* fonction replace *)
  155. let replace(t, member, i : t_member array * t_member * int) : unit =
  156.   if not(0 <= i && i <= arr_len(t) - 1)
  157.   then failwith "erreur replace : erreur d’indice"
  158.   else t.(i) <- member
  159. ;;
  160.  
  161. (* fonction inf_date *)
  162. let inf_date(d1, d2 : t_date * t_date) : bool =
  163.   (d1.y < d2.y) || ((d1.y = d2.y) && ((d1.m < d2.m) || ((d1.y = d2.y) && (d1.m = d2.m) && (d1.d <= d2.d))))
  164. ;;
  165.  
  166.  
  167. (* la case 0 du tableau n est pas utilisée *)
  168. (* fonction youngest_member *)
  169. let youngest_member(t : t_member array) : t_member =
  170.   let n : int = arr_len(t) in
  171.   if n = 0
  172.   then failwith "erreur youngest_member : tableau de longueur nulle"
  173.   else
  174.     let member : t_member ref = ref (t.(1))
  175.     in
  176.     (
  177.       for i = 2 to n - 1      
  178.       do
  179.         (* inf_date renvoie VRAI si la 1ere date est antérieure (donc plus vielle) que la seconde *)
  180.         (* on mémorisera le membre de la case du moment s'il est plus jeune que le plus jeune pécédement trouvé *)
  181.         if inf_date( (!member).birth, (t.(i)).birth) )
  182.         then member := t.(i)
  183.         else ()
  184.       done ;
  185.       !member
  186.     )
  187. ;;
  188.    
  189. (* fonction oldest_member *)
  190. let oldest_member(t : t_member array) : t_member =
  191.   let n : int = arr_len(t)
  192.   in
  193.   if n = 0
  194.   then failwith "erreur oldest_member : tableau de longueur nulle"
  195.   else
  196.     let member : t_member ref = ref (t.(1)) in
  197.     (
  198.       for i = 2 to n - 1
  199.       do
  200.         if inf_date((t.(i)).birth, ( !member).birth)
  201.         then member := t.(i)
  202.         else ()
  203.       done ;
  204.       !member
  205.     )
  206. ;;
  207.  
  208. (* test *)
  209. let t : t_member array = arr_make(20, {name = "truc" ; birth = {d = 20 ; m = 1 ; y = 2001}}) ;;
  210. replace(t, {name = "machin" ; birth = {d = 4 ; m = 6 ; y = 2007}}, 2) ;;
  211. youngest_member(t) ;;
  212. oldest_member(t) ;;
  213.  
  214.  
  215.  
  216. (* exercice 26 --------------------------------------------------------------*)
  217.  
  218. (* saisie de n valeurs comprises entre v_min et v_max *)
  219. let get_nval(n, v_min, v_max : int * float * float) : float array =
  220.   let t : float array = arr_make(n, 0.0) and indmax : int = n - 1 in
  221.   (
  222.     for i = 0 to indmax
  223.     do t.(i) <- get_float(v_min, v_max)
  224.     done ;
  225.     t
  226.   )
  227. ;;
  228.  
  229.  
  230. (* calcul de la somme, moyenne, min et max d’un tableau *)
  231. let comp_val(t : float array) : float * float * float * float =
  232.   let ln : int = arr_len(t) in
  233.   let indmax : int = ln - 1 in
  234.   if ln = 0
  235.   then failwith "erreur comp_val : tableau vide"
  236.   else
  237.     let sum : float ref = ref t.(0) and min : float ref = ref t.(0)
  238.         and max : float ref = ref t.(0)
  239.     in
  240.     (
  241.       for i = 1 to indmax
  242.       do
  243.         sum :=!sum +. t.(i);
  244.         if !min > t.(i)
  245.         then min := t.(i);
  246.        
  247.         if !max < t.(i)
  248.         then max := t.(i)
  249.         else ()
  250.       done ;
  251.       (!sum, !sum /. float_of_int(ln), !min, !max)
  252.     )
  253. ;;
  254.  
  255. (* saisie et calcul *)
  256. let main(v_min, v_max : float * float) : float array * float * float * float * float =
  257.   let n : int = get_intpos() in
  258.   let t : float array = get_nval(n, v_min, v_max) in
  259.   let (sum, avg, min, max) : float * float * float * float = comp_val(t) in
  260.   (t, sum, avg, min, max)
  261. ;;
  262.  
  263. (* exercice 27 --------------------------------------------------------------*)
  264. let tab : int array = [|12;6;3;1;0;-1|];;
  265.  
  266. let mirror(t : int array) : int array =
  267.   let ln : int = arr_len(t) in
  268.   let indmax : int = ln - 1
  269.   and r : int array = arr_make(ln, 0)
  270.   in
  271.   (
  272.     for i = indmax downto 0
  273.     do r.(i) <- t.(indmax - i)
  274.     done;
  275.     r
  276.   )
  277. ;;
  278.  
  279.  
  280. let mirror_bis(t : int array) : unit =
  281.   let ln : int = arr_len(t) in
  282.   let indmax : int = ln - 1
  283.   and r : int array = arr_make(ln, 0)
  284.   in
  285.   (
  286.     (* inverse les valeurs vers un tableau intermediaire *)
  287.     for i = indmax downto 0
  288.     do r.(i) <- t.(indmax - i)
  289.     done;
  290.  
  291.     (* recopioe des valeurs  inversées dans le tab source*)
  292.     for i = 0 to indmax
  293.     do t.(i) <- r.(i)
  294.     done;
  295.   )
  296. ;;
  297.  
  298. tab;;
  299. let inverse : int array = mirror(tab);;
  300. mirror_bis(tab);;
  301. tab;;
  302. (* exercice 28 --------------------------------------------------------------*)
  303. let seek(v, t : int * int array) : bool =
  304.   let n : int = arr_len(t) in
  305.   let exist : bool ref = ref false and thend : bool ref = ref false
  306.       and indmax : int = n - 1 and i : int ref = ref (-1)
  307.   in
  308.   (
  309.     while not( !thend)
  310.     do
  311.       i := !i + 1;
  312.       if !i <= indmax
  313.       then
  314.         (
  315.           exist := (v = t.( !i));
  316.           thend := !exist
  317.         )
  318.       else thend := true
  319.     done;
  320.     !exist
  321.   )
  322. ;;
  323.  
  324. (* variante rendant en plus l’indice *)
  325. let seek(v, t : int * int array) : bool * int =
  326.   let n : int = arr_len(t) in
  327.   let exist : bool ref = ref false and thend : bool ref = ref false
  328.       and indmax : int = n - 1 and i : int ref = ref (-1) in
  329.   (
  330.     while not( !thend)
  331.     do
  332.       i := !i + 1 ;
  333.       if !i <= indmax
  334.       then
  335.         (
  336.           exist := (v = t.( !i)) ;
  337.           thend := !exist
  338.         )
  339.       else thend := true
  340.     done ;
  341.     (!exist, !i)
  342.   )
  343. ;;
  344.  
  345.  
  346. (* exercice 29 --------------------------------------------------------------*)
  347. let simul() : int * int array =
  348.   let t : int array = arr_make(20, 0) and thend : bool ref = ref false
  349.       and nb : int ref = ref 0 and v : int ref = ref 0 in
  350.   (
  351.     while not( !thend)
  352.     do
  353.       nb := !nb + 1 ;
  354.       v := rand_int(1, 20) ;
  355.       t.( !v - 1) <- t.( !v - 1) + 1 ;
  356.       thend := ( !v = 20)
  357.     done ;
  358.     (!nb, t)
  359.   )
  360. ;;
  361.  
  362.  
  363. (* exercice 30 --------------------------------------------------------------*)
  364.  
  365. let shift_left(t : int array) : unit =
  366.   let ln : int = arr_len(t) in
  367.   let indmax : int = ln - 1 and c : int ref = ref 0 in
  368.   if ln <> 0
  369.   then
  370.     (
  371.       c := t.(0);
  372.       for i = 1 to indmax
  373.       do t.(i-1) <- t.(i)
  374.       done;
  375.       t.(indmax) <- !c
  376.     )
  377.   else ()
  378. ;;
  379.  
  380. let shift_right(t : int array) : unit =
  381.   let ln : int = arr_len(t) in
  382.   let indmax : int = ln - 1 and c : int ref = ref 0 in
  383.   if ln <> 0
  384.   then
  385.     (
  386.       c := t.(indmax);
  387.       for i = indmax downto 1
  388.       do t.(i) <- t.(i - 1)
  389.       done ;
  390.       t.(0) <- !c
  391.     )
  392.   else ()
  393. ;;
  394.  
  395. (* exercice 31 --------------------------------------------------------------*)
  396. let comp_sin(n : int) : (float * float) array =
  397.   if n <= 0
  398.   then failwith "erreur comp_sin : parametre negatif"
  399.   else
  400.     (
  401.       let pi : float = 3.141592653589793 and t = arr_make(n+1, (0.0, 0.0))
  402.           and x : float ref = ref 0.0 and y : float ref = ref 0.0
  403.       in
  404.       let dx : float = (2.0 *. pi) /. float_of_int(n) in
  405.       for i = 0 to n
  406.       do
  407.         x := -. pi +. float_of_int(i) *. dx ;
  408.         y := sin( !x) ;
  409.         t.(i) <- ( !x, !y)
  410.       done ;
  411.       t
  412.     )
  413. ;;
  414.  
  415.  
  416.  
  417. (* exercice 32 --------------------------------------------------------------*)
  418. (* types *)
  419. type t_prod = {name : string ; cat : string ; nb : int ref ; price : int ref} ;;
  420. type t_date = {year : int ; month : int ; day : int} ;;
  421. type t_mem = {forename : string ; name : string ; birth : t_date ; amount : int ref} ;;
  422. type t_products = t_prod array ;;
  423. type t_members = t_mem array ;;
  424.  
  425. (* fonction testant l’existence d’un produit de nom donne *)
  426. let prod_exists(name, tp : string * t_products) : bool * int =
  427.   let ln : int = arr_len(tp) in
  428.   let indmax : int = ln - 1 and found : bool ref = ref false and
  429.       thend : bool ref = ref false and i : int ref = ref (-1)
  430.   in
  431.   (
  432.     while not( !thend)
  433.     do
  434.       i := !i + 1 ;
  435.       if !i > indmax
  436.       then thend := true
  437.       else
  438.         if (tp.( !i)).name = name
  439.         then
  440.           (
  441.             found := true ;
  442.             thend := true
  443.           )
  444.         else i := !i + 1
  445.     done ;
  446.     (!found, !i)
  447.   )
  448. ;;
  449.  
  450. (* ajout de n exemplaires du produit de nom donne *)
  451. let add_product(name, nb, tp : string * int * t_products) : unit =
  452.   let (b, i) : bool * int = prod_exists(name, tp) in
  453.   if b
  454.   then (tp.(i)).nb := !((tp.(i)).nb) + nb
  455.   else (
  456.     print_string("produit non en stock");
  457.     print_newline();
  458.   )
  459. ;;
  460.  
  461. (* modification des prix d’un certain taux ; ici, le taux est un entier a diviser par 100 *)
  462. let mod_price(tp, rate : t_products * int) : unit =
  463.   let ln : int = arr_len(tp) in
  464.   let indmax : int = ln - 1 in
  465.   for i = 0 to indmax
  466.   do (tp.(i)).price := ( !((tp.(i)).price) * rate) / 100
  467.   done
  468. ;;
  469.  
  470. (* achat d’un produit ; il faut une fonction de recherche d’un client *)
  471. let mem_exists(name, tc : string * t_members) : bool * int =
  472.   let ln : int = arr_len(tc) in
  473.   let indmax : int = ln - 1 and found : bool ref = ref false
  474.       and thend : bool ref = ref false and i : int ref = ref (-1) in
  475.   (
  476.     while not( !thend)
  477.     do
  478.       i := !i + 1 ;
  479.       if !i > indmax
  480.       then thend := true
  481.       else
  482.         if (tc.( !i)).name = name
  483.         then
  484.           (
  485.             found := true ;
  486.             thend := true
  487.           )
  488.         else i := !i + 1
  489.     done ;
  490.     ( !found, !i)
  491.   )
  492. ;;
  493.  
  494. let buy(c_name, p_name, n, tc, tp : string * string * int * t_members * t_products) : bool =
  495.   let (bc, ic) : bool * int = mem_exists(c_name, tc)
  496.   and (bp, ip) : bool * int = prod_exists(p_name, tp)
  497.   and pricetot : int ref = ref 0 in
  498.   if bc && bp
  499.   then
  500.     (
  501.       pricetot := n * !(tp.(ip).price) ;
  502.       if !(tc.(ic).amount) >=!pricetot
  503.       then
  504.         (
  505.           tc.(ic).amount := !(tc.(ic).amount) - !pricetot ;
  506.           tp.(ip).nb := !(tp.(ip).nb) - n ;
  507.           true
  508.         )
  509.       else false
  510.     )
  511.   else false
  512. ;;
  513.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement