Jump to content

Recommended Posts

Posted

I need a lisp to make a loop to do sum and save the total numbers. for example I need to run it and type in command line 5.45, 5.55, 5.89 or more  and give me a total and it doesn't matter each time how many number we are typing and each time giving us a sum and the total of numbers. May you help me please. Thank you!

Posted
(defun c:mysum()
  (setq i 0 sum 0 nr T)
  (while nr
    (setq nr (getreal (strcat "Number no " (itoa (setq i (1+ i))) "=")))
    (setq sum (+ sum (if nr nr 0)))
    )
  (princ (strcat "sum= " (rtos sum)))
  (princ)
  )

 

Posted

command : ( + 5.45 5.55 5.89 ;|....etc....|; )

16.89

 

p/s: while inputting decimal <10, eg: '0.4' you can not suppress 0 as '.4' , zero '0' must be included, ie: 0.4 

Posted (edited)

Fuccaro not sure why need nr T and double check, this worked ok. Happy to take comments about method not right

 

Also CAL note Briscad opens a calculator.

Cal 23+42+36 enter it does much more than + - / etc.

 

hanhphuc nice idea.

 

(defun c:mysum()
  (setq i 0 sum 0 )
  (while   (setq nr (getreal (strcat "Number no " (itoa (setq i (1+ i))) "=")))
    (princ (strcat "sum= " (rtos  (setq sum (+ sum  nr)) 2 3) " "))
    )
  (princ)
)
(c:mysum)

 

Edited by BIGAL
Posted

@Bigal

I don't use AutoCAD these days and I decided to play Lisp when I have some time. And you know: if you (read: I) don't use it, you loose it.

I remember years ago my fingers running on the keybord and usually the programs used to run more or less from the first try.

Yes, I see that it works without that nr T too. We all know that a code can be more or less efficient. Maybe next time I will perform better.

Have a nice and virus free day!

Posted (edited)

I just thought that maybe there was a reason that I was missing with just doing (while. 

 

Thinking a bit more I am pretty sure did something in past.

enter 1st number

type +123.45 etc

follow with -23 +56.35 /23.2 it looks at the 1st character in a string and does that math action so much more than just +

 

Did the same for draw ortho lines L123 u45 r65.78 etc.

 

Had a few minutes

(defun c:alscalc ( / ans txt calc)
(setq ans (getreal "Enter 1st number "))
(setq calc "")
(while (/= (setq txt (getstring (strcat "\Enter calculation +-/*  Enter to exit " (rtos ans 2 3) " "))) "")
(setq calc (strcat calc " " txt))
(setq is (substr txt 1 1 ))
(setq num (atof (substr txt 2)))
(cond
((= is "/")(setq ans (/ ans num)))
((= is "+")(setq ans (+ ans num)))
((= is "-")(setq ans (- ans num)))
((= is "*")(setq ans (* ans num)))
)
)
(alert (strcat calc "\n\nAnswer =  " (rtos ans 2 3)))
(princ)
)

 

 

Edited by BIGAL

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...