乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 100|回复: 12

[编程交流] “如果”。。。。“和”。。。。。“或”。。。。

[复制链接]

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 17:24:00 | 显示全部楼层 |阅读模式
我目前正在使用LISP绘制管道弯头的平面图。
 
在绘图程序运行之前,我需要LISP分析用户输入的角度,以确定其是否等于90度或180度。
 
如果角度不等于其中任何一个,则程序需要按计划运行。
 
我试过用这样的东西。。。。
 
然而,我真的不明白“OR”、“AND”、“WHILE”和“NOT”函数是如何工作的。
 
回复

使用道具 举报

54

主题

3755

帖子

3583

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
438
发表于 2022-7-6 17:31:55 | 显示全部楼层
或者:如果是这个或那个,那么做一些事情(必须是其中之一)
并且:如果这个和那个,那么做一些事情(必须两者都做)
WHILE:WHILE something(=(getvar“cmdecho”0)),然后执行操作
不:如果某事不是某事,那么做某事
(如果(不等于(getvar“clayer”)0)
);不
(setvar“clayer”0)
);如果
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 17:35:35 | 显示全部楼层
感谢Alanjt提供的信息,非常感谢;但是你能看到我发布的代码中关于“or”的使用有什么错误吗(除了明显的no“defun”等)?
回复

使用道具 举报

54

主题

3755

帖子

3583

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
438
发表于 2022-7-6 17:39:49 | 显示全部楼层
 
试试这个:
你有一个反斜杠。
回复

使用道具 举报

CAB

29

主题

781

帖子

430

银币

中流砥柱

Rank: 25

铜币
526
发表于 2022-7-6 17:46:01 | 显示全部楼层
李·麦克
有很多方法可以解决这个问题,但在找到解决方案之前,您需要
澄清流程。在您的示例中,您使用的是整数90和180。对我来说这意味着
它们是用户输入的值,因为从实体(如直线)获得的任何角度
将是一个实数&可能以弧度而不是度为单位。
那是哪一个?
 
请注意,对于接近所需值的值,通常也必须使用模糊因子。
由于存在舍入误差,ACAD尤其如此。
 
这是您的示例已更正&没有给出正确的结果,您使用了错误的斜杠字符
  1. (if (or (/= elbang 90) (/= elbang 180))
  2.    (progn
  3.        "run elbow draw program"
  4.    )
  5. ) ; end if

 
