<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CALC</title>
    <script>
        // 숫자와 연산기호 입력
        function input_num(calc_num){
            document.getElementById("formula").value += calc_num;
        }

        // 사칙연산 수행
        function calculate(result){
           var formula = document.getElementById("formula").value;
           result = eval(formula);

           document.getElementById("result").value = result;
        }

        // 초기화
        function reset(){
            document.getElementById("formula").value = null;
            document.getElementById("result").value = null;
        }


    </script>
</head>
<body>
    <div align='center'>
        <input type="text" name="formula" id="formula" size="30"><br> <!-- size:30 == 180pt -->
        <input type="text" name="result" id="result" size="30"><br>

        <input type="button" value="초기화" style="width: 90pt;" onclick="reset()">
        <input type="button" value="=" size="15" id="result_btn" style="width: 90pt;" onclick="calculate()"><br>

        <input type="button" style="width: 45pt;" id="btn" value="7" onclick="input_num('7')">
        <input type="button" style="width: 45pt;" id="btn" value="8" onclick="input_num('8')">
        <input type="button" value="9" style="width: 45pt;" id="btn" onclick="input_num('9')">
        <input type="button" value="+" style="width: 45pt;" id="btn" onclick="input_num('+')"><br>

        <input type="button" value="4" style="width: 45pt;" id="btn" onclick="input_num('4')">
        <input type="button" value="5" style="width: 45pt;" id="btn" onclick="input_num('5')">
        <input type="button" value="6" style="width: 45pt;" id="btn" onclick="input_num('6')">
        <input type="button" value="-" style="width: 45pt;" id="btn" onclick="input_num('-')"><br>

        <input type="button" value="1" style="width: 45pt;" id="btn" onclick="input_num('1')">
        <input type="button" value="2" style="width: 45pt;" id="btn" onclick="input_num('2')">
        <input type="button" value="3" style="width: 45pt;" id="btn" onclick="input_num('3')">
        <input type="button" value="*" style="width: 45pt;" id="btn" onclick="input_num('*')"><br>

        <input type="button" value="0" style="width: 60pt;" id="btn" onclick="input_num('0')">
        <input type="button" value="." style="width: 60pt;" id="btn" onclick="input_num('.')"> 
        <input type="button" value="/" style="width: 60pt;" id="btn" onclick="input_num('/')"><br>
    </div>
</body>
</html>

 

'Web > Develop' 카테고리의 다른 글

[Javascript] serialization(직렬화)  (0) 2020.11.04
[Javascript] IIFE  (0) 2020.10.28
[PHP] isset()  (0) 2020.06.14
[PHP] PDO로 데이터베이스(mysql) 연결  (0) 2020.06.14
[PHP] phpinfo  (0) 2020.06.12

+ Recent posts