Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

To interpret mathematical formulas

10 REPLIES 10
Reply
Message 1 of 11
Lupo76
1107 Views, 10 Replies

To interpret mathematical formulas

Hello everyone,as we all know in lisp you can use mathematical formulas by first inserting the type of operation and then the parameters:

 

(* (+ 3 5) 2) = 16

 

there is a function that can be interpreted, however, the text string:

"(3+5)*2" = 16

 

?

 

Please also confirm the absence of a similar function.

 

Thanks

Hello everyone,


10 REPLIES 10
Message 2 of 11
Anonymous
in reply to: Lupo76

So what info are you asking for? 

 

Even if you remove the quotes it's still not in the proper format to execute.  You need to clarify what kind of help you are asking for.

Message 3 of 11
Lupo76
in reply to: Anonymous

I do not know how to explain better the problem Smiley Sad


The user must enter (3 +5) * 2


I am looking for a function that is able to interpret the string as a mathematical formula and then to return the formula result.


Message 4 of 11
Lee_Mac
in reply to: Lupo76

You would need to construct a function to parse the string and return an AutoLISP expression.

 

An example of such a function is used in this program by member HighflyingBird (may require membership), the parser function is attached.

 

e.g.:

 

_$ (trans_format "(3+5)*2")
(* (+ 3.0 5.0) 2.0)

 

 

 

Message 5 of 11
Lupo76
in reply to: Lee_Mac

Thanks !!!!!

great!

Just what I was looking for!

 

Smiley Very Happy

Message 6 of 11
Lee_Mac
in reply to: Lupo76

Lupo,

 

You're welcome Smiley Wink

 

Did you just want the result (16), or the AutoLISP expression itself?

Message 7 of 11
Lee_Mac
in reply to: Lee_Mac

If you just wanted the result (16), you could use an instance of ScriptControl to evaluate a VBScript expression, for example:

 

;; Eval VBScript Expression  -  Lee Mac 2011
;; Args: expr = string containing expression to evaluate

(defun LM:EvalVBExpression ( expr / scr res )
    (setq res
        (vl-catch-all-apply
            (function
                (lambda nil
                    (setq scr (vla-getinterfaceobject (vlax-get-acad-object) "ScriptControl"))
                    (vlax-put-property scr 'language "VBScript")
                    (vlax-invoke scr 'eval expr)
                )
            )
        )
    )
    (if scr (vlax-release-object scr))
    (if (not (vl-catch-all-error-p res)) res)
)
(vl-load-com)

 

e.g.:

 

_$ (LM:EvalVBExpression "(3+5)*2")
16

 

Message 8 of 11
Lupo76
in reply to: Lee_Mac

Very good,even better!
I used the above code with (eval):

 

(setq risult (eval (trans_format "(3+5)*2")))

 

but the function LM: EvalVBExpression is much cleaner.
thanks.

Message 9 of 11
Lee_Mac
in reply to: Lupo76

Excellent, you're welcome Smiley Happy

Message 10 of 11
amministrazione
in reply to: Lee_Mac

Hello Lee
I'm using the function

 

(LM:EvalVBExpression "(3+5)*2")

a new computer with Win7 64-bit + AutoCAD MAP3D 2012.
With this configuration, returns nil 😞

The exact same function with WinXP 32-bit + AutoCAD2012 works very well!

Can you tell me why?

Message 11 of 11
dbroad
in reply to: Lupo76

You hardly need to reinvent the wheel by fancy string parsing. Since AutoLISP was originally built, there has been a built-in calculator for this type of stuff.  It is arx based and must be arxloaded or automatically loaded by using the cal command.

 

(cal "(3+5)*2")

16

 

 

It also has lots of really useful unique functions and keywords that it supports.

Architect, Registered NC, VA, SC, & GA.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost