GLBasic forum

Codesnippets => Code Snippets => Topic started by: dreamerman on 2017-Jun-04

Title: Parsing / calculating math formulas
Post by: dreamerman 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
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...
Title: Re: Parsing / calculating math formulas
Post by: Qedo on 2017-Jun-04
I am using it and it seems to me well done and will definitely be useful
thank you
Title: Re: Parsing / calculating math formulas
Post by: bigsofty on 2017-Jun-05
Yup, could be the basis for a scripting module.  :good: