Jump to content

Code for mathematical calculations of two numbers


Recommended Posts

Posted (edited)
  On 3/22/2025 at 6:47 AM, Nikon said:

@Tsuky HUGE thank. The division operation is  active, but there is one drawback.
Dividing a smaller number by a larger one gives 0.
For example, 5/10=0 or 5/550=0... decimal division works fine 05./50.5=0.9.

???

Is it possible to remember the last choice of operation?

Expand  

If you submit integers to the divide function, the result will be an integer. See help ...

This is possible: remove the variable "op" from local variables, which could give:

(defun c:CalcTwoTxt ( / ss1 ss2 val1 val2 key rslt inspt)
  (princ "\nSelect first text")
  (while (not (setq ss1 (ssget "_+.:E:S" '((0 . "*TEXT"))))))
  (princ "\nSelect second text")
  (while (not (setq ss2 (ssget "_+.:E:S" '((0 . "*TEXT"))))))
  (setq val1 (extractnumber (cdr (assoc 1 (entget (ssname ss1 0))))))
  (setq val2 (extractnumber (cdr (assoc 1 (entget (ssname ss2 0))))))
  (cond
    ((and val1 val2)
      (if (not op) (setq op "+"))
      (initget "* + - /")
      (setq key (getkword (strcat "\nSelect an operation [*/+/-/\/]? <" op ">: ")))
      (if key (setq op key))
      (if (and (eq op "/") (member 0 val2))
        (setq rslt 0.0)
        (setq rslt (apply (read op) (append val1 val2)))
      )
      (initget 1)
      (setq inspt (getpoint "\nSpecify the insertion point of the result: "))
      (command "_.TEXT" inspt "2.5" "0" (rtos rslt 2 2))
    )
    (T (princ "\No value found in text"))
  )
  (prin1)
)

 

Edited by Tsuky
  • Like 1
Posted (edited)
  On 3/23/2025 at 10:17 AM, Tsuky said:

f you submit integers to the divide function, the result will be an integer. See help ...

This is possible: remove the variable "op" from local variables, which could give:

Expand  

The code with the added function

EXTRACTNUMBER.

(defun ExtractNumber (str / l rslt)
 (setq
 l
 (mapcar
 '(lambda (x)
 (if (and (> x 44) (< x 58) (/= x 47)) x 32)
 )
 (vl-string->list str)
 )
 l
 (mapcar
 '(lambda (x y)
 (if (not (= x y 32)) x)
 )
 l
 (append (cdr l) '(32))
 )
 l (vl-remove-if-not '(lambda (x) (eq (type x) 'INT) x) l)
 l (mapcar '(lambda (x) (if (not (eq x 32)) x (list nil))) l)
 )
 (eval (read (strcat "(setq rslt (list " (apply 'strcat (mapcar '(lambda (x) (if (not (listp x)) (chr x) " ")) l)) "))")))
)
(defun c:Calc2TxtTsk ( / ss1 ss2 val1 val2 key rslt inspt)
 (princ "\nSelect first text")
 (while (not (setq ss1 (ssget "_+.:E:S" '((0 . "*TEXT"))))))
 (princ "\nSelect second text")
 (while (not (setq ss2 (ssget "_+.:E:S" '((0 . "*TEXT"))))))
 (setq val1 (extractnumber (cdr (assoc 1 (entget (ssname ss1 0))))))
 (setq val2 (extractnumber (cdr (assoc 1 (entget (ssname ss2 0))))))
 (cond
 ((and val1 val2)
 (if (not op) (setq op "+"))
 (initget "* + - /")
 (setq key (getkword (strcat "\nSelect an operation [*/+/-/\/]? <" op ">: ")))
 (if key (setq op key))
 (if (and (eq op "/") (member 0 val2))
 (setq rslt 0.0)
 (setq rslt (apply (read op) (append val1 val2)))
 )
 (initget 1)
 (setq inspt (getpoint "\nSpecify the insertion point of the result: "))
 (command "_.TEXT" inspt "2.5" "0" (rtos rslt 2 2))
 )
 (T (princ "\No value found in text"))
 )
 (prin1)
)

 

Edited by Nikon
function EXTRACTNUMBER  added
Posted

So to first principles....

"No function definition" means it is looking for a function "ExtractNumber", I wonder if this was elsewhere in this thread.

Posted

No, the confused doesn't work... at 400+ posts it wold be reasonable expectation for you to look back at Tsukys other posts to see if the missing LISP function is in there.

Posted (edited)

The function EXTRACTNUMBER  has been added. The code works. Thanks!

Edited by Nikon
  • Like 1

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...