Advertisement
Wyvern67

TP noté 1

Nov 30th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.55 KB | None | 0 0
  1.  
  2. (*            *)
  3. (* Exercice 1 *)
  4. (*            *)
  5.  
  6. print_string "Exercice 1: \n";;
  7. type date = {
  8.     jour: int;
  9.     mois: int;
  10.     annee: int
  11. };;
  12.  
  13. let creer_date (sjour:int) (smois:int) (sannee:int) = {
  14.     jour = sjour;
  15.     mois = smois;
  16.     annee = sannee
  17. };;
  18.  
  19.  
  20. type produit = {
  21.     nom: string;
  22.     prix: float;
  23.     sdate: date
  24. };;
  25.  
  26. let creer_produit (snom:string) (sprix:float) (sdate:date) = {
  27.     nom = snom;
  28.     prix = sprix;
  29.     sdate = sdate
  30. };;
  31.  
  32. let fromage=(creer_produit "Fromage" 2.57 (creer_date 23 5 2015));;
  33.  
  34. let validite_produit (produit:produit) =
  35.     if produit.sdate.annee >= 2015 then
  36.         if produit.sdate.mois < 5 then
  37.             false
  38.         else
  39.             if produit.sdate.mois = 5 then
  40.                 if produit.sdate.jour < 30 then
  41.                     false
  42.                 else
  43.                     true
  44.             else
  45.                 true
  46.     else
  47.         false;;
  48.  
  49. let acheter (produit:produit) (q:int) =
  50.     if (validite_produit produit) = true then
  51.         produit.prix *. (float_of_int q)
  52.     else
  53.         0.0;;
  54.  
  55. let fromageMangeable=(creer_produit "Fromage" 2.57 (creer_date 23 6 2015));;
  56. print_string ("Prix de 4 fromages pourris: " ^ string_of_float (acheter fromage 4)^"€ (Invendables)\n");;
  57. print_string ("Prix de 4 fromages encore bons: " ^ string_of_float (acheter fromageMangeable 4)^"€\n");;
  58.  
  59. (* Renvoie true si le produit est vieux de plus de X mois *)
  60. let anciennete (produit:produit) (mois:int) =
  61.     if produit.sdate.annee >= 2015 then
  62.         if produit.sdate.mois < (11-mois) then
  63.             true
  64.         else
  65.             if produit.sdate.mois = (11-mois) then
  66.                 if produit.sdate.jour < 30 then
  67.                     true
  68.                 else
  69.                     false
  70.             else
  71.                 false
  72.     else
  73.         true;;
  74.  
  75. let reduction (p:produit) (q:int) =
  76.     let facture = (acheter p q) in
  77.         if facture > 0.0 then
  78.             if (anciennete p 3) && (q > 5) then
  79.                 0.9 *. facture
  80.             else
  81.                 facture
  82.         else
  83.             0.0;;
  84.  
  85.  
  86. print_newline();;
  87. print_string ("En promo: 6 fromages un peu vieux pour "^(string_of_float (reduction fromageMangeable 6))^"€\n");;
  88. print_string ("Prix normal: 6 fromages un peu vieux pour "^(string_of_float (acheter fromageMangeable 6))^"€\n");;
  89. print_string ("En promo: 6 fromages pourris pour "^(string_of_float (reduction fromage 6))^"€ (invendables)\n");;
  90.  
  91.  
  92. (*            *)
  93. (* Exercice 2 *)
  94. (*            *)
  95.  
  96.  
  97. print_string("\nExercice 2\n")
  98.  
  99. type intervalle = {
  100.     min:float;
  101.     max:float
  102. };;
  103.  
  104. let creer_inter (a:float) (b:float) = {
  105.     min=a;
  106.     max=b
  107. };;
  108.  
  109. let rec pi n a b =
  110.     if n=0 then
  111.         a
  112.     else
  113.         let m = (a +. b) /. 2.0 in
  114.             if (sin m) < 0.0 then
  115.                 pi (n-1) a m
  116.             else
  117.                 pi (n-1) m b;;
  118.  
  119. print_string("Pi~=" ^ string_of_float (pi 20 2.0 4.0));;
  120. print_newline();;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement