SOURCE

console 命令行工具 X clear

                    
>
console
$("#exp").on('keydown', function (event) {
    if (event.keyCode == 13) {
        cal($("#exp").val());
    }
});

$("#eq").click(function () {
    cal($("#exp").val());
});

$('#exp').bind('input', function () {
    var exp = $(this).val();
    var t = new ExpTree();
    if (!t.build(exp)) {
        console.log('错误!', exp);
        $('#expdiv').attr('class', 'form-group has-error has-feedback');
        $('#result').attr('style', 'background-color:#D87676');
        $('#result').html('表达式格式错误,请仔细检查!');
    } else {
        $('#expdiv').attr('class', 'form-group has-success has-feedback');
        $('#result').attr('style', '');
        <!-- $('#result').html(''); -->
        cal($("#exp").val());
    }
});

function cal(exp) {
    console.log(exp);
    var t = new ExpTree();
    t.build(exp);
    console.log('infix:', t.toInFix());
    var dtn = t.dao(t.root);
    var dt = new ExpTree();
    dt.root = dtn;
    console.log('postfix:', dt.toPostFix());
    var x0 = 1,
        x1 = 2,
        i = 0,
        overStep = false;
    while (Math.abs(x0 - x1) > 10E-9) {
        x0 = x1;
        x1 = x0 - (t.cal(t.root, x0) / dt.cal(dt.root, x0));
        ++i;
        if (i > 10E5) {
            overStep = true;
            break;
        }
    }
    if (Math.abs(x1 - Math.round(x1)) < 10E-8) {
        x1 = Math.round(x1);
    }
    $('#result').html('x = ' + x1);
    $('#result').attr('style', '');
    if (overStep) {
        $('#result').html('步数溢出,结果可能不准确。<br/> x = ' + x1);
        $('#result').attr('style', 'background-color:#FABD20');
    }
    if (isNaN(x1)) {
        $('#result').html('无解或牛顿法对此不收敛。');
        $('#result').attr('style', 'background-color:#D87676');
    }
    return x1;
}
$('#result').html('x = ' + cal($("#exp").val()));
<a href="https://github.com/iWoz/DSinJS"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>

<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading">求近似解</div>
        <div class="panel-body">
            <label for='exp'>输入以'x'为变量表达式:</label> <span class="label label-info"> 加+ 减- 乘* 除/ 乘方^ 三角函数 对数函数ln(x)和log(base,x)</span>

            <div id='expdiv' class="form-group has-success has-feedback">
                <input id="exp" class="form-control" style="max-width:100%" rows="1" placeholder="输入表达式,如:2x^4 - 3x + sin(x*x) - ln(x^3) + log(2,x) - 2" value="2x^4 - 3x + sin(x*x) - ln(x^3) + log(2,x) - 2"></input> <span class="form-control-feedback">= 0</span>

            </div>
            <div class="btn-group">
                <button class="btn btn-default btn-sm" id="eq">求解</button>
            </div>
            <div class="output" style="margin=5">
                <div class="well well-sm" id="result"></div>
            </div>
        </div>
        <div class="panel-footer">
            <div id="author">Author: <a href="http://wuzhiwei.net/about/" target="_blank">Tim Wu</a>

            </div>
        </div>
    </div>
</div>
.panel-heading {
    text-align:center;
}
#author {
    text-align:right;
}
#eq {
    margin-bottom:10px;
}
.has-feedback .form-control-feedback {
    top : 0px;
}

本项目引用的自定义外部资源