2017-04-06 137 views
0

我需要一个计算年龄的函数。我刚刚开始学习CLIPS。我可以计算几年,但答案是错误的。剪辑:计算年龄

(defrule calc-age 
(person (date-of-birth ?dob)) 
=> 
    (bind ?age (- 2017 ?dob)) 
(printout t ?age)) 

回答

0
CLIPS> 
(deffunction age (?cy ; current year 
        ?cm ; current month 
        ?cd ; current day 
        ?by ; birth year 
        ?bm ; birth month 
        ?bd) ; birth day 
    (bind ?age (- ?cy ?by)) 
    (if (or (< ?cm ?bm) 
      (and (= ?cm ?bm) (< ?cd ?bd))) 
     then 
     (bind ?age (- ?age 1))) 
    ?age) 
CLIPS> (age 2017 4 6 2017 3 2) 
0 
CLIPS> (age 2017 4 6 2016 8 3) 
0 
CLIPS> (age 2017 4 6 2016 4 3) 
1 
CLIPS> (age 2017 4 6 2016 3 3) 
1 
CLIPS> (age 2017 4 6 2015 3 3) 
2 
CLIPS> 
+0

非常感谢您! –