在您给出的简单示例中,我会这样做,假设只测试整数。
  1. ;;  elbang is /= 90 AND /= 180
  2. (if (and (/= elbang 90) (/= elbang 180))
  3.    (progn
  4.       (princ "run elbow draw program")
  5.    )
  6. ) ; end if

 
  1. ;;  OR returns true if either is true
  2. ;;  OR returns nil if Both are false
  3. ;;  so if Both are false, NOT returns True
  4. (if (not (or (= elbang 90) (= elbang 180)))
  5.    (progn
  6.      (princ "run elbow draw program")
  7.    )
  8. ) ; end if

 
  1. ;; Another way to test
  2. ;;  if var is not a member of the list
  3. (if (not (member elbang '(90 180)))
  4.    (progn
  5.      (princ "run elbow draw program")
  6.    )
  7. ) ; end if

 
  1. ;; Another way to test
  2. ;;  if var is not a member of the list
  3. (if (not (vl-position elbang '(90 180)))
  4.    (progn
  5.      (princ "run elbow draw program")
  6.    )
  7. ) ; end if

 
  1. ;; This one is a little tricky
  2. ;;  returns True if both test are true, like this
  3. ;;  (setq elbang 20)
  4. ;;  (/= 90 elbang) -> true
  5. ;;  (/= elbang 180) -> true
  6. ;;  both true then the prg will execute
  7. ;;  (setq elbang 90)
  8. ;;  (/= 90 elbang) -> flase
  9. ;;  (/= elbang 180) -> true
  10. ;;  any false will return false then the prg will NOT execute
  11. ;;  This is like an AND,
  12. (if (/= 90 elbang 180) ; elbang must be in the middle
  13.    (progn
  14.      (princ "run elbow draw program")
  15.    )
  16. ) ; end if
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 17:53:09 | 显示全部楼层
谢谢大家提供的信息,(CAB,你真的带着最后一篇帖子进城了-非常有信息,谢谢)。
 
现在我对函数的理解好多了。
 
我意识到我的错误与张贴Lisp程序,但谢谢你花时间指出它。
 
为了更好地阐明我的意图-
运行初始“用户输入程序”以获得用户值,包括
[列表]
  • 设备类型
  • 视图(平面/立面)
  • 插入点
  • 管道标称孔径(50/80/100/…等)
  • (用于弯头)-半径(长/短/5D)
  • (用于弯头)-角度
    根据输入的设备类型,各种其他程序将运行并根据所选视图和尺寸构建所选设备。
     
    为了解决使用“and/or”命令的问题,我对每个场景进行了限定,并使用“if”函数来分隔每个场景。
     

     
    1. (if (and (/= elbang 90.0) (< elbang 180.0))
    2.     (progn
    3.        "draw elbow"
    4.     ) ; end progn
    5. ) ; end if
    6. (if (= elbang 90.0)
    7.    (progn
    8.       "draw 90.0 elbow"
    9.    ) ; end progn
    10. ) ; end if
    11. etc etc

    这似乎很有效。
    再次感谢你的帮助。
  • 回复

    使用道具 举报

    CAB

    29

    主题

    781

    帖子

    430

    银币

    中流砥柱

    Rank: 25

    铜币
    526
    发表于 2022-7-6 17:54:21 | 显示全部楼层
    很高兴我能解释一下这个问题。
    当您有几个测试并且只有一个是真的时,或者如果在中进行测试,您希望其中一个是真的
    一个特定的顺序,那么COND就是要使用的函数。请参见此示例。
    1. (cond
    2. ((equal elbang 90.0 0.0001) ; this will catch rounding errors
    3.   (princ "draw 90.0 elbow") ; go do it
    4. )
    5. ((< 90.0 elbang 180.0) ; it is between 90 and 180
    6.   (princ "draw elbow")
    7. )
    8. (t ; elbang is < 90 OR >= 180
    9.   ;; Do what ever
    10. )
    11. )
    回复

    使用道具 举报

    114

    主题

    1万

    帖子

    1万

    银币

    中流砥柱

    Rank: 25

    铜币
    543
    发表于 2022-7-6 18:03:07 | 显示全部楼层
    我假设“cond”函数比“if”函数整洁——尽管我发现“if”函数仍然可以产生所需的结果。我不知道为此目的使用这样的函数有任何缺点。
     
    还有一个问题我想问-它已经在我脑海里一段时间了。。。
     
    “progn”前缀的确切含义是什么?
     
    起初,我认为“progn”前缀专门用于“if”函数,以通知程序在“if”命令决定如何处理数据之前,将有多行编程。然而,在前一个线程中,我注意到您使用了带有“while”函数的“progn”前缀。。。
    回复

    使用道具 举报

    CAB

    29

    主题

    781

    帖子

    430

    银币

    中流砥柱

    Rank: 25

    铜币
    526
    发表于 2022-7-6 18:06:25 | 显示全部楼层
    嗯,COND是这项工作的正确工具。只是选择的问题。
     
    PROGN是一组代码的包装器。
     

    计算一个测试表达式,如果它不是零,则计算其他表达式;
    重复此过程,直到测试表达式的计算结果为零
     
    PROGN使整个组成为测试表达式。
     
    在WHILE循环中使用的其他方法是使用COND或AND
     
    如果将一组代码包装在中,则每行都将执行,直到出现零。在这一点上
    并以零退出返回到退出循环的WHILE。
     
    COND的使用方法相同。
    回复

    使用道具 举报

    CAB

    29

    主题

    781

    帖子

    430

    银币

    中流砥柱

    Rank: 25

    铜币
    526
    发表于 2022-7-6 18:12:43 | 显示全部楼层
    LISP Logic and More作者:Charles Alan Butler又名CAB
     
    这是我对LISP逻辑函数及其用法的看法。
     
    与我一起探索if and or cond while&progn函数。
     
    if语句的工作方式如下
    1. (if (this is true)
    2. (then do this)
    3. )

     
    1. (if (this is true)
    2. (then do this)
    3. (else do this when false)
    4. )

     
     
     
    另一方面,(cond)stmt执行以下所有代码
    然后退出条件stmt。它返回最后一个的值
    子列表中的表达式。如果子列表中只有一个表达式(即,
    如果缺少结果),则返回测试表达式的值。
     
    1. (cond
    2. ((= 1 0)
    3.    none of this will get executed
    4.    because the conditions is false
    5. ) ; end cond 1
    6. ((= 1 1)
    7.     do all of this because it is true
    8.     and this
    9.     and this
    10.   ) ; end cond 2
    11.   ((= 2 2)
    12.     none of this will get
    13.     executed even if it is true
    14.     because the prior stmt was true
    15.   ) ; end cond 3
    16.   (T
    17.     This is often placed at the last position
    18.     in a condition statement and will be executed
    19.     if all the prior conditions are false
    20.     if any one of the above conditions are true
    21.      this will not be executed
    22.   ) ; end cond 4
    23. ) ; end cond stmt

    关于条件陈述的更多信息,请稍候。
     
     
    OR将处理每一行,只要它们为false,当返回true时它将退出
    1. (or (a. is true) ; stop here
    2.     (b. does not get this far)
    3. )

     
    1. (or (a. is false) ; keep going
    2.     (b. do this & if false keep going)
    3.     (c. do this if b was false)
    4. )

     
    和将处理每一行,只要它们为true,当返回false时它将退出
    1. (and (a. is true) ; keep going
    2.      (b. do this & if true keep going)
    3.      (c. do this if b was true)
    4. )

     
    请注意,AND和/或其自身仅返回true或nil,
    而IF和COND将返回表达式的值。
    1. (setq rtn
    2.       (or (+ 3 7) ; returns 10 which is not nil so it passes the test for true
    3.            (+ 2 4 6) ; return 12
    4.       )
    5. )

    而rtn中的值为t
    1. (setq rtn
    2.       (if (+ 3 7) ; returns 10 which is not nil so it passes the test for true
    3.            (+ 2 4 6) ; return 12
    4.       )
    5. )

    rtn中的值为12
    1. (setq rtn
    2.       (if (> 3 7) ; 3 is not > 7 so this returns nil
    3.            (+ 2 4 6) ; return 12
    4.       )
    5. )

    rtn中的值为零,因为没有其他行需要考虑
    1. (setq rtn
    2.       (if (> 3 7) ; 3 is not > 7 so this returns nil
    3.            (+ 2 4 6)
    4.            (- 10 2)
    5.       )
    6. )

    此处无支持,rtn值为8
     
    cond语句与if类似
    这是一个我喜欢的例子
    1. (initget 0 "Yes No")
    2. (setq ans
    3.          (cond
    4.                  ((getkword "\nGive your answer.  [Yes/No] <Yes>: "))
    5.                ("Yes")
    6.          )
    7. )

    如果用户点击回车键,则返回nil,cond返回“Yes”
    如果用户输入文本,则返回文本
     
     
     
     
    (progn)的一些示例
    注意,progn返回最后计算的表达式的值。
    1. (if (this is true)
    2.   (progn
    3.        do all of this
    4.        and this
    5.        and this
    6.    )
    7.   (don't do this)
    8. ); endif
    9. (if (this is false)
    10.   (don't do this)
    11.   (progn
    12.        do all of this
    13.        and this
    14.        and this
    15.    )
    16. )

     
    您可以看到,progn是一种将代码分组在一起的方法。
    当为true且
    当为false时,将执行第二组代码,
    如果有第二组代码。如果你忘记了(progn)只有第一个
    如果为true,则将执行代码行,其余代码将被跳过。
    还有其他方法可以将代码分组在一起,您将及时了解它们。
     
     
     
     
    另一方面,(cond)stmt执行以下所有代码
    然后退出条件stmt。记住,真理不是零。
     
    1. (cond
    2. ((= 1 0)
    3.    none of this will get executed
    4.    because the conditions is false
    5. ) ; end cond 1
    6. ((= 1 1)
    7.     do all of this because it is true
    8.     and this
    9.     and this
    10.   ) ; end cond 2
    11.   ((= 2 2)
    12.     none of this will get
    13.     executed even if it is true
    14.     because the prior stmt was true
    15.   ) ; end cond 3
    16.   (T
    17.     This is often placed at the last position
    18.     in a condition statement and will be executed
    19.     if all the prior conditions are false
    20.     if any one of the above conditions are true
    21.      this will not be executed
    22.   ) ; end cond 4
    23. ) ; end cond stmt

     
     
     
    当您想要有条件地执行cond时,这里有一个cond语句的技巧。
    我们经常看到这样的情况:
    1. (if (something is true) ; reminder, 'true' here is really anything (not nil)
    2. (cond
    3.    ((= 1 1) ...)
    4.    ((= 2 2) ...)
    5. ) ; end cond stmt
    6. ) ; endif

     
    它可以这样写,如果某件事不是真的,那么第一个条件就是真的&
    什么也没做,但cond语句满意地发现了真实条件。
    处理跳过其余条件。
    1. (cond
    2. ((not (something is true))) ; if something is not true stop here
    3. ((= 1 1) ...)
    4. ((= 2 2) ...)
    5. ) ; end cond stmt

     
     
     
    WHILE用作条件循环,并将循环继续到下一个循环
    表达式返回nil。在这里,我们测试cnt中的值,希望它变为0
    这样我们就可以退出while循环。循环中的代码执行5次,
    而cnt值为0到4。
    1. (setq cnt 0)
    2. (while (= cnt 5)
    3. ;;  do some stuff
    4. (setq cnt (1+ cnt))
    5. )

     
    也可以这样写,cnt值为0到4。
    1. (setq cnt -1)
    2. (while (= (setq cnt (1+ cnt)) 5)
    3. ;;  do some stuff
    4. )

     
    有时,要测试的条件是在中的代码末尾设置的
    循环。代码可能如下所示。
    1. (setq continue_loop t)
    2. (while continue_loop
    3. ;;  do some stuff
    4. (if (I want to leave the loop is true)
    5.    (setq continue_loop nil) ; flag to exit the loop
    6. )
    7. )

     
    也可以这样写:
    1. (setq continue_loop t)
    2. (while
    3. (progn
    4. ;;  do some stuff
    5.   (not (I want to leave the loop is true)) ; flag to exit the loop
    6. ) ; progn
    7. )

     
    你看,当(我想让循环为true)返回true时,not将返回true
    返回nil,因为它是progn中的最后一个表达式,所以是
    虽然看到了。然后退出循环。
     
     
    这是一个真实的例子。
    这是我输入数字如关键字的解决方案
    模拟的关键词是“90 180 270-90”
    1. 29

     
     
    现在就这些。
    再见。
    回复

    使用道具 举报

    发表回复

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    • 微信公众平台

    • 扫描访问手机版

    • 点击图片下载手机App

    QQ|关于我们|小黑屋|乐筑天下 繁体中文

    GMT+8, 2025-3-4 15:41 , Processed in 0.475951 second(s), 72 queries .

    © 2020-2025 乐筑天下

    联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表