Alyanto
asked on
Evaluate a simple string equation
I would like to be able to build a small string which will be evaluated as a mathematical calculation
all evaluation will always be formatted in this manner.
number operator number operator number operator number.
The only operators used are the basic + - / * set.
There will be a high volume of calculations producing every combination. The results will be listed as either meeting a predefined result or not.
The project is based around an alternative encryption model I have been considering for sometime.
all evaluation will always be formatted in this manner.
number operator number operator number operator number.
The only operators used are the basic + - / * set.
There will be a high volume of calculations producing every combination. The results will be listed as either meeting a predefined result or not.
The project is based around an alternative encryption model I have been considering for sometime.
This is not a question. So it is very difficult to provide an answer to you. What do you need?
And the question is?
ASKER
Well the question is: How to evaluate a string as a mathematical formula.
How and which how is the best is the question?
I had thought the first sentence aluded to the question.
How and which how is the best is the question?
I had thought the first sentence aluded to the question.
Well there is no class in C# that will dynamically parse a math string / equation and and calculate the results. So that means you will need to code it. Please see this article on the subject, CodeDom Calculator - Evaluating C# Math Expressions Dynamically.
The standard way of doing this is by using operator precedence and operator + operand stacks.
First tokenise the string (in your case splitting the string on spaces would do).
Walk through the array and push operators on the operator stack and operands on the operand stack.
When pushing an operator
Check the precedence of the operator (* / has higher precedence than - +) being pushed with the top operator on the stack
- if the stack top is higher then
pop the top of the operator stack and the top two operands from the operand stack.
Perform the operation and push the result back on the operand stack
Repeat the above process until the top of the operator stack is less than or equal to the new operator - at which point push the new.
When you have run out of tokens loop through the process of popping two operands and an operator and pushing the result to the operand stack until both stacks are empty - you will now have your result.
First tokenise the string (in your case splitting the string on spaces would do).
Walk through the array and push operators on the operator stack and operands on the operand stack.
When pushing an operator
Check the precedence of the operator (* / has higher precedence than - +) being pushed with the top operator on the stack
- if the stack top is higher then
pop the top of the operator stack and the top two operands from the operand stack.
Perform the operation and push the result back on the operand stack
Repeat the above process until the top of the operator stack is less than or equal to the new operator - at which point push the new.
When you have run out of tokens loop through the process of popping two operands and an operator and pushing the result to the operand stack until both stacks are empty - you will now have your result.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the solution, I finally got the opportunity to try it out this morning and I am really pleased with it. Cheers A***