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
= width * breadth
To copy to clipboard, switch view to plain text mode
say my classes are in a QMap called attributes i would do:
attributes[width]->getValue() // returns 2;
attributes[breadth]->getValue() // returns 4
attributes[width]->getValue() // returns 2;
attributes[breadth]->getValue() // returns 4
To copy to clipboard, switch view to plain text mode
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 ;(
Bookmarks