Calculator
<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>
'Web Programming > HTML CSS JAVASCRIPT' 카테고리의 다른 글
[CSS] 선택자 (0) | 2018.05.09 |
---|---|
[JavaScript] 달력 만들기 (0) | 2018.04.15 |
[JavaScript] 이미지 자동 변경 (0) | 2018.04.15 |
[JavaScript] 대화상자와 함수 (0) | 2018.04.08 |
[JavaScript] JavaScript란? (4) | 2018.04.08 |