1

これは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>
4

1 に答える 1

1

の複数の定義はsolve()互いに上書きされ、最終的な平方根のみが有効になります。それらを 1 つの関数に結合します。

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) ではなく平方関数 (x 2 ) です。

于 2012-11-21T20:46:49.253 に答える