Link to home
Start Free TrialLog in
Avatar of TRG00
TRG00

asked on

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
Avatar of stefan73
stefan73
Flag of Germany image

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
BTW: Try this download link for Bruce Eckel's "Thinking in Patterns":

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.
Avatar of TRG00
TRG00

ASKER

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
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)
ASKER CERTIFIED SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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