只有平方在计算器上工作

时间:2012-11-21 20:39:06

标签: javascript

这假设是一个JavaScript计算器,但除了奇数的正方形之外似乎没有计算。例如,即使我插入数字,加,减,除和乘也不会给我答案。我不知道它有什么问题。

感谢您的帮助!

    </style>

    <script type="text/javascript">
        <!-- JAVASCRIPT FUNCTIONS GO HERE -->
        var numberOne="", symbol="", numberTwo=""
        function equation (number){
            if(symbol==""){
                numberOne+=number
                document.getElementById('equation').value = numberOne;
            }
            else if(symbol!=""){
                numberTwo+=number
                document.getElementById('equation').value = numberTwo;
            }   
        }
        function getSymbol(operator){
            symbol=operator
            document.getElementById('equation').value = symbol;
        }

        function clear_equation () {
            document.getElementById('equation').value = "";
            numberOne=""
            numberTwo=""
            symbol=""
        }

        function remove () {
            Current = "0";{
            document.Calculator.Display.value = Current;
            }
        }

        function add(num1, num2) {
            output=Number(num1)+Number(num2)
            document.getElementById('equation').value = output;
        }
        function subtract(num1, num2) {
            output=Number(num1)-Number(num2)
            document.getElementById('equation').value = output;
        }
        function multiply(num1, num2) {
            output=Number(num1)*Number(num2)
            document.getElementById('equation').value = output;
        }
        function divide(num1, num2) {
            output=Number(num1)/Number(num2)
            document.getElementById('equation').value = output;
        }
        function sqrt(num1) {
            output=Number(num1)*Number(num1)
            document.getElementById('equation').value = output;
        }
        function sin(num1) {
            output=Math.sin(num1)
            document.getElementById('equation').value = output;
        }
        function solve (add) {
            if(symbol=="+"){
                add(numberOne, numberTwo)
            }
        }
        function solve (subtract) {
            if(symbol=="-"){
                subtract(numberOne, numberTwo)
            }
        }
        function solve (mutiply){
            if(symbol=="*"){
                mutiply(numberOne, numberTwo)
            }
        }
        function solve (divide) {
            if(symbol=="/"){
                divide(numberOne, numberTwo)
            }
        }
        function solve (sqrt) {
            if(symbol=="sqrt"){
                multiply(numberOne, numberOne)
            }
        }

    </script>
</head>
<body>
    <div class="container">
        <table cellspacing="0">
            <tr>
                <td colspan="5"><input class="equ" type="input" disabled="disabled" id="equation" value=""/></td>
            </tr>
            <tr>
                <td><input class="buttons" type="button" value="sin" onclick="getSymbol (this.value)" /></td>
                <td><input class="buttons" type="button" value="cos" onclick="getSymbol (this.value)" /></td>
                <td><input class="buttons" type="button" value="tan" onclick="getSymbol (this.value)" /></td>
                <td><input class="buttons" type="button" value="sqrt" onclick="getSymbol (this.value)" /></td>
            </tr>
            <tr>
                <td><input class="buttons" type="button" value="&larr;" onclick="remove ();"/></td>
                <td colspan="2"><input class="buttons" type="button" value="C" onclick="clear_equation (this.value)" /></td>
                <td><input class="buttons" type="button" value="/" onclick="getSymbol (this.value)"/></td>
            </tr>
            <tr>
                <td><input class="buttons" type="button" value="7" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="8" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="9" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="*" onclick="getSymbol (this.value)"/></td>
            </tr>
            <tr>
                <td><input class="buttons" type="button" value="4" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="5" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="6" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="-" onclick="getSymbol (this.value)"/></td>
            </tr>
            <tr>
                <td><input class="buttons" type="button" value="1" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="2" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="3" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="+" onclick="getSymbol (this.value)"/></td>
            </tr>
            <tr>
                <td colspan="2"><input class="buttons" type="button" value="0" onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="." onclick="equation (this.value)"/></td>
                <td><input class="buttons" type="button" value="=" onclick="solve ();"/></td>
            </tr>
        </table>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

solve()的多个定义只是相互覆盖,只有最终的平方根生效。将它们组合成一个函数。

function solve() {
    if(symbol=="+"){
        add(numberOne, numberTwo)
    }
    else if(symbol=="-"){
        subtract(numberOne, numberTwo)
    }
    else if(symbol=="*"){
        mutiply(numberOne, numberTwo)
    }
    else if(symbol=="/"){
        divide(numberOne, numberTwo)
    }
    else if(symbol=="sqrt"){
        sqrt(numberOne, numberOne)
    }
}

顺便说一句,你的“sqrt”函数实际上是一个平方函数(x 2 ),而不是平方根(√x)。