Write a function to solve a simple linear equation
I开发者_StackOverflow 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.
How is the equation entered? In text-form? As coefficients?
ax + b = c
of course has the solution x = (c - b) / a
.
For parsing, you could use regular expressions.
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.
精彩评论