Hey guys,
I need help with an algorithm im trying to develope, maybe you'll come up with a good one. Try to think and tell me...
how can I analyze a mathematic 'question' that is written in string and make it an answer in integer?
For example, I have:
PHP Code:
public mathExampleQuestion()
{
new question[] = "3 + 3 x 3 / 3 + 3"
// How do I loop through the string cells and analyze the answer converted to integer? (decimal value)
}
I'll tell you why its hard? its hard cuz we must follow the math rules, it means '*' and '/' first, only then you can '+' or '-' them.
Got any ideas?
EDIT: By the way, I actually thought of an algorithm but isnt sure if its the best one. Loop through the string cells and solve all the '*' and '/' expressions, then when you get to an answer of the first experssion for example, you copy the answer into a new string, then the next sign must be '+' or '-' cuz its looping till it find '*' or '/' and solves the expressions with it, then stops to solve when it doesnt find it (the '*' or '/'), then after I found the first expression, I add the next expression, of for example '+' or '-'. so for conclusion, solve all the expression with '*' or '/' then post the answers of them into a new string with the signs between, I'll show a code example:
PHP Code:
public example()
{
new question[] = "3 + 3 x 3 / 3 + 3"
// Loop through the string and solve all the expressions with '*' or '/'
// after make that a new string with the signs of '+' or '-' between
// After the operation the new string would look like this:
new analyzedQuestion[] = "3 + 3 + 3"
// Then I can easily loop through this string and solve it by checking the sign
// if its - or +
}
__________________