samifox Posted May 10, 2013 Share Posted May 10, 2013 Hi As my “beginner” membership state has been void (thankfully in some philosophic way ) I was forced tolearn the ABC of autolisp , so please be kind to me After reading help I start inventing tasks for my self-using the expressions I have learned. Im trying to ask the user to type 5 integers and then store them into a list The following code get endless loop (I code it by my totally self ) [font=Calibri][size=3];_ ask the user to type 5 integers[/size][/font] [size=3][font=Calibri] ;_put them into a list[/font][/size] [font=Calibri][size=3](defun C:demo (/ i)[/size][/font] [font=Calibri][size=3] (setq i 1)[/size][/font] [font=Calibri][size=3] (while[/size][/font] [size=3][font=Calibri] (and[/font][/size] [size=3][font=Calibri] (setq lst '(getint ("Type 5 numbers")))[/font][/size] [size=3][font=Calibri] (<= i 5)[/font][/size] [size=3][font=Calibri] (setq i (1+ i))[/font][/size] [size=3][font=Calibri] ) ;_and[/font][/size] [size=3][font=Calibri] ) ;_while[/font][/size] [size=3][font=Calibri]) ;_defun`[/font][/size] Why? Quote Link to comment Share on other sites More sharing options...
pBe Posted May 10, 2013 Share Posted May 10, 2013 You mean user will prompted 5 times or more until the list is 5 elements? Quote Link to comment Share on other sites More sharing options...
samifox Posted May 10, 2013 Author Share Posted May 10, 2013 yes enter between the numbers Quote Link to comment Share on other sites More sharing options...
pBe Posted May 10, 2013 Share Posted May 10, 2013 First of all: (setq lst '(getint ("Type 5 numbers"))) to (setq lst (getint "\nType 5 numbers: ")) And what do you mean by 5 numbers? a five digit integer? or single digit at every loop? Be wary of getint on the former Type 5 numbers: 56236 Requires an integer between -32768 and 32767. So what will it be? 5 digit number or single? Quote Link to comment Share on other sites More sharing options...
BIGAL Posted May 10, 2013 Share Posted May 10, 2013 Like Pbe If you want say multiple integers eg 1 12 3456 2 34 567 then you can create a loop and when you press enter twice it will stop this way can be 1 or 5 or as many as you want also can be fixed number required also. 12345 67890 please advise. Quote Link to comment Share on other sites More sharing options...
samifox Posted May 10, 2013 Author Share Posted May 10, 2013 I want the user to enter number between 1 to 100 and then I will start to play with this list 45 56 12 64 87 . The way it will be doesn't matter as I just learning. Of you u want to show few ways to solve it god bless Quote Link to comment Share on other sites More sharing options...
BIGAL Posted May 10, 2013 Share Posted May 10, 2013 A simple method is a while loop check for nil entry (setq ansint 0) ; dummy to start (while (/= ansint nil) (setq ansint (getint "\Enter integer press enter to stop")) (setq intans (cons ansint intans)) ) Quote Link to comment Share on other sites More sharing options...
pBe Posted May 10, 2013 Share Posted May 10, 2013 Sample: (defun C:demo (/ i lst) (while (and (< (length lst) 5) (setq i (initget 7) i (getreal "\nEnter number from 1 to 100 : ")) ) (if (<= i 100) (print (Setq lst (cons (fix i) lst))) (princ "\n<<Number out of range>>") ) ) lst (princ) ) Why getreal you ask? you will know if ever you want to be prompted for large numbers. HTH Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted May 10, 2013 Share Posted May 10, 2013 I would suggest perhaps: (defun c:5num ( / i l n ) (setq i 1) (while (and (< i 6) (setq n (1-100 (strcat "\nEnter number " (itoa i) ": "))) ) (setq i (1+ i) l (cons n l) ) ) (reverse l) ) (defun 1-100 ( m / n ) (while (and (setq n (getint m)) (not (<= 1 n 100)) ) (princ "\nNumber must be between 1 and 100.") ) n ) Quote Link to comment Share on other sites More sharing options...
samifox Posted May 10, 2013 Author Share Posted May 10, 2013 A simple method is a while loop check for nil entry (setq ansint 0) ; dummy to start (while (/= ansint nil) (setq ansint (getint "\Enter integer press enter to stop")) (setq intans (cons ansint intans)) ) its the same as the while getint() does, is it not? Quote Link to comment Share on other sites More sharing options...
pBe Posted May 10, 2013 Share Posted May 10, 2013 (edited) Only 5 digit numbers : [for learning purposes] (defun C:demo2 (/ i lst) (while (and (< (length lst) 5) (setq i (initget 7) i (getreal "\nEnter 5 digit number: ")) ) (if (< [color="blue"]9999[/color] i 100000) (print (Setq lst (cons (fix i) lst))) (princ "\n<<Number out of range>>") ) ) lst (princ) ) Edited May 10, 2013 by pBe number doesnt compute Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted May 10, 2013 Share Posted May 10, 2013 Only 5 digit numbers 1000 - 9999 = 4 digits Quote Link to comment Share on other sites More sharing options...
pBe Posted May 10, 2013 Share Posted May 10, 2013 1000 - 9999 = 4 digits ahhhh yes numbers will be the death of me LM Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted May 10, 2013 Share Posted May 10, 2013 ahhhh yes numbers will be the death of me LM Here is another way to determine the number of digits: (defun c:5dig ( / d n ) (while (and (setq n (getreal "\nEnter a 5-digit number: ")) (/= 5 (setq d (digits (setq n (fix n))))) ) (princ (strcat "\n" (itoa n) " has " (itoa d) " digits.")) ) n ) (defun digits ( n ) (1+ (fix (/ (log n) (log 10)))) ) Quote Link to comment Share on other sites More sharing options...
pBe Posted May 10, 2013 Share Posted May 10, 2013 (defun digits ( n ) (1+ (fix (/ (log n) (log 10)))) ) Very nice Lee I knew there's a math solution to that besides using the usual operators. Can you explain the function log in a way everyone else can understand it? Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted May 10, 2013 Share Posted May 10, 2013 Very nice Lee I knew there's a math solution to that besides using the usual operators. Cheers pBe Can you explain the function log in a way everyone else can understand it? How long have you got? To briefly explain, the AutoLISP log function returns the natural logarithm (usually written 'ln') of the supplied number, that is, the logarithm with base e (2.718...), or the inverse exponential function. For logarithms in general we have: log_a(b) = x => a^x = b That is, the logarithm base a of b results in a number such that a raised to that number will equal b. In my function, I am calculating a value x such that: n = 10^x Using a logarithm of base 10 this can be easily calculated (since log_x(x) = 1): n = 10^x log_10(n) = log_10(10^x) log_10(n) = x(log_10(10)) => x = log_10(n) However, since we only have access to the natural logarithm function (log), we must proceed in the following way: n = 10^x log(n) = log(10^x) log(n) = x(log(10)) => x = log(n)/log(10) Quote Link to comment Share on other sites More sharing options...
pBe Posted May 10, 2013 Share Posted May 10, 2013 To briefly explain, the AutoLISP log function returns the natural logarithm (usually written 'ln') of the supplied number, that is, the logarithm with base e (2.718...), or the inverse exponential function.... Excellent, I may not know what that means now but in time I will. Thank you Lee, appreciate the lesson. Cheers Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted May 10, 2013 Share Posted May 10, 2013 Excellent, I may not know what that means now but in time I will. Thank you Lee, appreciate the lesson. Cheers You're very welcome. There's far too much information on logarithms than could be explained in a forum post, but if you are interested in learning more, the Khan Academy have a whole section on the topic: https://www.khanacademy.org/math/algebra/logarithms-tutorial Quote Link to comment Share on other sites More sharing options...
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.