8

我是 JavaScript 新手,我正在尝试编写一个解决线性方程的简单脚本。到目前为止,我的脚本解决了只有正负的线性方程,例如“2x + 28 - 18x = 36 - 4x + 10”。我希望它也能够解决包含乘法和除法的线性方程/代数问题,例如“2x * 3x = 4 / 2x”。

我有点知道下一步该做什么,但我认为我现在拥有的脚本可能过于复杂,并且只会使添加乘法和除法变得更加复杂。

下面是我的脚本。我希望得到一些关于如何改进和简化我已经拥有的东西以及添加乘法和除法的最佳方法的建议?

我在 JS Bin 上的脚本:http: //jsbin.com/ufekug/1/edit

我的脚本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Problem Solver</title>
<script>
window.onload = function() {
    // Total Xs on each side of equation
    // Example problem: 5x + 2 = 10 - 2x
    var leftSideXTotal = 0; // 5
    var rightSideXTotal = 0; // -2

    // Total integers on each side of equation
    // Example problem: 5x + 2 = 10 - 2x
    var leftSideIntTotal = 0; // 2
    var rightSideIntTotal = 0; // 10


    // Enter a math problem to solve
    var problem = "5x + 2 = 10 - 2x";


    // Remove all spaces in problem
    // Example problem: 5x + 2 = 10 - 2x
    problem = problem.replace(/\s/g,''); // 5x+2=10-2x

    // Add + signs in front of all - signs
    // Example problem: 5x + 2 = 10 - 2x
    problem = problem.replace(/-/gi, "+-"); // 5x+2=10+-2x

    // Split problem into left and right sides
    // Example problem: 5x + 2 = 10 - 2x
    var problemArray = problem.split("=");
    var problemLeftSide = problemArray[0]; // 5x+2
    var problemRightSide = problemArray[1]; // 10+-2x

    // Split values on each side into an array
    var problemLeftSideValues = problemLeftSide.split("+");
    var problemRightSideValues = problemRightSide.split("+");

    // Go through the left side values and add them up
    for (var i = 0; i < problemLeftSideValues.length; i++) {

        // Current value
        var currentValue = problemLeftSideValues[i];
        // Length of current value
        var currentValueLength = currentValue.length;

        if (currentValue.charAt(currentValueLength - 1) == "x") { //Check if current value is a X value

            // Remove X from end of current value
            currentValue = currentValue.split("x");

            // Add to total Xs on left side
            leftSideXTotal = Number(leftSideXTotal) + Number(currentValue[0]);

        } else {

            // Add to total integers on left side
            leftSideIntTotal = Number(leftSideIntTotal) + Number(problemLeftSideValues[i]);

        }
    }

    // Go through the right side values and add them up
    for (var i = 0; i < problemRightSideValues.length; i++) {

        // Current value
        var currentValue = problemRightSideValues[i];
        // Length of current value
        var currentValueLength = currentValue.length;

        if (currentValue.charAt(currentValueLength - 1) == "x") { //Check if current value is a X value

            // Remove X from end of current value
            currentValue = currentValue.split("x");

            // Add to total Xs on right side
            rightSideXTotal = Number(rightSideXTotal) + Number(currentValue[0]);

        } else {

            // Add to total integers on right side
            rightSideIntTotal = Number(rightSideIntTotal) + Number(problemRightSideValues[i]);

        }
    }

    // Compute
    var totalXs = (leftSideXTotal - rightSideXTotal)
    var totalIntegers = (rightSideIntTotal - leftSideIntTotal)
    var solution = (totalIntegers / totalXs)

    // Display solution
    document.getElementById("divSolution").innerText = solution;
}
</script>
</head>

<body>
<div id="divSolution"></div>
</body>
</html>
4

2 回答 2

8

您需要编写(或使用)运算符优先级解析器。

这个想法是将方程变成一棵树,例如

x + 3 = 3x - 2

真的是结构

        =
     /     \
    +       -
   / \     / \
  x   3   *   2
         / \
        3   x

每个运算符描述树的两个“分支”之间的操作。使用 javascript 对象创建结构应该不难:

function tree(lterm,op,rterm) {
   t.operator = op;
   t.left = lterm;
   t.right = rterm;
   return t;
}

expression = tree("x", "/", tree("x","+",3) );  //    x / (x+3)

然后通过操纵树,您可以求解方程,或进行计算。要评估一个表达式(没有未知数),您从终端开始遍历树,然后从一个交叉点向上到另一个交叉点。您可以用结果替换树的一部分,或用结果对其进行注释 - 将结果变量添加到tree对象。

