问题
var Answer = '2a + 5b' // downloaded from database
var input = '5*b+ 2*a' // user input after calculating some algebraic expression.
if (Answer == input){
// tell student he solved the math problem correctly.
}
I want a function so that the above if-statement return true. My problem is that my answer and user input is a string, and when compared they are not equal. The user however have solved the initial problem correct.
回答1:
Even though certain forms of algebraic expressions -such as low-degree polynomials-- are evidently easy to test for equality or equivalence, a general and complete method (and hence an algorithm) cannot exist, because there exist forms of algebraic expressions for which the problem (of deciding their equality) is undecidable, as it was proved by the Richardson's theorem:
Richardson's theorem establishes a limit on the extent to which an algorithm can decide whether certain mathematical expressions are equal. It states that for a certain fairly natural class of expressions, it is undecidable whether a particular expression E satisfies the equation E = 0, and similarly undecidable whether the functions defined by expressions E and F are everywhere equal (in fact, E = F if and only if E − F = 0). It was proved in 1968 by computer scientist Daniel Richardson of the University of Bath.
来源:https://stackoverflow.com/questions/60876231/function-that-check-if-two-algebraic-expressions-are-equal-in-javascript