2017년 6월 3일 토요일

JavaScript : 계산기 소스

<BODY onLoad="f.txt.focus()">
<p align=center>
<form name="f">
<table border=1 width=300 height=200>
<tr>
<td colspan=5>
<input type="text" name="txt" size="30" onKeydown="if(event.keyCode==13)f.txt.focus()">
</td>
</tr>
<tr align=center>
<td><input type="button" value="  7  " onClick="inter(7)"></td>
<td><input type="button" value="  8  " onClick="inter(8)"></td>
<td><input type="button" value="  9  " onClick="inter(9)"></td>
<td><input type="button" value="  *  " onClick="inter('*')" ></td>
<td><input type="button" value="  /  " onClick="inter('/')" ></td>
</tr>
<tr align=center>
<td><input type="button" value="  5  " onClick="inter(5)"></td>
<td><input type="button" value="  6  " onClick="inter(6)"></td>
<td><input type="button" value="  4  " onClick="inter(4)"></td>
<td><input type="button" value="  +  " onClick="inter('+')"></td>
<td><input type="button" value="  -  " onClick="inter('-')" v></td>
</tr>
<tr align=center>
<td><input type="button" value="  1  " onClick="inter(1)"></td>
<td><input type="button" value="  2  " onClick="inter(2)"></td>
<td><input type="button" value="  3  " onClick="inter(3)"></td>
<td colspan=2 align=center><input type="button" value="  %  " onClick="inter('%')"></td>
</tr>
<tr>
<td align=center><input type="button" value="  0  " onClick="inter(0)"></td>
<td colspan=2 align=center><input type="button" value="  =  " onClick="cal3()"></td>
<td colspan=2 align=center><input type="reset" value="  c  " onClick="inter('c')" ></td>
</tr>
</table>
</form>
</p>
<script language="javascript">
<!--
var x=0;
function inter(num) {
if(x==0)
f.txt.value+=num;
else {
f.txt.value=num;
x=0;
}
}
function cal3() {
f.txt.value=eval(f.txt.value);
x=1;  //계산을 하면 x=1
}
-->
</script>