### Author Topic: Parsing / calculating math formulas  (Read 3669 times)

#### dreamerman

• Global Moderator
• Dr. Type
• Posts: 431
##### 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 stuffGOSUB math_parser_initPRINT "Please type x value:", 10, 10INPUT x_val\$, 10, 20mp_add_iconst("x", x_val\$)PRINT "Write equation below", 10, 30INPUT user_str\$, 10, 40user_str\$ = parse_math_cmd\$(user_str\$)PRINT "result: " + user_str\$, 10, 50DEBUG "result: " + user_str\$SHOWSCREENMOUSEWAITEND`
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

• Dr. Type
• Posts: 362
• to program what I have do how should programming?
##### 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: 2768
##### 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)