I’m needing to write a program (C#) that will allow the user to create generic formulas with variables and numbers. For example:
D = A + (A - C / X)(7.8 - 6.6)
F = E + (E - C / X)(7.8 - 6.6)
FinalResult = (A + D)(0.9) + (E + F)(0.32) + B(0.1) + .023
where all variables would mean for me to go to a database and look something up based on values and return a number in its place. So A would be 2.12 for example (and the same for C and E)
Whats the best way to structure this program? How would I make my program read these formulas?
I’ve seen a little bit of the MathML but not sure how to get that started (or an example of it)
–Update–
I mentioned MathML just seeing some other questions involving it on SO. Y’all are correct, I’m not needing to display it, I’m just needing to identify what someone wants and use that to calculate something.
My variables will map to something in a database. For example, A would tell me to use the following:
Vend_Key = 3
Trmnl_Key = 5
Prod_Key = 7
which would then be used to return a number from a database.
I’m going to need a database to save the formula’s that people create. I’ll have to have a scheduled task that can run and execute these functions and save the information somewhere else.
So basically you need a calculator?
If so, then what I would do first is to search & replace the variable letters with their value, and then you can use some algorithm for evaluating the expression such as first converting it to postfix notation (much easier to evaluate in an algorithm) using something like this (google for infix to postfix algorithm for more and examples):
If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.
Repeat this step till all the characters are scanned.
When you have the postfix string ready, you can use the simple postfix evaluation algorithm which basically goes like this:
If the scannned character is an operand, add it to the stack. If the scanned character is an operator, there will be atleast two operands in the stack.
Repeat this step till all the characters are scanned.
topStack will be the result of your expression.