Is it possible to convert A string that is an equation with a variable into a equation?

萝らか妹 提交于 2020-01-25 12:16:34

问题


I need to convert a string returned from prompt into an equation, however the parseFloat returns as only the first number, and symbols in an equation, and stops at the variable. The variable will always = x. The program is designed to convert an algebraic expression say 15*x(5^4-56)*17/x=15 into an expression, and calculate the value of x. If someone could show me how to do this, it would help dramatically. I am currently using multiple prompts, having the user put in the equation before x, then the equation after x, then it inserts a variable in between the two, and calculates it's value.

Edit:

I have no variables predefined, and it must work in equations where x > 1000, or x != //an integer.

Thanks in advance!


回答1:


Seems to be a complex problem...

This is a solution for a simple relaxed version of your problem. Hope you can use some components of this.

Constraints:

  1. answer for x should be integers between 0 and 1000
  2. the left hand side of the expression should be proper javascript syntax

var input = prompt("enter the equation");  //eg: x*x+x+1=241
var parts = input.split('=');

//solving equation starts
var x = 0;
var temp = eval(parts[0]);
while (temp != parts[1] && x<1000){
   x++;
   temp = eval(parts[0]);
}
var ans = (x<1000)?"answer is "+x:"this program cannot solve this";
//solving equation finishes
  
alert(ans);

You can replace the "solving equation" part with some numerical methods used in computer science to solve equations (more details here) . You will have to parse the left side of equation and map them to proper javascript expressions (as a string to execute with eval()) if you want to allow users to use your syntax.




回答2:


Javascript can evaluate strings using the eval function, but the variable as to be defined before hand, and the equation has to be formatted in way that javascript can understand:

var x = 15
var string = "15*x*17/x"
eval(string)

Your example: "15*x(5^4-56)*17/x=15" would not run however, because it would evaluate x(5^4-56) as a javascript expression, which is invalid.




回答3:


Using all the info, and other mehtods I found about this, I have put together a communinty answer. Anyone is invited to change and/or add their methods to this.

In order to do this with the least work possible for the user and coder, you would implement the following code.

var input = prompt("enter the equation");  //eg: x*x+x+1=241
var parts = input.split('=');

//solving equation starts
var x = 0; //Or the lowest possible value of "x"
var temp = eval(parts[0]);
while (temp != parts[1] && x<1000){ // && x < The highest number to evaluate
   x++; //Add the increment (determines the maximum amount of digits) eg x+0.1 for tenths max, x+2 for only even integers etc.
   temp = eval(parts[0]);
}
var ans = (x<1000)?"answer is "+x:"this program cannot solve this"; //make sure x< is the same as line 7.
//solving equation finishes
  
alert(ans);

But, this runs very slowly if you allow tenths, or a range larger than 2000.`

A faster way of running this would be to define arrays allowing any variable (instead of just x) and a different eveulation process such as here. (do the right click view html and click on the first js source to see code) but, this is 2k lines. Both are usable, but the second is more efficient, and can solve multivariate equations.



来源:https://stackoverflow.com/questions/27431870/is-it-possible-to-convert-a-string-that-is-an-equation-with-a-variable-into-a-eq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!