I need to know how to write a function to solve a simple linear equation like 2x +1 = 5
. How would one do this? If anyone can show some code or point me to a site, it would be much appreciated.
views:
57answers:
2
+1
A:
How is the equation enterd? In text-form? As coefficients?
ax + b = c
of course has the solution x = (c - b) / a
.
For parsing, you could use regular expressions.
Dario
2010-09-13 18:03:28
I want to be able to enter it as `2x +1 = 5` then have the function return `x = 2`
agentbanks217
2010-09-13 18:22:00
+1
A:
Parse the expression into a simple tree, then use basic math to solve it. I'd start by converting it to postfix notation and then evaluating that.
Jim Brissom
2010-09-13 18:03:47