Results 1 to 7 of 7

Thread: eval or better?

  1. #1
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default eval or better?

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

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: eval or better?

    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.

  3. The following user says thank you to e8johan for this useful post:

    mikro (10th October 2006)

  4. #3
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: eval or better?

    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.

  5. #4
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: eval or better?

    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?

  6. #5
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: eval or better?

    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?

  7. #6
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: eval or better?

    Take at look at Function parser for C++

  8. #7
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: eval or better?

    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 ;(

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.