Re: [問題] lisp 的if判斷式
以下程式,示範 AutoLISP
if 的使用方式。
因為,if 後面的 then- block
和 else- block 只接受一個指令,
所以,使用 (progn . . .) 來對付
; file: TEST-02.LSP
; y= f(x)= a*(x^2) + b*x + c= 0
; (setq a 1.0 b 2.0 c 1.0)
; (setq a 1.0 b -2.0 c 1.0)
; (setq a 1.0 b 3.0 c 1.0)
(setq a 1.0 b 1.0 c 1.0)
; d= (b^2) - 4*a*c
(setq d (- (* b b) (* 4.0 a c)))
(if (> d 0.0)
(progn ; yes! d > 0.0
(princ "D > 0, there are 2 real number solution\n")
; x= (-b + sqrt(d))/(2*a)
(setq x1 (/ (+ (* b -1.0) (sqrt d)) (* 2.0 a)))
(setq x2 (/ (- (* b -1.0) (sqrt d)) (* 2.0 a)))
(setq y1 (+ (* a x1 x1) (* b x1) c))
(setq y2 (+ (* a x2 x2) (* b x2) c))
(princ (strcat "x1= " (rtos x1 2 20) ", y1= " (rtos y1 2 20) "\n"))
(princ (strcat "x2= " (rtos x2 2 20) ", y2= " (rtos y2 2 20) "\n"))
); yes! d > 0.0
(progn
(if (< d 0.0)
(progn ; yes! d < 0.0
(princ "D < 0, there is no real number solution!\n")
(princ (strcat "D= " (rtos D 2 20) "\n"))
)
(progn ; no! d = 0.0
(princ "D = 0, there is 1 real number solution\n")
(setq x (/ (* b -1.0) (* 2.0 a)))
(setq y (+ (* a x x) (* b x) c))
(princ (strcat "x= " (rtos x 2 20) ", y= " (rtos y 2 20) "\n"))
)
)
); no! d <= 0.0
)
(princ)
; end of file
※ 引述《ggg888 (g8)》之銘言:
: 不好意思
: 想請教幾個問題
: 1.
: 請問市面上有關lisp的書那一本比較好?
: visual lisp跟原本的lisp相較之下
: 二者有何不同
: 2.
: lisp中有像其他高階語言那般有 if ()
: else if ()
: else ()
: 的語法嗎?
: 若沒有
: 那麼在lisp裡怎麼達到同樣的function
: 3.
: 現在autocad的版本都有支援visual basic
: 那麼各位比較傾向用lisp或是visual basic來撰寫自己想要的函式呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.34.222.10
討論串 (同標題文章)
Cad_Cae 近期熱門文章
PTT數位生活區 即時熱門文章