Is it possible to convert A string that is an equa

2019-09-04 06:19发布

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!

3条回答
Fickle 薄情
2楼-- · 2019-09-04 06:55

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.

查看更多
倾城 Initia
3楼-- · 2019-09-04 06:57

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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-09-04 07:05

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.

查看更多
登录 后发表回答