Calculating nested formulas (Strategy design pattern)

Posted on 2005-05-05
Last Modified: 2008-02-01
I need some help on this problem.

I want to add this feature to a c++ spread-sheet program. Where the user can add formulas (avg...) to a library, and create new formulas from scratch or a combination of the existing library.
And a function that takes the nested formula apart, and calculates it.
When implementing multiple formulas I thought that is would be useful to use the Strategy design pattern.

But as a newcomer to c++, I have little idea how to effectively transform this and add the design pattern. Asking, Experts for help, please.

Question by:TRG00
    LVL 12

    Expert Comment

    Hi TRG00,
    I don't think a strategy pattern is a feasible choice here. If you want to have formula handling, use a syntax tree. You can then identify identical subexpressions, or perform simplifications. A strategy pattern would only obfuscate the simple arithmetic calculations here. Maybe you want a chain of responsibility pattern for the various parse and simplification stages?

    BTW: The description is a bit vague - do you want some GUI where a user can assemble a formula with a couple of mouse clicks?


    LVL 12

    Expert Comment

    BTW: Try this download link for Bruce Eckel's "Thinking in Patterns":

    The book is not published yet (it's been in this stage for several years) and is far from complete, but it has some very interesting pattern descriptions in it.

    Author Comment

    Sorry for this simplistic questions.
    I am using QT in Linux, have the complete spread-sheet running (copy, paste, save….)
    So would like to add function feature =(avg(A1;A10) * 10)
    Now I should take the nested function apart in a syntax tree, and evaluate each brach.
    Where my definitions/implementation of functions(Min, Max..) will be stored in a “function Class”

    Tried to find more specific info on syntax tree, how one uses it, but my searches in Google have been futile.

    Thanks for the effort
    LVL 12

    Expert Comment

    A syntax tree is a tree where the operators are the root node of a subtree, i.e.,


    results in

    1:     *
    2:   3    +
    3:       4    5

    (let's see if the EE displays this correctly)
    LVL 12

    Accepted Solution

    OK, looks good. Ooops, the tree displays another expression it's 3*(4+5), but you get the picture.

    So, the root node of the expression is '*' with its left branch containing '3' and no more subnodes, and the right branch contains the sub-expression (4+5), consisting of a sub-node with the operator '+', a left branch containing '4' and no more subnodes, and a right branch containing '5' and no more subnodes.

    In this kind of tree you can store any kind of expression, including functions with any number of arguments - you just need a tree which supports more an arbitrary number of subnodes. Calculating the result is easy, it's just recursing through the tree. A tree node could contain a functor instance (function class), which does this for you.

    You probably don't need simplification rules for a spreadsheet, but they're also quite easy to implement.

    Maybe you could also store in the functions which kind of arguments they expect - such as matrices, vectors, single scalars, or booleans.
    LVL 14

    Expert Comment

    No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
    I will leave the following recommendation for this question in the Cleanup topic area:

    Accept: stefan73

    Any objections should be posted here in the next 4 days. After that time, the question will be closed.

    EE Cleanup Volunteer

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now