Source Code / Coding untuk kalkulator....contoh kalkulator seperti di atas....
<center>
<form action="" name="Keypad">
<b>
</b><table>
</table>
<table border="2" cellpadding="1" cellspacing="5" style="height: 60px; width: 50px;">
<tbody>
<tr>
<td align="center" colspan="3"><input name="ReadOut" size="24" type="Text" value="0" width="100%" />
</td>
<td></td>
<td><input name="btnClear" onclick="Clear()" type="Button" value=" C " />
</td>
<td><input name="btnClearEntry" onclick="ClearEntry()" type="Button" value=" CE " />
</td>
</tr>
<tr>
<td><input name="btnSeven" onclick="NumPressed(7)" type="Button" value=" 7 " />
</td>
<td><input name="btnEight" onclick="NumPressed(8)" type="Button" value=" 8 " />
</td>
<td><input name="btnNine" onclick="NumPressed(9)" type="Button" value=" 9 " />
</td>
<td></td>
<td><input name="btnNeg" onclick="Neg()" type="Button" value=" +/- " />
</td>
<td><input name="btnPercent" onclick="Percent()" type="Button" value=" % " />
</td>
</tr>
<tr>
<td><input name="btnFour" onclick="NumPressed(4)" type="Button" value=" 4 " />
</td>
<td><input name="btnFive" onclick="NumPressed(5)" type="Button" value=" 5 " />
</td>
<td><input name="btnSix" onclick="NumPressed(6)" type="Button" value=" 6 " />
</td>
<td></td>
<td align="center"><input name="btnPlus" onclick="Operation('+')" type="Button" value=" + " />
</td>
<td align="center"><input name="btnMinus" onclick="Operation('-')" type="Button" value=" - " />
</td>
</tr>
<tr>
<td><input name="btnOne" onclick="NumPressed(1)" type="Button" value=" 1 " />
</td>
<td><input name="btnTwo" onclick="NumPressed(2)" type="Button" value=" 2 " />
</td>
<td><input name="btnThree" onclick="NumPressed(3)" type="Button" value=" 3 " />
</td>
<td></td>
<td align="center"><input name="btnMultiply" onclick="Operation('*')" type="Button" value=" * " />
</td>
<td align="center"><input name="btnDivide" onclick="Operation('/')" type="Button" value=" / " />
</td>
</tr>
<tr>
<td><input name="btnZero" onclick="NumPressed(0)" type="Button" value=" 0 " />
</td>
<td><input name="btnDecimal" onclick="Decimal()" type="Button" value=" . " />
</td>
<td colspan="3"></td>
<td><input name="btnEquals" onclick="Operation('=')" type="Button" value=" = " />
</td>
</tr>
</tbody></table>
<b>
</b></form>
</center>
<span style="font-family: Verdana, Arial, Helvetica; font-size: x-small;">
<script language="JavaScript">
<!-- Begin
var FKeyPad = document.Keypad;
var Accumulate = 0;
var FlagNewNum = false;
var PendingOp = "";
function NumPressed (Num) {
if (FlagNewNum) {
FKeyPad.ReadOut.value = Num;
FlagNewNum = false;
}
else {
if (FKeyPad.ReadOut.value == "0")
FKeyPad.ReadOut.value = Num;
else
FKeyPad.ReadOut.value += Num;
}
}
function Operation (Op) {
var Readout = FKeyPad.ReadOut.value;
if (FlagNewNum && PendingOp != "=");
else
{
FlagNewNum = true;
if ( '+' == PendingOp )
Accumulate += parseFloat(Readout);
else if ( '-' == PendingOp )
Accumulate -= parseFloat(Readout);
else if ( '/' == PendingOp )
Accumulate /= parseFloat(Readout);
else if ( '*' == PendingOp )
Accumulate *= parseFloat(Readout);
else
Accumulate = parseFloat(Readout);
FKeyPad.ReadOut.value = Accumulate;
PendingOp = Op;
}
}
function Decimal () {
var curReadOut = FKeyPad.ReadOut.value;
if (FlagNewNum) {
curReadOut = "0.";
FlagNewNum = false;
}
else
{
if (curReadOut.indexOf(".") == -1)
curReadOut += ".";
}
FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry () {
FKeyPad.ReadOut.value = "0";
FlagNewNum = true;
}
function Clear () {
Accumulate = 0;
PendingOp = "";
ClearEntry();
}
function Neg () {
FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1;
}
function Percent () {
FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accumulate);
}
// End -->
</script>
<span style="font-family: Tahoma;"><a href="http://www.javascriptfreecode.com/" target="_blank"><span style="font-size: 8pt; text-decoration: none;">JavaScript Free Code</span></a></span></span>
No comments:
Post a Comment