PDA

View Full Version : eval or better?



mikro
10th October 2006, 09:45
i need some help in deciding the best way to implement a feature. I need to give the user the possibility to define formulas with which the programm will then calculate.
The Formulas should have normal arithmetic possibilties as well as if/then
the variables used in the formula will enable me to find the correct class in a QMap which will deliver the actual value. For example consider the formula

= width * breadth
say my classes are in a QMap called attributes i would do:

attributes[width]->getValue() // returns 2;
attributes[breadth]->getValue() // returns 4
how to implement this?
first of all: to replace all the variables by searching for the variables in the formula i should use some special characters around them like %%width%% or something like that to distinguish them from arithmetic functions. But this would make it less easy for the person writing the formula. So i though instead i should go through my attributes-QMap and for each key i find there search and replace this key in the formula.
at that point the above formula should allready be
= 2 * 4;
Afterwards do i simply eval it or is there anything more elegant? originally i expected my program to use MySQL then i would simply have used this formula as a query which would have enabled me to use if/then as well. Now i have to be able to use SQLite and even though it seems to have some possibilty to do if/then the syntax seems to be different (haven't quite found it yet) so the formula would have to change depending on wether MySQL or SQLite is used, which doesn't suit me at all ;(

e8johan
10th October 2006, 13:32
Try having a look at SpeedCrunch - http://code.google.com/p/speedcrunch/ - the evaluator is pretty much separated from the user interface so you might be able to use it right away.

mikro
10th October 2006, 16:04
that looks good, but unfortunately there is not much documentation yet. It would probably be easier if it would compile, so i could at least watch it working. hmm, i will have a longer look at the source to see wether i can make sense of it, but i'll still be open to new ideas.

mikro
12th October 2006, 14:33
meanwhile i had a closer look at speedcrunch (and using a stable version was also able to compile it). It seems it could work for me, but it is a lot of stuff that i probably don't need (like bcmath - the app i am working on will never want to have more than 2 digits after the .).
Currently i am working on the widget to create the formula and want to try to implement a syntax highlighter there. It occurs to me, that once i have defined the syntax highlighter i might as well use it to parse through my formula later on for calculating as well?

mikro
16th October 2006, 09:58
well forget about my last post - i thought my main problem would be to translate the fomula, just now i begin to realize that C++ does not have an eval as i am used to from php. Also, as stated before, that would not have been a problem, if i was using MySQL, but unfortunately i have QSqlite.

So i see where Speedcrunch would help me. Unfortunately that still requires QT3-Supportfiles. Does anybody know of a solution that does not?

jrideout
18th October 2006, 23:22
Take at look at Function parser for C++ (http://warp.povusers.org/FunctionParser/)

mikro
19th October 2006, 18:19
that looks great, especially as it could also work with if-statements. unfortunately i don't find a possibility to ask this class for the variables it needs to have filled. The situation is that i have assets that might have 10 or 20 or more attributes (they can be freely defined by the user) and a formula (which has also been defined by the user) in the database.
I read the database, parse the formula for the variablenames and insert the values instead and then eval the formula. With the function parser i had hoped to do those two steps in one, but it requires me to know which variables it needs and even the necessary sequence ;(