Taschenrechner | Beispiel mit JavaScript

Einfacher JavaScript Taschenrechner

Sourcecode:

<script language="JavaScript">

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);
}
</script>

<form name="Keypad" action>
 <table>
  <tr>
   <td>
     <table border="2" width="100" height="60" cellpadding="0" cellspacing="10" bgcolor="#BBBBFF">
      <tr>
       <td colspan="5"><input name="ReadOut" type="Text" size="31" value="0" width="100%" readonly> </td>
      </tr>
      <tr>
       <td align="middle"><input name="btnSeven" type="Button" value="  7  " onclick="NumPressed(7)"> </td>
       <td align="middle"><input name="btnEight" type="Button" value="  8  " onclick="NumPressed(8)"> </td>
       <td align="middle"><input name="btnNine" type="Button" value="  9  " onclick="NumPressed(9)"> </td>
       <td align="middle"><input name="btnNeg" type="Button" value="  +/- " onclick="Neg()"> </td>
       <td align="middle"><input name="btnPercent" type="Button" value="  %  " onclick="Percent()"> </td>
      </tr>
      <tr>
       <td align="middle"><input name="btnFour" type="Button" value="  4  " onclick="NumPressed(4)"> </td>
       <td align="middle"><input name="btnFive" type="Button" value="  5  " onclick="NumPressed(5)"> </td>
       <td align="middle"><input name="btnSix" type="Button" value="  6  " onclick="NumPressed(6)"> </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 align="middle"><input name="btnOne" type="Button" value="  1  " onclick="NumPressed(1)"> </td>
       <td align="middle"><input name="btnTwo" type="Button" value="  2  " onclick="NumPressed(2)"> </td>
       <td align="middle"><input name="btnThree" type="Button" value="  3  " onclick="NumPressed(3)"> </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 align="middle"><input name="btnZero" type="Button" value="  0  " onclick="NumPressed(0)"> </td>
       <td align="middle"><input name="btnDecimal" type="Button" value="   .  " onclick="Decimal()"> </td>
       <td align="middle"><input name="btnEquals" type="Button" value="  =  " onclick="Operation('=')"> </td>
       <td align="middle"><input name="btnClear" type="Button" value=  C    onclick="Clear()"> </td>
       <td align="middle"><input name="btnClearEntry" type="Button" value= CE  onclick="ClearEntry()"> </td>
      </tr>
     </table>
    </td>
   </tr>
 </table>
</form>

Sponsoren und Investoren

Sponsoren und Investoren sind jederzeit herzlich willkommen!
Wenn Sie die Information(en) auf dieser Seite interessant fanden, freuen wir uns über eine kleine Spende. Empfehlen Sie uns bitte auch in Ihren Netzwerken (z. B. Twitter, Facebook oder Google+). Herzlichen Dank!

Nutzen Sie unsere Suchfunktion:

Nach oben Sitemap
Impressum Datenschutz

Hinweis: Diese Webseite kann Werbeanzeigen und Werbeeinblendungen oder eingebundene Links von Diensten und Inhalten Dritter enthalten. Beachten Sie dazu unsere Datenschutzerklärung.