嵌套多个如果是else语句

发布于 2025-01-21 21:46:11 字数 1432 浏览 3 评论 0原文

我正在尝试制定一个剪辑程序,以吸收患者和捐助者的血型,并在医生输血前进是否返回。当我加载程序时,我在最后会得到一个错误的标志。有其他方法可以嵌套在当时的语句中吗?

; define template
    
(deftemplate blood-type
   (slot blood))
    
(deftemplate patients
   (slot receiver)
   (slot donor))
        
; reading input
    
(defrule reading-input    
   => 
   (printout t "Enter the receiver/patient blood type: ")  
   (assert (receiver (read))) 
   (printout t "Enter the donor's blood type: ") 
   (assert (donor (read))))
        
; checking for valid input
    
(defrule checking-input    
   (receiver ?receiver)
   (donor ?donor) 
   => 
   (if (and (eq ?receiver O) (eq ?donor O)) 
      then     
      if (and (eq ?receiver A) (eq ?donor A))
        then
        (if (and (eq ?receiver A)(eq ?donor O))
           then
           (if (and (eq ?receiver B) (eq ?donor B))
              then
              (if (and (eq ?receiver B) (eq ?donor O))
                 then
                 (if (and (eq ?receiver AB) (eq ?donor AB))
                    then
                    (if (and (eq ?receiver AB) (eq ?donor A))
                       then
                       (if (and (eq ?receiver AB) (eq ?donor B))   
                          then
                          (if (and (eq ?receiver AB) (eq ?donor O))
                             then (printout t "Continue with Transfusion" crlf)
                             else (printout t "Stop transfusion" crlf))))))))))

I'm trying to make a CLIPS program that takes in a patient's and donor's blood type and returns if the doctor should go forward with the transfusion. When I load in the program, I get a FALSE flag at the very end. Is there another way that I can nest in the If-Then statements?

; define template
    
(deftemplate blood-type
   (slot blood))
    
(deftemplate patients
   (slot receiver)
   (slot donor))
        
; reading input
    
(defrule reading-input    
   => 
   (printout t "Enter the receiver/patient blood type: ")  
   (assert (receiver (read))) 
   (printout t "Enter the donor's blood type: ") 
   (assert (donor (read))))
        
; checking for valid input
    
(defrule checking-input    
   (receiver ?receiver)
   (donor ?donor) 
   => 
   (if (and (eq ?receiver O) (eq ?donor O)) 
      then     
      if (and (eq ?receiver A) (eq ?donor A))
        then
        (if (and (eq ?receiver A)(eq ?donor O))
           then
           (if (and (eq ?receiver B) (eq ?donor B))
              then
              (if (and (eq ?receiver B) (eq ?donor O))
                 then
                 (if (and (eq ?receiver AB) (eq ?donor AB))
                    then
                    (if (and (eq ?receiver AB) (eq ?donor A))
                       then
                       (if (and (eq ?receiver AB) (eq ?donor B))   
                          then
                          (if (and (eq ?receiver AB) (eq ?donor O))
                             then (printout t "Continue with Transfusion" crlf)
                             else (printout t "Stop transfusion" crlf))))))))))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2025-01-28 21:46:11

您的条件筑巢非常复杂。您可以在没有任何嵌套的情况下获得正确的结果:

         CLIPS (6.4 2/9/21)
CLIPS>
(defrule read-and-check-input    
   => 
   (printout t "Enter the receiver/patient blood type: ")  
   (bind ?receiver (read)) 
   (printout t "Enter the donor's blood type: ") 
   (bind ?donor (read))
   (if (or (and (eq ?receiver O) (eq ?donor O)) 
           (and (eq ?receiver A) (eq ?donor A))
           (and (eq ?receiver A)(eq ?donor O))
           (and (eq ?receiver B) (eq ?donor B))
           (and (eq ?receiver B) (eq ?donor O))
           (and (eq ?receiver AB) (eq ?donor AB))
           (and (eq ?receiver AB) (eq ?donor A))
           (and (eq ?receiver AB) (eq ?donor B))   
           (and (eq ?receiver AB) (eq ?donor O)))
      then (printout t "Continue with Transfusion" crlf)
      else (printout t "Stop transfusion" crlf)))
CLIPS> (reset)
CLIPS> (run)
Enter the receiver/patient blood type: A
Enter the donor's blood type: O
Continue with Transfusion
CLIPS> (reset)
CLIPS> (run)
Enter the receiver/patient blood type: B
Enter the donor's blood type: AB
Stop transfusion
CLIPS>

如果这是用于课堂分配的,则可能会使用不使用有意义的方式使用模式匹配的方法获得失败的成绩。

Your nesting of conditions is unnecessarily complex. You can achieve the correct results without any nesting:

         CLIPS (6.4 2/9/21)
CLIPS>
(defrule read-and-check-input    
   => 
   (printout t "Enter the receiver/patient blood type: ")  
   (bind ?receiver (read)) 
   (printout t "Enter the donor's blood type: ") 
   (bind ?donor (read))
   (if (or (and (eq ?receiver O) (eq ?donor O)) 
           (and (eq ?receiver A) (eq ?donor A))
           (and (eq ?receiver A)(eq ?donor O))
           (and (eq ?receiver B) (eq ?donor B))
           (and (eq ?receiver B) (eq ?donor O))
           (and (eq ?receiver AB) (eq ?donor AB))
           (and (eq ?receiver AB) (eq ?donor A))
           (and (eq ?receiver AB) (eq ?donor B))   
           (and (eq ?receiver AB) (eq ?donor O)))
      then (printout t "Continue with Transfusion" crlf)
      else (printout t "Stop transfusion" crlf)))
CLIPS> (reset)
CLIPS> (run)
Enter the receiver/patient blood type: A
Enter the donor's blood type: O
Continue with Transfusion
CLIPS> (reset)
CLIPS> (run)
Enter the receiver/patient blood type: B
Enter the donor's blood type: AB
Stop transfusion
CLIPS>

If this is for a class assignment, you might get a failing grade using an approach that doesn't utilize pattern matching in a meaningful way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文