LauKwokFai Posted April 27, 2011 Posted April 27, 2011 Dear all, I would like to set up a lisp that when input number is singular, do routine A, and do routine B while input number is plural....how CAD knows the input number is singular or plural ? Thank you Quote
MSasu Posted April 27, 2011 Posted April 27, 2011 You mean odd and even? (= (- (/ theNumber 2.0) (/ theNumber 2)) 0) Presuming that your argument is an integer. Regards, Mircea Quote
pBe Posted April 27, 2011 Posted April 27, 2011 Dear all, I would like to set up a lisp that when input number is singular, do routine A, and do routine B while input number is plural....how CAD knows the input number is singular or plural ? Thank you if you use getstring (string) (setq number (getstring "\nEnter Number:")) (if ( = (strlen number) 1) (routine A)(Routine B)) if you're using getint: (integer) (setq number (getint "\nEnter Number:")) (if (< number 10)(routine A)(Routine B)) Quote
pBe Posted April 27, 2011 Posted April 27, 2011 (edited) You mean odd and even? (= (- (/ theNumber 2.0) (/ theNumber 2)) 0) Presuming that your argument is an integer. Regards, Mircea I guess you're right.. the OP might meant to say Odd / Even or (equal (fix (/ number 2.00))(/ number 2.00)) Edited April 27, 2011 by pBe Quote
LauKwokFai Posted April 27, 2011 Author Posted April 27, 2011 You mean odd and even? (= (- (/ theNumber 2.0) (/ theNumber 2)) 0) Presuming that your argument is an integer. Sorry, I meant odd and even !! This is a very interesting solution, thank you so much. Quote
MSasu Posted April 27, 2011 Posted April 27, 2011 You're welcome! And please check the solution proposed by pBe in his/here second post - is more compact than mine. Regards, Mircea Quote
LibertyOne Posted April 27, 2011 Posted April 27, 2011 Dear all, I would like to set up a lisp that when input number is singular, do routine A, and do routine B while input number is plural....how CAD knows the input number is singular or plural ? Thank you Why not define two functions EVENP and ODDP? EVENP returns T when even and nil when odd. ODDP returns T when odd and nil when even. (defun evenp (i) (= (rem i 2) 0)) (defun oddp (i) (/= (rem i 2) 0)) Uses: (evenp 2) ;returns => T (evenp -5) ;returns => nil (if (evenp 4) ;<--substitute any integer where the 4 is (routineA) (routineB) ) Quote
LauKwokFai Posted April 27, 2011 Author Posted April 27, 2011 It is getting too deep for my level of autolisp knowledge, thank you so much anyway Quote
Lee Mac Posted April 27, 2011 Posted April 27, 2011 Another two: (defun evenp ( x ) (zerop (logand 1 x))) (defun oddp ( x ) (= 1 (logand 1 x))) Curious how similar this thread is to: http://www.cadtutor.net/forum/showthread.php?58684-Check-number-in-a-list-%28singular-or-plural-%29 Quote
pBe Posted April 27, 2011 Posted April 27, 2011 Another two: (defun evenp ( x ) (zerop (logand 1 x))) (defun oddp ( x ) (= 1 (logand 1 x))) Been trying to figiure that (logand) a while ago.. Thanks for the snippet Lee Quote
Michaels Posted April 27, 2011 Posted April 27, 2011 Another two: (defun evenp ( x ) (zerop (logand 1 x))) (defun oddp ( x ) (= 1 (logand 1 x))) Curious how similar this thread is to: http://www.cadtutor.net/forum/showthread.php?58684-Check-number-in-a-list-%28singular-or-plural-%29 You may think that we could be one . Regards Quote
caravaggio Posted April 27, 2011 Posted April 27, 2011 Thank you to everyone, been learning a lot today Quote
alanjt Posted April 27, 2011 Posted April 27, 2011 Skinnin' Cats...Keep your after-hours activities to yourself. Quote
Ryder76 Posted April 27, 2011 Posted April 27, 2011 Keep your after-hours activities to yourself. :lol: Quote
LibertyOne Posted April 27, 2011 Posted April 27, 2011 Another two: (defun evenp ( x ) (zerop (logand 1 x))) (defun oddp ( x ) (= 1 (logand 1 x))) interesting method in solving it this way... Quote
Lee Mac Posted April 27, 2011 Posted April 27, 2011 interesting method in solving it this way... There's probably many more ways to do it... (defun oddp ( n ) (and (member (rem n 10) '(1 3 5 7 9)))) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.