以下是包含在树类中的一些有用方法:

  • 左转
  • 获得权利
  • 漂亮打印
  • 评估
  • evaluate("x",5) // x=5,现在计算 ...

可以通过这种方式“解析”的不仅仅是线性运算。更好的解析器将有一个运算符列表,其中包括 =*/+- 以及一元运算符: - ( ) sin cos...

我没有在 javascript 中使用运算符优先级解析器,但有些必须预先编写好。当然,这个网站上的好心人会给我的答案添加一两个很好的链接。

顺便说一句,树方法有很多应用。在电子表格中:

A2 = A1+B1

在布尔求解器中:

A = not (B or C)
C = true

在 XML 解析中:

<main>
  <part>A</part>
  <part>B</part>
</main> 
于 2013-01-07T16:18:22.667 回答
0

我定义了两个函数:

getTotalX():它将为您x提供任何输入字符串的计数。

getTotalScalars():它会给你标量(数字)的总数。

最后,您更新的代码(它仍然只做加法和减法):

<script>
window.onload = function() {
    // Total Xs on each side of equation
    // Example problem: 5x + 2 = 10 - 2x
    var leftSideXTotal = 0; // 5
    var rightSideXTotal = 0; // -2

    // Total integers on each side of equation
    // Example problem: 5x + 2 = 10 - 2x
    var leftSideIntTotal = 0; // 2
    var rightSideIntTotal = 0; // 10


    // Enter a math problem to solve
    var problem = "5x + 2 = 10 - 2x";


    // Remove all spaces in problem
    // Example problem: 5x + 2 = 10 - 2x
    problem = problem.replace(/\s/g,''); // 5x+2=10-2x

    // Add + signs in front of all - signs
    // Example problem: 5x + 2 = 10 - 2x
    problem = problem.replace(/-/gi, "+-"); // 5x+2=10+-2x

    // Split problem into left and right sides
    // Example problem: 5x + 2 = 10 - 2x
    var problemArray = problem.split("=");
    var problemLeftSide = problemArray[0]; // 5x+2
    var problemRightSide = problemArray[1]; // 10+-2x

    leftSideXTotal = getTotalX(problemLeftSide);
    leftSideIntTotal = getTotalScalars(problemLeftSide);

    rightSideXTotal = getTotalX(problemRightSide);
    rightSideIntTotal = getTotalScalars(problemRightSide);

    // Compute
    var totalXs = (leftSideXTotal - rightSideXTotal)
    var totalIntegers = (rightSideIntTotal - leftSideIntTotal)
    var solution = (totalIntegers / totalXs)

    // Display solution
    document.getElementById("divSolution").innerText = solution;

    // Find the total number of X in the string
    function getTotalX(data) {
        data = data.replace(/\s/g,'');
        xCount = 0;

        if(data.indexOf('x') != -1) {
            if (data.indexOf('+') != -1) {
                data = data.split('+');

                for(var i = 0; i < data.length; i++) {
                    xCount += getTotalX(data[i]);
                }
            } else if (data.indexOf('-') != -1) {
                data = data.split('-');

                // Single negative
                if(data[0] == "") {
                    xCount -= getTotalX(data[1]);
                } else {
                    xCount += getTotalX(data[0]);

                    for(var i = 1; i < data.length; i++) {
                        xCount -= getTotalX(data[i]);
                    }
                }
            } else {
                xCount = parseInt(data.split('x')[0]);
            }
        }

        return xCount;
    }

    // Find the total of scalars
    function getTotalScalars(data) {
        data = data.replace(/\s/g,'');
        intCount = 0;

        if (data.indexOf('+') != -1) {
            data = data.split('+');

            for(var i = 0; i < data.length; i++) {
                intCount += getTotalScalars(data[i]);
            }
        } else if (data.indexOf('-') != -1) {
            data = data.split('-');

            // Single negative
            if(data[0] == "") {
                intCount -= getTotalScalars(data[1]);
            } else {
                intCount += getTotalScalars(data[0]);

                for(var i = 1; i < data.length; i++) {
                    intCount -= getTotalScalars(data[i]);
                }
            }
        } else {
            if(data.indexOf('x') == -1) {
                intCount = parseInt(data.split('x')[0]);
            } else {
                intCount = 0;
            }
        }

        return intCount;
    }
}
</script>
于 2013-01-07T08:21:36.473 回答