How to setup Math program
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 开发者_如何学Goa 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):
- Scan the Infix string from left to right.
- Initialise an empty stack.
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.
- If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.
Repeat this step till all the characters are scanned.
- (After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.
- Return the Postfix string.
When you have the postfix string ready, you can use the simple postfix evaluation algorithm which basically goes like this:
- Scan the Postfix string from left to right.
- Initialise an empty stack.
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.
- If the scanned character is an Operator, then we store the top most element of the stack(topStack) in a variable temp. Pop the stack. Now evaluate topStack(Operator)temp. Let the result of this operation be retVal. Pop the stack and Push retVal into the stack.
Repeat this step till all the characters are scanned.
- After all characters are scanned, we will have only one element in the stack. Return topStack.
topStack will be the result of your expression.
I'm working on a project my self that takes user entered formula as well. The way I've been planning on working it is parsing out the various letters. and then doing an expression tree.
Each operator has 2 values. Create a class that holds 2 values and operator type. the 2 values can be either values or more operators. When you evaluate the tree it will call the top operator which will call it's children, which call their children and so on till the evaluation has been completed and the result returned.
For reading: take the string passed to the program and start parsing it. scan first for multiplication and division. where these occur you can simply into single values, same with parenthesis. with those values entered, search now for addition and subtraction. enter these into the expression tree with what ever children then now have. when that's done you have the entire equation in the expression tree.
精彩评论