Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* TD7 exo 9*)
- #use "APutil.ml";;
- let simul(t : float) : int * float =
- let nb : int ref = ref 0
- and newt : float ref = ref t
- and var : float ref = ref 0.0 in
- if t < 10.0 || t > 50.0
- then failwith "erreur simul : taille initiale non conforme"
- else
- (
- while (!newt < 2.0 *. t)
- do
- nb := !nb + 1 ;
- var := 0.5 *. (float_of_int(rand_int(80, 120)) /. 100.0) ;
- newt := !newt +. !var ;
- if !nb mod 10 = 0
- then
- (
- print_string("au siècle ") ;9
- print_int(!nb) ;
- print_string(", ");
- print_string("la nouvelle taille est ");
- print_float(!newt) ;
- print_string( "cm") ;
- print_newline()
- )
- else ()
- done ;
- (!nb, !newt)
- ) ;
- ;;
- (* exo 10 *)
- let generate_point(n : int) : int * int =
- (rand_int(-n, n-1), rand_int(-n, n-1))
- ;;
- let is_inside_circle((x, y), n : (int * int) * int) : bool =
- (x * x + y * y) < n * n
- ;;
- let compute_pi(nb, n : int * int) : float =
- let incircle : int ref = ref 0 in
- (
- for i = 1 to nb
- do
- if is_inside_circle(generate_point(n), n)
- then incircle := !incircle + 1
- else ()
- done ;
- 4.0 *. (float_of_int(!incircle) /. float_of_int(nb))
- )
- ;;
- compute_pi(100000, 10000);;
- (* exo 11 *)
- (* premiere version *)
- let get_answer() : char =
- let answ : char ref = ref 'a' and thend : bool ref = ref false in
- (
- while not(!thend)
- do
- print_string("saisissez votre reponse : ") ;
- answ := read_char() ;
- if !answ <> '>' && !answ <> '<' && !answ <> '='
- then
- (
- print_string("erreur de saisie ; merci de saisir <, > ou =") ;
- print_newline()
- )
- else thend := true
- done;
- !answ
- )
- ;;
- let guess_number() : int =
- let thend : bool ref = ref false
- and min : int ref = ref 1
- and max : int ref = ref 200
- and answ : char ref = ref 'a'
- and prop : int ref = ref 0
- in
- (
- while not(!thend)
- do
- prop := rand_int(!min, !max) ;
- print_string("valeur proposee : ") ;
- print_int(!prop) ;
- print_newline() ;
- answ := get_answer() ;
- if !answ = '<'
- then max := !prop - 1
- else
- if !answ = '>'
- then min := !prop + 1
- else
- if !answ = '='
- then
- (
- thend := true ;
- print_string("merci") ;
- print_newline()
- )
- else ()
- done ;
- !prop
- )
- ;;
- (* variante avec controle *)
- let guess_number() : int =
- let thend : bool ref = ref false
- and min : int ref = ref 1
- and max : int ref = ref 200
- and answ : char ref = ref 'a'
- and prop : int ref = ref 0
- in
- (
- while not(!thend)
- do
- if !min > !max
- then
- (
- thend := true ;
- prop := 0 ;
- print_string("tricheur !") ;
- print_newline()
- )
- else
- (* c'est inchange par rapport a la version precedente *)
- ;;
- (* -------------- exo de TP ---------------------*)
- (* ex12 factoriel *)
- let fact(n : int) : int =
- let res : int ref = ref 1 in
- if n < 0
- then failwith "erreur fact ; parametre negatif"
- else
- (
- for i = 1 to n
- do res := !res * i
- done ;
- !res
- )
- ;;
- (* ex13 triplet *)
- let get_letter() : char =
- let res : char ref = ref 'a' and thend : bool ref = ref false in
- (
- while not(!thend)
- do
- print_string("saisissez une lettre : ") ;
- res := read_char() ;
- thend := (!res >= 'a' && !res <= 'z’) || (!res >= 'A' && !res <= 'Z’)
- done ;
- !res
- )
- ;;
- let get_char(min, max : char * char) : char =
- let res : char ref = ref 'a' and thend : bool ref = ref false in
- (
- while not(!thend)
- do
- print_string("saisissez un caractère compris entre ") ;
- print_char(min) ;
- print_string(" et ") ;
- print_char(max) ;
- print_string(" : ") ;
- res := read_char() ;
- thend := !res >= min && !res <= max
- done ;
- !res
- )
- ;;
- let get_code() : char * char * char =
- let c1 : char = get_letter() and c2 : char = get_char(’0’, '9’)
- and c3 : char ref = ref 'a' in
- (
- if (c1 >= 'a' && c1 <= 'z’)
- then c3 := get_char(’A’, 'Z’)
- else c3 := get_char(’a’, 'z’) ;
- (c1, c2, !c3)
- )
- ;;
- (* ex14 *)
- let print_letter() : unit =
- let res : char ref = ref 'a' in
- (
- for i = 0 to 25
- do
- print_char(char_of_int(int_of_char( !res) + i)) ;
- print_char(' '’)
- done ;
- print_newline() ;
- res := 'A' ;
- for i = 25 downto 0
- do
- print_char(char_of_int(int_of_char( !res) + i)) ;
- print_char(' '’)
- done ;
- print_newline()
- );
- ;;
- (* ex15 *)
- let print_multable(n : int) : unit =
- for i = 1 to 10
- do
- print_int(i) ;
- print_string(" * ") ;
- print_int(n) ;
- print_string(" = ") ;
- print_int(i * n) ;
- print_newline()
- done ;
- ;;
- let print_multables(p : int) : unit =
- for i = 1 to p
- do
- print_multable(i) ;
- print_newline()
- done ;
- ;;
- let get_int() : int =
- let v : int ref = ref 0
- and thend : bool ref = ref false
- in
- (
- while not( !thend)
- do
- print_string("saisissez un entier strictement positif : ") ;
- v := read_int() ;
- thend := (1 <= !v)
- done ;
- !v
- )
- ;;
- let get_print_multables() : unit =
- let p : int = get_int()
- in
- print_multables(p)
- ;;
- get_print_multables();;
- (* exo 16 *)
- let syracuse(a : int) : bool =
- let v : int ref = ref a in
- (
- while not(!v = 1)
- do
- if !v mod 2 = 0
- then v := !v / 2
- else v := 3 * !v + 1
- done ;
- true
- )
- ;;
- let syracuse_bis(a : int) :unit =
- let v : int ref = ref a in
- (
- while not(!v = 1)
- do
- if !v mod 2 = 0
- then v := !v / 2
- else v := 3 * !v + 1;
- print_int(!v);
- print_string(" ");
- done ;
- )
- ;;
- syracuse_bis(17);;
- (* exo 17 *)
- let print_mult3() : unit =
- (
- for i = 1 to 100
- do
- print_int(3 * i) ;
- print_string(" ")
- done ;
- print_newline()
- )
- ;;
- let print_mult3bis() : unit =
- let v : int ref = ref 3
- and compteur : int ref = ref 1 in
- (
- while !compteur <= 100
- do
- print_int(!v) ;
- print_string(" ") ;
- v := !v + 3;
- compteur := !compteur +1;
- done ;
- print_newline()
- )
- ;;
- print_mult3bis();;
- let print_mult3inf100() : unit =
- let v : int ref = ref 3 in
- (
- while !v < 100
- do
- print_int(!v) ;
- print_string(" ") ;
- v := !v + 3
- done ;
- print_newline()
- )
- ;;
- (* exo en autonomie *)
- (* exo 18 *)
- let classmarks() : int * int * int * int =
- let nb : int ref = ref 0
- and v : int ref = ref 0
- and min : int ref = ref 20
- and max : int ref = ref 0
- and sum : int ref = ref 0
- in
- (
- while !v <> -1
- do
- print_string("saisissez une note : ") ;
- v := read_int() ;
- if !v >= 0 && !v <= 20
- then
- (
- nb := !nb + 1 ;
- if !v < !min
- then min := !v ;
- if !v > !max
- then max := !v ;
- sum := !sum + !v
- )
- else ()
- done ;
- (!nb, !max, !min, !sum / !nb)
- )
- ;;
- (* exo 19 *)
- type t_cow = {prod : float ; milk : float ; us : float} ;;
- (* simulation de la production d'une vache *)
- let simulcow() : t_cow =
- let cowprod : float ref = ref 0.0
- and cowmilk : float ref = ref 0.0
- and cowus : float ref = ref 0.0
- in
- (
- cowprod := float_of_int(rand_int(18, 22)) ;
- if rand_int(1, 120) <= 10
- then cowmilk := !cowprod /. 2.0
- else cowmilk := !cowprod ;
- if rand_int(1, 100) <= 12
- then cowus := 0.0
- else cowus := !cowmilk ;
- {prod = !cowprod ; milk = !cowmilk ; us = !cowus}
- )
- ;;
- (* simulation de la production de n vaches *)
- let simulncow(n : int) : t_cow =
- let cowprod : float ref = ref 0.0
- and cowmilk : float ref = ref 0.0
- and cowus : float ref = ref 0.0
- and cow : t_cow ref = ref ({prod = 0.0 ; milk = 0.0 ; us = 0.0})
- in
- (
- for j = 1 to n
- do
- cow := simulcow() ;
- cowprod := !cowprod +. (!cow).prod ;
- cowmilk := !cowmilk +. (!cow).milk ;
- cowus := !cowus +. (!cow).us
- done ;
- {prod = !cowprod ; milk = !cowmilk ; us = !cowus}
- )
- ;;
- (* simulation de la production de n vaches sur 30 jours *)
- let simul30(n : int) : t_cow =
- let cowprod : float ref = ref 0.0
- and cowmilk : float ref = ref 0.0
- and cowus : float ref = ref 0.0
- and cow : t_cow ref = ref ({prod = 0.0 ; milk = 0.0 ; us = 0.0})
- in
- (
- for i = 1 to 30
- do
- cow := simulncow(n) ;
- cowprod := !cowprod +. (!cow).prod ;
- cowmilk := !cowmilk +. (!cow).milk ;
- cowus := !cowus +. (!cow).us
- done ;
- {prod = !cowprod ; milk = !cowmilk ; us = !cowus}
- )
- ;;
- (* saisie d'un nombre positif *)
- let get_intpos() : int =
- let v : int ref = ref (-1) in
- (
- while !v < 0
- do
- print_string(" saisissez un nombre positif : ") ;
- v := read_int()
- done ;
- !v
- )
- ;;
- (* calcul du temps pour produire un certain nombre de reblochons *)
- (* avec le lait de n vaches *)
- let calcreb(n : int) : int =
- let nbreb : int = get_intpos()
- and cowus : float ref = ref 0.0
- and nbday : int ref = ref 0
- and cow : t_cow ref = ref ({prod = 0.0 ; milk = 0.0 ; us = 0.0})
- in
- let req : float = float_of_int(nbreb * 4)
- in
- (
- while !cowus < req
- do
- nbday := !nbday + 1 ;
- cow := simulncow(n) ;
- cowus := !cowus +. (!cow).us
- done ;
- !nbday ;
- )
- ;;
- (* exo 20 *)
- #use "APutil.ml" ;;
- open_graph(1000, 700) ;;
- (* type pour representer des points *)
- type t_point = {x : int ; y : int} ;;
- (* fonctions de l'exercice 4, modifiee *)
- let generate_point(n : int) : t_point =
- {x = rand_int(-n, n-1) ; y = rand_int(-n, n-1)}
- ;;
- let is_inside_circle(p, n : t_point * int) : bool =
- (p.x * p.x + p.y * p.y) < n * n
- ;;
- let compute_pi(nb, n : int * int) : float =
- let incircle : int ref = ref 0
- and p : t_point ref = ref({x = 0 ; y = 0})
- in
- (
- draw_rect(20, 20, 2 * n, 2 * n) ;
- draw_circle(n + 20, n + 20, n) ;
- for i = 1 to nb
- do
- p := generate_point(n) ;
- if is_inside_circle(!p, n)
- then
- (
- set_color(red) ;
- incircle := !incircle + 1
- )
- else set_color(green) ;
- plot((!p).x + n + 20, (!p).y + n + 20)
- done ;
- 4.0 *. (float_of_int(!incircle) /. float_of_int(nb))
- )
- ;;
- (* exo 21 *)
- let draw_sin(n : int) : unit =
- let pi : float = 3.141592653589793 in
- let ang : float ref = ref 0.0
- and x : float ref = ref 0.0
- and y : float ref = ref 0.0
- in
- let dang : float = pi /. float_of_int(n)
- in
- (
- for i = 1 to n
- do
- ang := !ang +. dang ;
- x := cos(!ang) ;
- y := sin(!ang)
- done
- )
- ;;
- let draw_sin(n : int) : unit =
- let pi : float = 3.141592653589793
- and dx : int = 200
- and dy : int = 200
- in
- let ang : float ref = ref 0.0
- and y : float ref = ref 0.0
- in
- let dang : float = (2.0 *. pi) /. float_of_int(n)
- in
- (
- moveto(dx + int_of_float(100.0 *. !ang), dy + int_of_float(100.0 *. sin(!ang)));
- for i = 1 to n
- do
- ang := !ang +. dang ;
- y := sin(!ang) ;
- lineto(dx + int_of_float(100.0 *. !ang), dy + int_of_float(100.0 *. !y))
- done
- )
- ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement