2012-11-21 3 views
1

이것은 자바 스크립트 계산기라고 생각되지만 이상한 사각형을 제외하고는 계산이 수행되지 않습니다. 예를 들어, 덧셈, 뺄셈, 나눗셈과 곱셈은 숫자를 삽입하더라도 답을주지 못합니다. 나는 그것이 무엇이 잘못되었는지 모른다.계산기에서 제곱 된 작업 만 수행

도움 주셔서 감사합니다. solve()

</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

일부 alert() 중지 점을 넣고 실패한 부분을 확인하십시오. –

+2

'solve()'함수를 계속 재정의하고 있습니다. 마지막은'sqrt'이므로, 하나가 붙습니다. –

+2

단계별 코드를 단계별로 살펴 보았습니까? Firebug와 Chrome Dev Tools 모두이 기능을 지원합니다. – tjameson

답변

1

다중 정의는 단지 최종 제곱근 한 효과를 가지고 서로를 덮어 씁니다. 그것들을 하나의 기능으로 결합하십시오. 당신의 "SQRT"기능이 실제로 사각형 기능 (X 2),하지 제곱근 (√ X)입니다 그런데

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) 
    } 
} 

.