Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 354

# Calculating nested formulas (Strategy design pattern)

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.

Thanks
TRG
0
TRG00
• 4
1 Solution

Commented:
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?

Cheers!

Stefan
0

Commented:

http://64.78.49.204/TIPatterns-0.9.zip

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.
0

Author Commented:
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
TRG
0

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

3*4+5

results in

1:     *
2:   3    +
3:       4    5

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

Commented:
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.
0

Commented:
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.

wayside
EE Cleanup Volunteer
0

## Featured Post

• 4
Tackle projects and never again get stuck behind a technical roadblock.