Click here to Skip to main content
16,022,362 members
Please Sign up or sign in to vote.
1.57/5 (4 votes)
See more:
To start with i have a formula i.e. A = B + C - D .

I want to dynamically set target and generate new formula.

For example,
If i set target as D by changing any other variable A,B,or C.
Then i should get the formula as D = B + C - A. and D's value should be recalculated.

I can set any target and can change any one variable at a time.

Note it has to be dynamic as i have many formulas like this to be implemented.


Any help will be great for me.


Thanks
Posted

1 solution

This is a greatly simplified example of Computer algebra system (CAS):
http://en.wikipedia.org/wiki/Computer_algebra_system[^].

Traditional approach to it is based on parsing some symbolic language. You need to parse the expression and output the syntax errors if some are found. Please see some CodeProject articles:
Math Parser[^],
Math Parser .NET[^],
Parsing Mathematical Expressions with muParser[^],
a Tiny Parser Generator v1.2[^].

You can find some more: http://www.codeproject.com/search.aspx?q=Parser+%22C%23%22&doctypeid=1[^].

[EDIT]

So, what to do with this? You should parse the expression into the data structure called binary expression tree: http://en.wikipedia.org/wiki/Expression_tree[^].

Such a simple thing as substitution of a numeric value and calculation of the numeric value of the expression would be just traversing a tree and performing the calculation on each node, passing the result to child nodes. If you want, you can compiler it into IL code during runtime, using System.Reflection.Emit — this is pretty advanced topic; mastering it would require good IL experience; and debugging would be pretty difficult.

[END EDIT]

There is another approach to this: you can wrap your expression in some valid C# or VB.NET code and try to compile it as an assembly using CodeDOM. Likewise, it can get all the compilation error diagnostics. Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

—SA
 
Share this answer
 
v3
Comments
ridoy 8-Aug-13 2:14am    
very calculated answer on this type on this type of question,+5.
Sergey Alexandrovich Kryukov 8-Aug-13 11:37am    
Actually, not quite: at first, I failed to express main point, which is not added, after [EDIT].
Thank you very much.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900