明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1936|回复: 1

[LISP]计算单行文字+、-、*、/的程序

[复制链接]
发表于 2005-11-22 10:10:00 | 显示全部楼层 |阅读模式

  本人前几天找到一个计算单行文字的用LISP语言编的可实现+、-、*、/的程序,加载后从命令行输入JS就行了,共享一下,代码如下:

(defun *error* (ERROR)
     (princ "error:")
     (princ "CAO ZUO ERROE")
     (PRINC "\n please try a time")
     )
(defun getss(/ SS N I NAME0 NAME X0 X1)
     (INITGET (+ 1 2 4))
     (setq ss (ssget '((0 . "TEXT"))))
     (if (= ss nil) (setq ss (ssadd)))
     (setq ssa (ssadd))
     (while (/= (setq n (sslength ss)) 0)
            (progn
            (setq i 1)
            (setq name0 (ssname ss 0))
            (setq x0 (caddr (assoc 10 (entget name0))))
            (while (< i n)
                  (progn
                  (setq name (ssname ss i))
                  (setq X1 (caddr (assoc 10 (entget name))))
                  (if (< X0 X1)
                       (progn
                       (setq name0 name)
                       (setq  X0  X1)
                  ))
                  (setq i (+ 1 i))
              ))
              (setq ssa (ssadd name0 ssa))      
              (setq ss (ssdel name0 ss))
       ))
       )

(DEFUN C:JS(/  NN II ENT NAME TXT PP P0 PP0 NAME1
              P ST ZH ZW ANG I N W TEMP)
     (SETvar "BLIPMODE" 0)
     (SETvar "CMDECHO" 0)
     (PROMPT "\n    FIRST-SSGET:")
     (INITGET (+ 1 2 4))
     (getss)
     (SETQ SS1 ssa)
     (PROMPT "\n    SECOND-SSGET:")
     (getss)
     (SETQ SS2 ssa)
    
     (INITGET (+ 1 2 4))
     (IF (= (SSLENGTH SS2) 0)
         (SETQ JSF (GETSTRING "\n    JI SUAN FU:?<+>  "))
         (SETQ JSF (GETSTRING "\n    JI SUAN FU:?<*>  ")))
     (WHILE (AND (/= JSF "+") (/= JSF "-") (/= JSF "*") (/= JSF "/") (/= JSF ""))
            (GETSTRING "\n    JI SUAN FU:?<*>  "))
     (IF (AND (= (SSLENGTH SS2) 0) (= JSF "")) (SETQ JSF "+"))
     (IF (AND (/= (SSLENGTH SS2) 0) (= JSF "")) (SETQ JSF "*"))
    
     (SETQ P0 (CDR (ASSOC 10 (ENTGET (SSNAME SS1 0)))))
     (INITGET (+ 1 2 4))
     (SETQ PP0 (GETPOINT "\n    TEXT-POINT:?"))
     (SETvar "BLIPMODE" 0)
     (IF (= (SSLENGTH SS2) 0)           
           (PROGN
                 (SETQ XI (GETREAL "\n   XU CHU DE XI SHU [/] :?<1>"))          
                 (IF (= XI NIL) (SETQ XI 1)))
           (PROGN
                 (SETQ XI (GETREAL "\n   XU CHU DE XI SHU [/]:? <100>"))
                 (IF (= XI NIL) (SETQ XI 100))
            ))

     (SETQ WS (GETINT "\n    XIAO SHU WEI:?<2> "))
     (IF (= WS NIL) (SETQ WS 2))
     (SETQ NN1 (SSLENGTH SS1))
     (SETQ II 0)
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     (IF (= (SSLENGTH SS2) 0)(PROGN
         (WHILE (< II NN1)
           (SETQ ENT1 (ENTGET (SETQ NAME1 (SSNAME SS1 II))))
           (IF (= II 0) (SETQ NAME0 NAME1))
           (SETQ TXT1 (CDR (ASSOC 1 ENT1)))
           (SETQ TXT1 (ATOF TXT1))
           (IF (= II 0) (SETQ TXT TXT1))
           (IF (/= II 0) (PROGN
             (COND ((= JSF "+") (SETQ TXT (+ TXT TXT1)))
                   ((= JSF "-") (SETQ TXT (- TXT TXT1)))
                   ((= JSF "*") (SETQ TXT (* TXT TXT1)))    
                   ((= JSF "/") (SETQ TXT (/ TXT TXT1)))
                   )
            ))
        (SETQ II (+ II 1))
        )
        (COMMAND "COPY" NAME0 "" P0 PP0)
        (SETQ TXT (/ TXT XI))
        (SETQ TXT (RTOS TXT 2 WS))
         (setq txt-style (cdr (assoc 7 (entget name0))))
         (setq style-higth (cdr (assoc 40 (tblsearch "style" txt-style))))
         (if (= style-higth 0.0)
            (COMMAND "CHANGE" "L" "" "" "" "" "" "" TXT )
            (command "change" "l" "" "" "" "" "" txt))
        ))
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     (IF (AND (/= SS2 NIL) (/= SS1 NIL)) (PROGN
       (SETQ NN2 (SSLENGTH SS2))
       (IF (>= NN1 NN2) (SETQ NN NN2) (SETQ NN NN1))
       (WHILE (< II NN)
           (SETQ ENT1 (ENTGET (SETQ NAME1 (SSNAME SS1 II))))
           (SETQ ENT2 (ENTGET (SETQ NAME2 (SSNAME SS2 II))))
           (SETQ TXT1 (CDR (ASSOC 1 ENT1)))
           (SETQ TXT2 (CDR (ASSOC 1 ENT2)))
           (SETQ TXT1 (ATOF TXT1))
           (SETQ TXT2 (ATOF TXT2))
           (COND ((= JSF "+") (SETQ TXT (+ TXT1 TXT2)))
                   ((= JSF "-") (SETQ TXT (- TXT1 TXT2)))
                   ((= JSF "*") (SETQ TXT (* TXT1 TXT2)))    
                   ((= JSF "/") (SETQ TXT (/ TXT1 TXT2)))
                   )
           (COMMAND "COPY" NAME1 "" P0 PP0)
           (SETQ TXT (/ TXT XI))
           (SETQ TXT (RTOS TXT 2 WS))
           (setq txt-style (cdr (assoc 7 (entget name1))))
           (setq style-higth (cdr (assoc 40 (tblsearch "style" txt-style))))
           (if (= style-higth 0.0)
            (COMMAND "CHANGE" "L" "" "" "" "" "" "" TXT )
            (command "change" "l" "" "" "" "" "" txt))
           (SETQ II (+ II 1))
           )
         ))
       (SETvar "BLIPMODE" 0)
       (SETvar "CMDECHO" 0)
       (PRINC)
           )
(DEFUN C:TCH(/ SS NN II ENT NAME TXT PP0 P0 P XI JSF WS)
     (SETvar "BLIPMODE" 0)
     (SETvar "CMDECHO" 0)
     (INITGET (+ 1 2 4))
     (GETSS)
     (SETQ SS SSA)
     (INITGET (+ 1 2 4))
     (SETQ JSF (GETSTRING "\n   JI SUAN FU:?<*>  "))
     (WHILE (AND (/= JSF "+") (/= JSF "-") (/= JSF "*") (/= JSF "/") (/= JSF ""))
            (GETSTRING "JI SUAN FU:?<*>  "))
     (IF (= JSF "") (SETQ JSF "*"))
     (SETQ XI (GETREAL "\n  XI SHU:?"))
     (SETQ P0 (CDR (ASSOC 10 (ENTGET (SSNAME SS 0)))))
     (SETvar "BLIPMODE" 0)
     (INITGET (+ 1 2 4))
     (SETQ PP0 (GETPOINT "\n  TEXT-POINT:?"))
     (SETvar "BLIPMODE" 0)
     (SETQ WS (GETINT "\n  XIAO SHU WEI:?<1> "))
     (IF (= WS NIL) (SETQ WS 1))
   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     (SETQ NN (SSLENGTH SS))
     (SETQ II 0)
     (WHILE (< II NN)
           (SETQ ENT (ENTGET (SETQ NAME (SSNAME SS II))))
           (SETQ TXT (CDR (ASSOC 1 ENT)))
           (SETQ TXT (ATOF TXT))
           (COND ((= JSF "+") (SETQ TXT (+ TXT XI)))
                 ((= JSF "-") (SETQ TXT (- TXT XI)))
                 ((= JSF "*") (SETQ TXT (* TXT XI)))    
                 ((= JSF "/") (SETQ TXT (/ TXT XI)))
                   )
           (COMMAND "COPY" NAME "" P0  PP0)
           (SETQ TXT (RTOS TXT 2 WS))
           (setq txt-style (cdr (assoc 7 (entget name))))
           (setq style-higth (cdr (assoc 40 (tblsearch "style" txt-style))))
           (if (= style-higth 0.0)
            (COMMAND "CHANGE" "L" "" "" "" "" "" "" TXT )
            (command "change" "l" "" "" "" "" "" txt))
           (SETQ II (+ II 1))
           )
       (SETvar "BLIPMODE" 0)
       (SETvar "CMDECHO" 0)
       (PRINC)
           )
(DEFUN C:HZZ(/  NN II ENT NAME TXT PP P0 PP0 NAME1
              P ST ZH ZW ANG I N W TEMP)
     (SETvar "BLIPMODE" 0)
     (SETvar "CMDECHO" 0)
     (PROMPT "\n    FIRST-SSGET:")
     (INITGET (+ 1 2 4))
     (getss)
     (SETQ SS1 ssa)
     (PROMPT "\n    SECOND-SSGET:")
     (getss)
     (SETQ SS2 ssa)
    
     (SETQ P0 (CDR (ASSOC 10 (ENTGET (SETQ NAME0 (SSNAME SS1 0))))))
     (INITGET (+ 1 2 4))
     (SETQ PP0 (GETPOINT "\n    TEXT-POINT:?"))
    
     (INITGET(+ 1 2 4))
     (SETQ XI (GETREAL "\n   HUI ZONG GAN JIN-- DAN WEI ZHONG:? <0.888>"))
     (IF (= XI NIL) (SETQ XI 0.888))

     (SETvar "BLIPMODE" 0)
     (SETQ WS (GETINT "\n    XIAO SHU WEI:?<2> "))
     (IF (= WS NIL) (SETQ WS 2))
    
     (SETQ NN1 (SSLENGTH SS1))
     (SETQ II 0)
     (SETQ TXT 0)
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     (IF (AND (/= SS2 NIL) (/= SS1 NIL)) (PROGN
       (SETQ NN2 (SSLENGTH SS2))
       (IF (>= NN1 NN2) (SETQ NN NN2) (SETQ NN NN1))
       (WHILE (< II NN)
           (SETQ ENT1 (ENTGET (SETQ NAME1 (SSNAME SS1 II))))
           (SETQ ENT2 (ENTGET (SETQ NAME2 (SSNAME SS2 II))))
           (SETQ TXT1 (CDR (ASSOC 1 ENT1)))
           (SETQ TXT2 (CDR (ASSOC 1 ENT2)))
           (SETQ TXT1 (ATOF TXT1))
           (SETQ TXT2 (ATOF TXT2))
           (IF (= TXT1 XI) (SETQ TXT (+ TXT TXT2)))
           (SETQ II (+ II 1))
           )
         ))
        (COMMAND "COPY" NAME0 "" P0 PP0)
        (SETQ TXT (RTOS TXT 2 WS))
           (setq txt-style (cdr (assoc 7 (entget name0))))
           (setq style-higth (cdr (assoc 40 (tblsearch "style" txt-style))))
           (if (= style-higth 0.0)
            (COMMAND "CHANGE" "L" "" "" "" "" "" "" TXT )
            (command "change" "l" "" "" "" "" "" txt))

       (SETvar "BLIPMODE" 0)
       (SETvar "CMDECHO" 0)
       (PRINC)
           )

发表于 2012-12-13 15:49:33 | 显示全部楼层
研究一下,感觉还是比较靠谱
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 15:39 , Processed in 0.171596 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表