Simple Calculator

[Total: 0    Average: 0/5]

This shows how to craete a simple calculator.

Body Code:

<CENTER><FONT SIZE=+0>Regular Calculator</FONT></CENTER>

<CENTER><FORM name="Keypad" action="">
</CENTER>

<CENTER><TABLE WIDTH="50%" >
<CAPTION>&nbsp;
<CENTER><TABLE BORDER=2 CELLSPACING=0 WIDTH="10" HEIGHT="30" >
<TR>
<TD ALIGN=CENTER COLSPAN="3"><INPUT name="ReadOut" type="Text" size=10 value="0" width=80%></TD>

<TD </TD></TD>

<TD><INPUT name="btnClear" type="Button" value="  C  " onclick="Clear()"></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=CENTER><INPUT name="btnPlus" type="Button" value="  +  " onclick="Operation('+')"></TD>

<TD ALIGN=CENTER><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=CENTER><INPUT name="btnMultiply" type="Button" value="  *  " onclick="Operation('*')"></TD>

<TD ALIGN=CENTER><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></CENTER>
&nbsp;</CAPTION>

<TR>
<TD></TD>
</TR>
</TABLE></CENTER>
</FORM>
<BR><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 *= 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 Clear () {
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>

Times Viewed: 1

Leave a Reply

Your email address will not be published. Required fields are marked *