moneysharing

This commit is contained in:
Leonie Gallois 2020-11-20 15:03:36 +01:00
parent 4e99858ad5
commit df3b14c089
4 changed files with 143 additions and 0 deletions

99
src/MSgfile.ml Normal file
View file

@ -0,0 +1,99 @@
open Graph
open Printf
open MoneySharing
type path = string
(* Format of text files:
% Welcome to MoneySharing, your favorite tool to ease your reimbursements !
% Please, type the name of all users of your group:
u Gaby
u Flo
u Macha
% You can now enter your payements as it follows: p userWhoPaid [forWhichUser1; forWhichUser2 ..] amount
p Flo [Gaby; Flo; Macha] 11.0
p Gaby [Flo] 8.5
*)
let write_file path graph l_id=
(* Open a write-file. *)
let ff = open_out path in
(* Write in this file. *)
fprintf ff "%% Here is your MoneySharing graph.\n\n" ;
(* Write all users *)
n_iter_sorted graph (fun id -> fprintf ff "u %s\n" (get_user id l_id)) ;
fprintf ff "\n" ;
fprintf ff "%% Here are the reimbursements to be made.\n\n" ;
(* Write all arcs *)
e_iter graph (fun id1 id2 lbl -> fprintf ff "p %d %d %s\n" (get_user id1 l_id) (get_user id2 l_id) lbl) ;
fprintf ff "\n%% End of reimbursements\n" ;
close_out ff ;
()
let read_comment graph line l_id=
try Scanf.sscanf line " %%" (graph, l_id)
with _ ->
Printf.printf "Unknown line:\n%s\n%!" line ;
failwith "from_file"
(* Reads a line with a user. *)
let read_user id graph line l_id=
try Scanf.sscanf line "u %s" (fun user l_id-> ((init_node graph user id), l_id) )
with e ->
Printf.printf "Cannot read node in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
failwith "from_file"
(* Reads a line with a payement. *)
let read_payement graph line l_id=
try Scanf.sscanf line "p %s %r %f"
(fun u l_u label -> ((paiement graph u l_u label), l_id))
with e ->
Printf.printf "Cannot read arc in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
failwith "from_file"
let from_file path =
let infile = open_in path in
(* Read all lines until end of file.
* n is the current node counter. *)
let rec loop n graph l_id=
try
let line = input_line infile in
(* Remove leading and trailing spaces. *)
let line = String.trim line in
let (n2, (graph2, l2)) =
(* Ignore empty lines *)
if line = "" then (n, (graph, l_id))
(* The first character of a line determines its content : n or e. *)
else match line.[0] with
| 'u' -> (n+1, read_node n graph line l_id )
| 'p' -> (n, read_arc graph line l_id)
(* It should be a comment, otherwise we complain. *)
| _ -> (n, read_comment graph line l_id)
in
loop n2 graph2 l2
with End_of_file -> (graph, l_id) (* Done *)
in
let final_graph_lid= loop 0 empty_graph [] in
close_in infile ;
final_graph_lid

7
src/MSgfile.mli Normal file
View file

@ -0,0 +1,7 @@
open Graph
type path = string
val from_file: path -> (string graph, (string,id) list)
val write_file: path -> string graph -> (string,id) list-> unit

28
src/moneySharing.ml Normal file
View file

@ -0,0 +1,28 @@
open Graph
(*fonction qui créé le noeud associé à un utilisateur et rentre la correspondance dans la table des id*)
let init_node g user id l_id=
((new_node g id), ((user,id)::l_id))
(*fonction qui renvoie l'id d'un utilisateur*)
let get_id utilisateur l_id= match l_id with
|[]-> raise Not_found
|(a,id1)::b-> if a=utlisateur then id1 else get_id utilisateur b
(*fonction qui renvoie le nom correspondant à un id*)
let get_user id1 l_id= match l_id with
|[]-> raise Not_found
|(nom,a)::b-> if a=id1 then nom else get_user id1 b
(*fonction qui rentre les paiements réalisés*)
let rec paiement g utilisateur l_utilisateurs montant l_id=
let id1=(get_id utilisateur l_id) in
match l_utilisateurs with
|[]-> (g, l_id)
|a::b-> paiement (add_arc g id1 (get_id a l_id) montant) id1 b montant l_id

9
src/moneySharing.mli Normal file
View file

@ -0,0 +1,9 @@
open Graph
val paiement: int graphe -> string -> string list -> float -> (string,id) list -> (int graphe, (string,id) list)
val init_node: int graphe -> string ->id -> (string,id) list-> (int graphe, (string,id) list)
val get_id: string -> (string, id) list -> id
val get_user: id -> (string, id) list -> string