开发者

Write a function to solve a simple linear equation

开发者 https://www.devze.com 2023-01-16 12:23 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消