Parsing / calculating math formulas

Previous topic - Next topic

dreamerman

Many years ago I've written an VB app to solve polynomials and draw graphs for them, key part of that project were routines for calculating simpler math stuff with keeping the proper order of performing mathematical operations. And that basically this code is, a simple math parser.
How to use it? First init with 'GOSUB math_parser_init', add your custom variables like 'x' with 'mp_add_iconst("x", 21)', and then just calculate with 'parse_math_cmd$(your_string$)' (you can also add custom functions) for example:
Code (glbasic) Select
GLOBAL user_str$, x_val$ // for inputed stuff
GOSUB math_parser_init

PRINT "Please type x value:", 10, 10
INPUT x_val$, 10, 20
mp_add_iconst("x", x_val$)
PRINT "Write equation below", 10, 30
INPUT user_str$, 10, 40
user_str$ = parse_math_cmd$(user_str$)
PRINT "result: " + user_str$, 10, 50
DEBUG "result: " + user_str$
SHOWSCREEN
MOUSEWAIT
END


You can use this for gui positioning/animations: button.left = integer(parse_math_cmd$("screen_width-20"))
or more advanced calculator app: result = parse_math_cmd$("2sin(0.5)+3(pi+int(1.1))-fps")
and so on...
Check my source code editor for GLBasic - link Update: 20.04.2020

Qedo

I am using it and it seems to me well done and will definitely be useful
thank you

bigsofty

Yup, could be the basis for a scripting module.  :good:
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)