본문 바로가기

Web/JavaScript

[JS&HTML]Calculator



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<html>
<head>
   <title></title>
<script language="javascript">
    var flag = false;
    function input(num) {
        if (flag == false) {
            cal.text.value="";
            flag = true;
        }
        cal.text.value += num;
    }
    function posit() {
        answer();
        cal.text.value = (cal.text.value)*-1;
    }
    function answer() {
        var str = cal.text.value;
         var strArray = str.split('^');
         var findValue = "^";
         cal.text.value = eval(cal.text.value);
 
         if (str.indexOf(findValue) != -1) {
            //만약 내 출력 값에 ^가 들어간다면 
            
            cal.text.value = Math.pow(strArray[0],strArray[1]);   
            
         }
    }
    function doSin() {
        cal.text.value = Math.sin((document.cal.text.value*3.141592)/180);
    }
    function doCos() {
        cal.text.value = Math.cos((document.cal.text.value*3.141592)/180);
    }
    function doTan() {
        cal.text.value = Math.tan((document.cal.text.value*3.141592)/180);
    }
    function cls() {
        if (flag==true) {
            flag = false;
            cal.text.value="0";
        }
    }
</script>
</head>
<body>
    <form name=cal>
    <center>
        <input type="text" name="text" value="0" style="text-align: right;" id="text">
        <br>
        <input type="button" value="Clear" onclick="cls()">
        <input type="button" value="=" onclick="answer()">
        <br>
        <table>
            <tr>
                <td>
                    <input type="button" name="number" value="1" onclick="input(this.value)">
                    <input type="button" name="number" value="2" onclick="input(this.value)">
                    <input type="button" name="number" value="3" onclick="input(this.value)">
                </td>
                <td>
                    <input type="button" name="operator" value="+" onclick="input(this.value)" >    
                </td>
                <td>
                    <input type="button" name="operator" value="x^y" onclick="input('^')">    
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" name="number" value="4" onclick="input(this.value)">
                    <input type="button" name="number" value="5" onclick="input(this.value)">
                    <input type="button" name="number" value="6" onclick="input(this.value)">
                </td>
                <td>
                    <input type="button" name="operator" value="-" onclick="input(this.value)">
                </td>
                <td>
                    <input type="button" name="operator" value="sin" onclick="doSin()">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" name="number" value="7" onclick="input(this.value)">
                    <input type="button" name="number" value="8" onclick="input(this.value)">
                    <input type="button" name="number" value="9" onclick="input(this.value)">
                </td>
                <td>
                    <input type="button" name="operator" value="*" onclick="input(this.value)">
                </td>
                <td>
                    <input type="button" name="operator" value="cos" onclick="doCos()">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" name="number" value="0" onclick="input(this.value)">
                    <input type="button" name="number" value="+/-" onclick="posit()">
                    <input type="button" name="number" value="." onclick="input(this.value)">
                </td>
                <td>
                    <input type="button" name="operator" value="/" onclick="input(this.value)">
                </td>
                <td>
                    <input type="button" name="operator" value="tan" onclick="doTan()">
                </td>
            </tr>
        </table>
    </center>
</form>
</body>
</html
cs

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

[AJAX]  (0) 2018.05.09
[JavaScript]회원가입 만들어보기!  (3) 2018.04.07
[HTML&JS]웹에서 이미지 사진 변환  (1) 2018.04.06
[HTML&JAVASCRIPT]달력 만들기  (1) 2018.04.06