DeltiX
Administrator
Dołączył: 20 Kwi 2006
Posty: 219
Przeczytał: 0 tematów
Pomógł: 2 razy Ostrzeżeń: 0/5
|
Wysłany: Czw 22:17, 23 Lip 2009 Temat postu: Kalkulator prosty |
|
|
[link widoczny dla zalogowanych]
Opis: Klasyczny kalkulator z klawiszami.
Kod: |
<form name="keypad" action="">
<table summary="" border="2" width="50" height="60" cellpadding="1" cellspacing="5">
<tr>
<td colspan="3" align="middle">
<input name="readout" type="text" size="24" value="0" width="100%">
</td>
<td>
</td>
<td>
<input name="btnclear" type="button" value=" c " onclick="myclear()">
</td>
<td><input name="btnclearentry" type="button" value=" ce " onclick="clearentry()">
</td>
</tr>
<tr>
<td>
<input name="btnseven" type="button" value=" 7 " onclick="numpressed(7)">
</td>
<td>
<input name="btneight" type="button" value=" 8 " onclick="numpressed(8)">
</td>
<td>
<input name="btnnine" type="button" value=" 9 " onclick="numpressed(9)">
</td>
<td>
</td>
<td>
<input name="btnneg" type="button" value=" +/- " onclick="neg()">
</td>
<td>
<input name="btnpercent" type="button" value=" % " onclick="percent()">
</td>
</tr>
<tr>
<td>
<input name="btnfour" type="button" value=" 4 " onclick="numpressed(4)">
</td>
<td>
<input name="btnfive" type="button" value=" 5 " onclick="numpressed(5)">
</td>
<td>
<input name="btnsix" type="button" value=" 6 " onclick="numpressed(6)">
</td>
<td>
</td>
<td align="middle"><input name="btnplus" type="button" value=" + " onclick="operation('+')">
</td>
<td align="middle"><input name="btnminus" type="button" value=" - " onclick="operation('-')">
</td>
</tr>
<tr>
<td>
<input name="btnone" type="button" value=" 1 " onclick="numpressed(1)">
</td>
<td>
<input name="btntwo" type="button" value=" 2 " onclick="numpressed(2)">
</td>
<td>
<input name="btnthree" type="button" value=" 3 " onclick="numpressed(3)">
</td>
<td>
</td>
<td align="middle"><input name="btnmultiply" type="button" value=" * " onclick="operation('*')">
</td>
<td align="middle"><input name="btndivide" type="button" value=" / " onclick="operation('/')">
</td>
</tr>
<tr>
<td>
<input name="btnzero" type="button" value=" 0 " onclick="numpressed(0)">
</td>
<td>
<input name="btndecimal" type="button" value=" . " onclick="decimal()">
</td>
<td colspan="3">
</td>
<td>
<input name="btnequals" type="button" value=" = " onclick="operation('=')">
</td>
</tr>
</table>
</form>
</div>
<font face="verdana, arial, helvetica" size="2">
<script language="javascript">
<!-- begin
var fkeypad = document.keypad;
var accum = 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 )
accum += parseFloat(readout);
else if ( '-' == pendingop )
accum -= parseFloat(readout);
else if ( '/' == pendingop )
accum /= parseFloat(readout);
else if ( '*' == pendingop )
accum = accum * parseFloat(readout);
else
accum = parseFloat(readout);
fkeypad.readout.value = accum;
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 myclear () {
accum = 0;
pendingop = "";
clearentry();
}
function neg () {
fkeypad.readout.value = parseFloat(fkeypad.readout.value) * -1;
}
function percent () {
fkeypad.readout.value = (parseFloat(fkeypad.readout.value) / 100) * parseFloat(accum);
}
// end -->
</script> |
Post został pochwalony 0 razy
|
|