TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on November 24, 2004, 10:21:53 AM
-
Re-write the following function using a different procedure but produce the same results.
(defun c:cyrds ( / cubic_yards)
(if
(setq cubic_yards
(/
(apply '*
(list
(getreal "\nEnter length: ")
(getreal "\nEnter width: ")
(getreal "\nEnter height: ")
)
)
27.00
)
)
(prompt
(strcat
"\nCubic yards of concrete = "
(rtos cubic_yards 2 2)
)
)
)
(princ)
)
-
I'd prefer to leave the return value numerical so it could be used to feed another function or the command line. Dressing up the return value could be performed by a calling function.
(defun CubicYards ( )
(apply '*
(cons
(/ 1 27.0)
(mapcar
'(lambda (what)
(initget 7)
(getdist (strcat "\nEnter " what))
)
'("length: " "width: " "height: ")
)
)
)
)
-
MP, very slick..
That one would be hard to beat.
-
Here is my offering.
Rather straight forward.
(defun cyrds (bu / x y z)
(initget 7)
(setq x (getdist "\nEnter length: "))
(initget 7)
(setq y (getdist "\nEnter width: "))
(initget 7)
(setq z (getdist "\nEnter height: "))
(/ (/ (* x y z) 27.0) (if (= bu "Feet") 1 1728))
)
(defun c:cyards(/ units)
(initget "Feet Inches")
(setq units (getkword "\nYou base unit is in [Feet or Inches] <Inches>."))
(prompt (strcat "\nCubic yards of concrete = " (rtos (cyrds units) 2 2)))
(princ)
)
-
While we're on the subject of nested functions..............
(defun c:cyrds ( / get-real cubic-yards val lst)
(defun get-real (what)
(getreal (strcat "\n"what": "))
)
(defun cubic-yards (lst)
(/ (apply '* lst) 27.00)
)
(if
(= (length
(foreach item '("Length" "Width" "Height")
(if (setq val (get-real item))
(setq lst (cons val lst))
)
)
)
3
)
(princ
(strcat "\nCubic yards = "(rtos (cubic-yards lst) 2 2))
)
; else
(princ
(strcat
"\nI need more than "
(itoa (length lst))
" numbers to complete my task"
)
)
)
(princ)
)
-
... and the recursive variant:
(defun cubicYards (lst)
(cond ((= (length lst) 3)
(mapcar 'princ (list (/ (apply '* lst) 27.0) " cubic yards")))
((cubicYards (cons (getdist (strcat "\nEnter "
(nth (length lst) '("length" "width" "height"))
": ")) lst)))
)
(princ)
)
-
Stig, what are you doing talking in old money ????
-
Old money?
As for what I'm doing, I generally have no idea :D
-
Old money?
in the UK, when we refer to anything pre-decimal/pre-metric (i.e. feet and inches) it's referred to as "old money" like when we had £/s/d
-
Ah ok. Thanks Hendie.
... they started it!