Advertisement
Guest User

L6

a guest
Dec 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.14 KB | None | 0 0
  1. (* zadanie 1 *)
  2.  
  3.  
  4. let zgadnij() =
  5.   begin
  6.     print_string "Podaj liczbe od 0 do 100: ";
  7.     let wylosowana = Random.int 101
  8.     and proba = ref (read_int()) in
  9.     while wylosowana <> !proba do
  10.       if wylosowana > !proba
  11.       then
  12.         print_string "Moja wieksza"
  13.       else
  14.         print_string "Moja mniejsza";
  15.      
  16.       print_newline();
  17.       proba := read_int()  
  18.     done;
  19.     print_string "Gratulacje!";
  20.     print_newline ()
  21.   end;;
  22.  
  23. (* zadanie 2 *)
  24. type 'a bt = Empty | Node of 'a * 'a bt * 'a bt;;
  25.  
  26. let t = Node(1, Node(2, Empty, Node(3, Empty, Empty)), Empty);;
  27.  
  28. let  printBT bt =
  29.   let rec aux bt iter =
  30.     let printkropki iter =
  31.       for i = 0 to iter do
  32.         print_string "..."
  33.       done;
  34.     in        
  35.     begin
  36.       match bt with
  37.         Empty -> printkropki (iter - 1);
  38.                  print_string "||";
  39.       | Node(a, bt1, bt2) -> aux bt2 (iter+1);
  40.                              print_newline();
  41.                              printkropki (iter -1);
  42.                              print_int a;
  43.                              print_newline();
  44.                              aux bt1 (iter+1);
  45.     end
  46.   in
  47.   aux bt 0;
  48.   print_newline();;
  49.  
  50. printBT t;;
  51. printBT Empty;;
  52.  
  53.  
  54.  
  55. (* zadanie 3 *)
  56.  
  57. let sortuj_plik() =
  58.   begin
  59.     print_string "Podaj nazwe pliku: ";
  60.     let filename = ref (read_line())
  61.     and filenameOut = "result.txt" in
  62.     let inputFile = open_in !filename in
  63.     let numOfElements = ref (int_of_string (input_line inputFile)) in
  64.     let elements = ref (Array.make !numOfElements 0)
  65.     and i = ref 0 and j = ref 0 in
  66.     while !numOfElements > 0 do
  67.       !elements.(!i) <- int_of_string (input_line inputFile);
  68.       i := !i + 1;
  69.       numOfElements := !numOfElements - 1;
  70.     done;
  71.     close_in inputFile;
  72.     let xs = ref (Array.to_list !elements) in
  73.     xs := List.sort compare !xs;
  74.     elements := Array.of_list !xs;
  75.     let outputFile = open_out filenameOut in
  76.     while !i > 0 do
  77.       output_string outputFile (string_of_int !elements.(!j));
  78.       output_string outputFile " ";
  79.       j := !j + 1;
  80.       i := !i - 1;
  81.     done;
  82.     close_out outputFile;
  83.   end;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement