算和平均

时间:2014-04-22 20:49:25

标签: clips

具有

(deffacts people
(customer (name a) (money 20) )
(customer (name b) (money 30) )
(customer (name c) (money 40) )
)

我想计算平均金额。我如何计算我拥有的客户数量,平均值?

1 个答案:

答案 0 :(得分:1)

CLIPS> (deftemplate customer (slot name) (slot money))
CLIPS> 
(deffacts people
   (customer (name a) (money 20))
   (customer (name b) (money 30))
   (customer (name c) (money 40)))
CLIPS> (reset)
CLIPS> 
(defrule average
   (exists (customer))
   =>
   (bind ?count 0)
   (bind ?sum 0)
   (do-for-all-facts ((?f customer)) TRUE
      (bind ?count (+ ?count 1))
      (bind ?sum (+ ?sum ?f:money)))
   (printout t "There are " ?count " customers with average money of " (/ ?sum ?count) crlf))
CLIPS> (reset)
CLIPS> (run)
There are 3 customers with average money of 30.0
CLIPS>