 Author Topic: Parsing / calculating math formulas  (Read 1812 times) dreamerman

• Global Moderator
• Dr. Type
•       • • Posts: 288 Parsing / calculating math formulas
« on: 2017-Jun-04 »
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
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 Qedo Re: Parsing / calculating math formulas
« Reply #1 on: 2017-Jun-04 »
I am using it and it seems to me well done and will definitely be useful
thank you bigsofty

• Community Developer
• Prof. Inline
•      • • Posts: 2628 Re: Parsing / calculating math formulas
« Reply #2 on: 2017-Jun-05 »
Yup, could be the basis for a scripting module. 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)