usecase
This commit is contained in:
parent
96f15c7045
commit
c97e4edc84
3 changed files with 27 additions and 12 deletions
|
@ -51,7 +51,7 @@ let read_comment graph line l_id=
|
|||
|
||||
(* Reads a line with a user. *)
|
||||
let read_user id graph l_id line =
|
||||
try Scanf.sscanf line "u %s" (fun user l_id-> ((init_node graph user id), l_id) )
|
||||
try Scanf.sscanf line "u %s" (fun user-> (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"
|
||||
|
@ -59,7 +59,7 @@ let read_user id graph l_id line =
|
|||
(* Reads a line with a payement. *)
|
||||
let read_payement graph line l_id=
|
||||
try Scanf.sscanf line "p %s %s %f"
|
||||
(fun u l_u label -> ((paiement graph u (String.split_on_char ',' l_u) label), l_id))
|
||||
(fun user l_user label -> ((paiement graph user (String.split_on_char ',' l_user) label, l_id)
|
||||
with e ->
|
||||
Printf.printf "Cannot read arc in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
|
||||
failwith "from_file"
|
||||
|
@ -83,7 +83,7 @@ let from_file path =
|
|||
|
||||
(* The first character of a line determines its content : n or e. *)
|
||||
else match line.[0] with
|
||||
| 'u' -> (n+1, (read_user n graph l_id line))
|
||||
| 'u' -> (n+1, read_user n graph l_id line)
|
||||
| 'p' -> (n, read_payement graph line l_id)
|
||||
|
||||
(* It should be a comment, otherwise we complain. *)
|
||||
|
|
|
@ -4,26 +4,37 @@ open Tool
|
|||
|
||||
(*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))
|
||||
((new_node g id), ((user,id,0)::l_id))
|
||||
|
||||
|
||||
(*fonction qui renvoie l'id d'un utilisateur*)
|
||||
let rec get_id utilisateur l_id= match l_id with
|
||||
|[]-> raise Not_found
|
||||
|(a,id1)::b-> if a=utilisateur then id1 else get_id utilisateur b
|
||||
|(a,id1,val)::b-> if a=utilisateur then id1 else get_id utilisateur b
|
||||
|
||||
(*fonction qui renvoie le nom correspondant à un id*)
|
||||
let rec 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
|
||||
|(nom,a,val)::b-> if a=id1 then nom else get_user id1 b
|
||||
|
||||
let set_val_du a l_id montant l_utilisateurs=
|
||||
List.map (fun (nom,id,val)-> if nom=a
|
||||
then (nom,id,(Float.sub val (Float.div montant (Float.of_int(List.length l_utilisateurs)))))
|
||||
else (nom,id,val)
|
||||
) l_id
|
||||
|
||||
let set_val_pret utilisateur montant l_id=
|
||||
List.map (fun (nom,id,val)-> if nom=utilisateur
|
||||
then (nom,id,(Float.add val montant))
|
||||
else (nom,id,val)
|
||||
) l_id
|
||||
|
||||
(*fonction qui rentre les paiements réalisés*)
|
||||
let rec paiement g utilisateur l_utilisateurs montant l_id= match l_utilisateurs with
|
||||
let rec paiement g utilisateur l_utilisateurs montant l_id=
|
||||
set_val_pret utilisateur montant l_id
|
||||
match l_utilisateurs with
|
||||
|[]-> (g, l_id)
|
||||
|a::b-> if not(a=utilisateur)
|
||||
then paiement (add_arc g (get_id utilisateur l_id) (get_id a l_id) (Float.div montant (Float.of_int(List.length l_utilisateurs)))) utilisateur b montant l_id
|
||||
else paiement g utilisateur b montant l_id
|
||||
|
||||
|a::b-> paiement g utilisateur b montant (set_val_du a l_id montant l_utilisateurs)
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -6,4 +6,8 @@ val init_node: float graph -> string ->id -> (string * id) list-> (float graph *
|
|||
|
||||
val get_id: string -> (string * id) list -> id
|
||||
|
||||
val get_user: id -> (string * id) list -> string
|
||||
val get_user: id -> (string * id) list -> string
|
||||
|
||||
val set_val_du:
|
||||
|
||||
val set_val_pret:
|
Loading…
Reference in a new issue