TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on March 31, 2006, 11:29:56 AM
-
Calculate the volume of a cylinder given diameter and height. However, to make it a challenge you can not use any variables. You must prompt the user for for both diameter and height, include error checking for negative numbers, zero and <enter>. Local functions are allowed.
have fun!
-
<sitting on hands for now>
:evil:
-
you can not use any variables.
Whether truly I have understood, what it is impossible to use SETQ?
-
If I understand Mark's intent, and I believe I do, he meant you cannot use set or setq.
-
For fun I offer --
(defun c:foo ( )
(princ
(strcat "Volume of cylinder = "
(rtos
( (lambda ( foo )
(* pi
(expt (foo "\nEnter radius: ") 2)
(foo "\nEnter height: ")
)
)
(lambda ( pmt ) (initget 7) (getdist pmt))
)
)
)
)
(princ)
)
-
Another fun variant --
(defun c:foo ( / foo )
(defun foo ( pmt ) (initget 7) (getdist pmt))
(princ
(strcat "Volume of cylinder = "
(rtos
( (lambda ( radius height )
(* pi
(expt radius 2)
height
)
)
(foo "\nEnter radius: ")
(foo "\nEnter height: ")
)
)
)
)
(princ)
)
-
And for a daffy combination of the two previous variants --
(defun c:foo ( )
(princ
(strcat "Volume of cylinder = "
(rtos
( (lambda ( foo )
( (lambda ( radius height )
(* pi
(expt radius 2)
height
)
)
(foo "\nEnter radius: ")
(foo "\nEnter height: ")
)
)
(lambda ( pmt ) (initget 7) (getdist pmt))
)
)
)
)
(princ)
)
:P
-
Here's mine:
;Calculate the volume of a cylinder given diameter and height. However,
;to make it a challenge you can not use any variables. You must prompt
;the user for for both diameter and height, include error checking for
;negative numbers, zero and <enter>. Local functions are allowed.
;
;
;
;Cylinder pi * radius2 * height
;
;
;
(defun c:cylinder (/ ask sq)
(defun ask (msg)
(initget 7)
(getreal msg)
)
(defun sq (num)
(* num num)
)
(princ
(strcat "\nCylinder Volume: "
(rtos (* pi
(sq (ask "\nEnter Diameter for Cylinder: "))
(ask "\nEnter Height for Cylinder: ")
)
2
)
)
)
(princ)
)
-
Will I'd say you copied my idea but you posted first. :-)
(defun c:cubevol ( / ask)
(defun ask (msg)
(initget 7)
(getreal msg)
)
(princ
(strcat "Volume = "
(rtos
(*
(* pi
(expt (/ (ask "\nDiameter: ") 2) 2)
)
(ask "\nHeight: ")
)
)
)
)
(princ)
)
-
Nice challenge Mark .. :-)
-
There was once a tale told of WW2 in the Pacific, The Japanese were on one side of the island and the Allies were on the other with a big jungle in the middle. The question was how to go after the Japanese.
The Aussie said "just go through the jungle and get the Japanese"
The Brit said "No, we need to go around the jungle"
The American said " Naw you are all wrong! We just remove the jungle and go get them Japs"
The Aussie lisp would be just to paste this in the command line
(* pi (getreal "raduis")(getreal "radius again")(getreal "height"))
Has the additional advantage of working with truncated cones as well :-D
-
On deeper thought it does has to be a cylinder or very nearly one. but there is also
'cal (sqr(radius))*pi* (height)
but you have to type the radius and height in, and technically it is not lisp but it is short and sweet
-
(apply '*
(list (expt (cond (1 (initget 6) (getdist "\nRadius: "))) 2)
(cond (1 (initget 6) (getdist "\nHeight: ")))))
-
Yes I know I need glasses.... :lol:
(apply
'*
(list (expt (cond (1 (initget 7) (/ (getdist "\nDiameter: ") 2)))
2)
(cond (1 (initget 7) (getdist "\nHeight: ")))))
-
hehehehe .. I was just posting, you spoil my fun :cry:
-
Late to the party as usual, but here is my late entry.
(* (/ pi 4)
(expt (progn (initget 7) (getdist "\nDiameter: ")) 2)
(progn (initget 7) (getdist "\nHeight: ")))
-
I must admit too.... I forgot about the usage of progn :oops:
-
Expt! that was what I was missing! :cry:
(* (expt (getdist "radius?")2) pi (Getdist "Height"))
(*) doesn't need a progn, and if someone types in "George" it is their problem.
Now the tricky bit would be to draw the cylinder at the same time! :-P
-
(apply '(lambda (a b) (* pi a a b 0.25)) (mapcar '(lambda (a) (initget 7) (getdist a)) '("Diameter?" "Height?")))
-
Good stuff! That poor cat got skinned a bunch'a ways didn't it. :-)
-
I don't know how you come up with these challenges Mark. Very creative.
-
I don't know how you come up with these challenges Mark. Very creative.
It ain't easy! Wish I was talented enough to challenge the big guns. :-)
And for all those reading this, don't be shy, write your own challenges and post them.