剪辑:计算年龄

时间:2017-04-06 05:57:11

标签: date clips

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

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

1 个答案:

答案 0 :(得分: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>