Problème Calculatrice en JAVASCRIPt [Fermé]

Messages postés
98
Date d'inscription
dimanche 22 janvier 2006
Statut
Membre
Dernière intervention
18 novembre 2015
- - Dernière réponse : internetasso
Messages postés
1064
Date d'inscription
jeudi 25 octobre 2007
Statut
Membre
Dernière intervention
30 juin 2013
- 30 juil. 2008 à 11:14
Bonjour,
j'ai réalisé une calculette en javascipt.Jusque là tout fonctionne bien mais le hic ç'est que lorsque j'obtiens le resultat d'une operation et que je veux en effectuer une autre le chiffre que je tape se met à la suite du précédent resultat.Je sèche un peu la dessus.Merci de bien vouloir me filer un coup de main.

ci-dessous mon code:

<HTML>
<TITLE>Calculatrice en javascript</TITLE>
<META http-equiv=Content-Language content=fr>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 5.50.4522.1800" name=GENERATOR>
<META AUTHOR="http://www.javascriptfr.com/article.aspx?ID=134">
<BODY bgcolor="#e7e7e7">
<CENTER>
<FORM name=Keypad action="">
<TABLE height=115 cellSpacing=5 cellPadding=1 width=220 border=2 bgcolor="#003366">
<TBODY>
<TR bgcolor="#E7F1FE">
<TD align=middle colSpan=3 height="23">
<div align="center">
<INPUT size=12 value=0 name=ReadOut width="94">

</div>
</TD>
<TD width="0" height="23">
</TD>
<TD width="47" height="23">
<div align="center">
<INPUT onclick=Clear() type=button value=" C " name=btnClear>
</div>
</TD>
<TD width="47" height="23">
<div align="center">

<INPUT onclick=ClearEntry() type=button value="CE" name=btnClearEntry>
</div>
</TD>
</TR>
<TR bgcolor="#E7F1FE">
<TD width="47">
<div align="center">
<INPUT onclick=NumPressed(7) type=button value=" 7 " name=btnSeven>
</div>
</TD>

<TD width="47">
<div align="center">
<input onClick=NumPressed(8) type=button value=" 8 " name=btnEight>
</div>
</TD>
<TD width="47">
<div align="center">
<INPUT onclick=NumPressed(9) type=button value=" 9 " name=btnNine>
</div>
</TD>
<TD width="0">
<div align="center"></div>

</TD>
<TD width="47">
<div align="center">
<INPUT onclick=Neg() type=button value="+/-" name=btnNeg>
</div>
</TD>
<TD width="47">
<div align="center">
<INPUT onclick=Percent() type=button value="%" name=btnPercent>
</div>
</TD>

</TR>
<TR bgcolor="#E7F1FE">
<TD width="47" height="23">
<div align="center">
<INPUT onclick=NumPressed(4) type=button value=" 4 " name=btnFour>
</div>
</TD>
<TD width="47" height="23">
<div align="center">
<INPUT onclick=NumPressed(5) type=button value=" 5 " name=btnFive>
</div>

</TD>
<TD width="47" height="23">
<div align="center">
<INPUT onclick=NumPressed(6) type=button value=" 6 " name=btnSix>
</div>
</TD>
<TD width="0" height="23">
<div align="center"></div>
</TD>
<TD align=middle width="47" height="23">
<div align="center">
<INPUT onclick="Operation('+')" type=button value=" + " name=btnPlus>

</div>
</TD>
<TD align=middle width="47" height="23">
<div align="center">
<INPUT onclick="Operation('-')" type=button value=" - " name=btnMinus>
</div>
</TD>
</TR>
<TR bgcolor="#E7F1FE">
<TD width="47">
<div align="center">
<INPUT onclick=NumPressed(1) type=button value=" 1 " name=btnOne>

</div>
</TD>
<TD width="47">
<div align="center">
<INPUT onclick=NumPressed(2) type=button value=" 2 " name=btnTwo>
</div>
</TD>
<TD width="47">
<div align="center">
<INPUT onclick=NumPressed(3) type=button value=" 3 " name=btnThree>
</div>

</TD>
<TD width="0">
<div align="center"></div>
</TD>
<TD align=middle width="47">
<div align="center">
<INPUT onclick="Operation('*')" type=button value=" * " name=btnMultiply>
</div>
</TD>
<TD align=middle width="47">
<div align="center">
<INPUT onclick="Operation('/')" type=button value=" / " name=btnDivide>

</div>
</TD>
</TR>
<TR bgcolor="#E7F1FE">
<TD width="47" height="23">
<div align="center">
<INPUT onclick=NumPressed(0) type=button value=" 0 " name=btnZero>
</div>
</TD>
<TD width="47" height="23">
<div align="center">
<INPUT onclick=Decimal() type=button value=" , " name=btnDecimal>

</div>
</TD>
<TD colSpan=3 height="23">
<div align="center"></div>
</TD>
<TD width="47" height="23">
<div align="center">
<INPUT onclick="Operation('=')" type=button value=" = " name=btnEquals>
</div>
</TD>

</TR>
</TBODY>
</TABLE>
</FORM></CENTER><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 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>
</FONT></BODY></HTML>
Afficher la suite 

1 réponse

Meilleure réponse
Messages postés
1064
Date d'inscription
jeudi 25 octobre 2007
Statut
Membre
Dernière intervention
30 juin 2013
126
1
Merci
tiens aller une petite calculette vite fait bien fait...

<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<meta name="generator" content="Calculette">
<title>Votre calculette</title>
</head>

<body>
<center>
<p>
<form name="Keypad" action="(Référence vide!)">
<table>
<tr>
<td>
<table border="2" width="50" height="60" cellpadding="1" cellspacing="5">
<tr>
<td colspan="3"><input name="ReadOut" size="24" value="0" width="100%" type="text"></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><input name="btnPlus" type="button" value=" + " onclick="Operation('+')"></td>
<td><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><input name="btnMultiply" type="button" value=" * " onclick="Operation('*')"></td>
<td><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>
</td>
</td></tr>
</table>
</form>
<font face="Verdana,Arial,Helvetica" size="2">
<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>
</font></p>
<p></center>
</body>

</html>

C'est basique mais ca fonctionne, tu peux changer tout bien entendu mais le fonctionnement sera le même...

La création d'un site commence par la réflexion de son devenir...

Dire « Merci » 1

Quelques mots de remerciements seront grandement appréciés. Ajouter un commentaire

CCM 60239 internautes nous ont dit merci ce mois-ci