home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / TESTS / LF / MACHINE.LF < prev    next >
Text File  |  1996-06-04  |  4KB  |  233 lines

  1. :: boisson(prix => 5).
  2.  
  3. gini <| boisson.
  4. perrier <| boisson.
  5. oasis <| boisson.
  6. coca <| boisson.
  7. coca_light <| boisson.
  8.  
  9. cake <| snack.
  10. lion <| snack.
  11. bounty <| snack.
  12. raider <| snack.
  13. yes <| snack.
  14.  
  15. snack <| a_vendre.
  16. boisson <| a_vendre.
  17.  
  18. :: cake(prix => 3).
  19. :: lion(prix => 3).
  20. :: bounty(prix => 3.5).
  21. :: raider(prix => 3.5).
  22. :: yes(prix => 4).
  23.  
  24.  
  25. :: piece.
  26.  
  27. piece <| objet.
  28. boisson <| objet.
  29. snack <| objet.
  30.  
  31. :: objet(quantite => N:int, prix => real).
  32.  
  33. dynamic(machine_contient)?
  34.  
  35. machine_contient(
  36.     [
  37.     cake(quantite => 3),
  38.     lion(quantite => 7),
  39.     bounty(quantite => 2),
  40.     raider(quantite => 3),
  41.     yes(quantite => 7),
  42.     gini(quantite => 2),
  43.     perrier(quantite => 9),
  44.     oasis(quantite => 2),
  45.     coca(quantite => 1),
  46.     coca_light(quantite => 1),
  47.     piece(prix => 0.5,quantite => 1),
  48.     piece(prix => 1,quantite => 3),
  49.     piece(prix => 2,quantite => 2),
  50.     piece(prix => 5,quantite => 2),
  51.     piece(prix => 10,quantite => 1)
  52.     ]).
  53.  
  54. machine :- 
  55.     write('Distributeur de Snacks et Boissons de DEC PRL.'),
  56.     nl,
  57.     nl,
  58.     machine_contient(Contenu:list),
  59.     write('La machine contient:'),
  60.     nl,
  61.     nl,
  62.     affiche(Contenu),
  63.     nl,
  64.     lire(Commande),
  65.     write('Vous avez commande un ',root_sort(Commande:a_vendre(prix => P))),
  66.     ajouter(root_sort(Commande) & @(prix => P,quantite => -1)),
  67.     nl,
  68.     nl,
  69.     write('Vous devez payer ',P,'F.'),
  70.     nl,
  71.     nl,
  72.     payer(P,Commande),
  73.     nl,
  74.     write('-----------------------------------------------------'),
  75.     nl,
  76.     fail.
  77.  
  78. machine :- machine.
  79.  
  80.  
  81. lire(C:a_vendre(quantite => 1)) :-
  82.     machine_contient(Choses),
  83.     repeter,
  84.     (
  85.         write('Que voulez vous acheter ? '),
  86.         read_halt(C),
  87.         nl,
  88.         dans(C,Choses),
  89.         !
  90.     ;
  91.         write('Il n''y en a pas a vendre...'),
  92.         nl,
  93.         fail
  94.     ).
  95.  
  96.  
  97. payer(N:real,C) :-
  98.     N<0,
  99.     !,
  100.     (
  101.         trouver_pieces(-N,L),
  102.         !,
  103.         write('La machine rend:'),
  104.         rendre_pieces(L)
  105.     ;
  106.         write('Il n''y a pas assez de monnaie...'),
  107.         nl,
  108.         rendre_le_plus(-N-0.5)
  109.     ),
  110.     nl,
  111.     write('Prenez votre ',root_sort(C),' dans le distributeur SVP.'),
  112.     nl,
  113.     nl,
  114.     ! .
  115.  
  116. rendre_le_plus(N) :-
  117.     N=<0,
  118.     write('La machine ne vous rend RIEN !!!'),
  119.     nl,
  120.     ! .
  121.  
  122. rendre_le_plus(N) :-
  123.     trouver_pieces(N,L),
  124.     !,
  125.     write('La machine ne peut rendre que ',N,
  126.           'F au mieux, soit:'),
  127.     rendre_pieces(L),
  128.     ! .
  129.  
  130. rendre_le_plus(N) :-
  131.     rendre_le_plus(N-0.5).    
  132.  
  133.  
  134. payer(0,C) :- 
  135.     write('Le compte est bon.'),
  136.     nl,
  137.     write('Veuillez prendre votre ',root_sort(C),' dans le distributeur...'),
  138.     nl,
  139.     nl,
  140.     ! .
  141.  
  142. payer(P,C) :-
  143.     write('Donnez une piece de F ? '),
  144.     read_halt(N:real),
  145.     N={0.5;1;2;5;10},
  146.     !,
  147.     ajouter(piece(prix => N,quantite => 1)),
  148.     payer(P-N,C).
  149.  
  150. payer(P,C) :-
  151.     write('Inacceptable.'),
  152.     nl,
  153.     write('Recommencez.'),
  154.     nl,
  155.     payer(P,C).
  156.  
  157.  
  158. ajouter(C) :-
  159.     machine_contient(Choses),
  160.     retract(machine_contient),
  161.     mettre(C,Choses,Contenu),
  162.     assert(machine_contient(Contenu)).
  163.  
  164.  
  165. mettre( C:@(prix => P,quantite => R),
  166.     [A:@(prix => P,quantite => Q)|Reste],
  167.     [B:@(prix => P,quantite => Q+R)|Reste]) :- 
  168.     A=root_sort(C),
  169.     B=root_sort(C),
  170.     !.
  171.  
  172. mettre(C,[A|B],[A|B2]) :- mettre(C,B,B2).
  173.  
  174.  
  175. trouver_pieces(N,somme_egale(piece_dans(Choses),N)) :- 
  176.     machine_contient(Choses).
  177.  
  178.  
  179. piece_dans([]) -> [].
  180. piece_dans([piece(prix => P,quantite => Q)|B]) ->
  181.     apres(piece_dans(B),Q,P).
  182. piece_dans([A|B]) -> piece_dans(B).
  183.  
  184. apres(C,0,A) -> C.
  185. apres(C,N,P) -> apres([P|C],N-1,P).
  186.  
  187.     
  188.     
  189. somme_egale(list,0) -> [].
  190. somme_egale([],N) -> {}.
  191. somme_egale([A|B],N) -> {[A|somme_egale(B,N-A)];somme_egale(B,N)}.
  192.  
  193.  
  194.  
  195. rendre_pieces([]) :- nl.
  196. rendre_pieces([N|Reste]) :-
  197.     ajouter(piece(prix => N,quantite => -1)),
  198.     write(' ',N,'F'),
  199.     rendre_pieces(Reste).
  200.     
  201.  
  202.  
  203. affiche([]) :- ! .
  204. affiche([A:@(quantite => 0)|B]) :-
  205.     !,
  206.     affiche(B).
  207.  
  208. affiche([A|B]) :-
  209.     write('       ',A),
  210.     nl,
  211.     affiche(B).
  212.  
  213. dans(A,[ B:@(quantite => N:int) | @]) :-
  214.     N>0,
  215.     A=root_sort(B).
  216.  
  217. dans(A,[B|C]) :- dans(A,C).
  218.  
  219. repeter.
  220. repeter :- 
  221.     write('Recommencez...'),
  222.     nl,
  223.     repeter.
  224.  
  225. read_halt(C) :-
  226.     read_token(D),
  227.     end_check(D, C).
  228.  
  229. end_check(end_of_file, _) :- halt.
  230. end_check(C, C).
  231.  
  232. machine ?
  233.