Pagine

sabato 20 dicembre 2008

Calcolatrice javascript 2


Nel post precedente avevamo costruito una semplice calcolatrice che ci restituiva dei calcoli in base ai numeri inseriti nella caselle di testo. Questa volta, spero in maniera semplice ed esaustiva, implementiamo il progetto precedente con l'ausilio di un tastierino numerico.


Il codice:


<html><head>


<script language="javascript">

function uno(){document.forms.g.txt1.value=document.forms.g.txt1.value +1;}

function due(){document.forms.g.txt1.value=document.forms.g.txt1.value +2;}

function tre(){document.forms.g.txt1.value=document.forms.g.txt1.value +3;}

function qua(){document.forms.g.txt1.value=document.forms.g.txt1.value +4;}

function cin(){document.forms.g.txt1.value=document.forms.g.txt1.value +5;}

function sei(){document.forms.g.txt1.value=document.forms.g.txt1.value +6;}

function set(){document.forms.g.txt1.value=document.forms.g.txt1.value +7;}

function ott(){document.forms.g.txt1.value=document.forms.g.txt1.value +8;}

function nov(){document.forms.g.txt1.value=document.forms.g.txt1.value +9;}

function zer(){document.forms.g.txt1.value=document.forms.g.txt1.value +0;}

function vir(){document.forms.g.txt1.value=document.forms.g.txt1.value +'.';document.forms.g.virgola.disabled=true;}

//ad ogni tasto ho assegnato un numero ed alla virgola aggiungo la disabilitazione del tasto stesso una volta premuto


//adesso le operazioni che assegnano un segno, riabilitani il tasto della virgola e ripuliscono il campo di testo


function piu(){document.forms.g.txtsegno.value='+';document.forms.g.virgola.disabled=false;document.forms.g.txt2.value=document.forms.g.txt1.value;document.forms.g.txt1.value="";}

function men(){document.forms.g.txtsegno.value='-';document.forms.g.virgola.disabled=false;document.forms.g.txt2.value=document.forms.g.txt1.value;document.forms.g.txt1.value="";}

function per(){document.forms.g.txtsegno.value='X';document.forms.g.virgola.disabled=false;document.forms.g.txt2.value=document.forms.g.txt1.value ;document.forms.g.txt1.value="";}

function div(){document.forms.g.txtsegno.value=':';document.forms.g.virgola.disabled=false;document.forms.g.txt2.value=document.forms.g.txt1.value;document.forms.g.txt1.value="";}

//infine l'uguale che fa partire il calcolo


function ugu(){

Segno=document.forms.g.txtsegno.value;

if (Segno=="+"){document.forms.g.txt3.value=document.forms.g.txt1.value;document.forms.g.ris.value=eval(document.forms.g.txt2.value)+eval(document.forms.g.txt3.value);

}

if (Segno=="-"){document.forms.g.txt3.value=document.forms.g.txt1.value;document.forms.g.ris.value=eval(document.forms.g.txt2.value)- eval(document.forms.g.txt3.value);}

if (Segno=="X"){document.forms.g.txt3.value=document.forms.g.txt1.value;document.forms.g.ris.value=document.forms.g.txt2.value * document.forms.g.txt3.value}

if (Segno==":"){document.forms.g.txt3.value=document.forms.g.txt1.value;document.forms.g.ris.value=document.forms.g.txt2.value / document.forms.g.txt3.value}

document.forms.g.txt1.value="";

document.forms.g.virgola.disabled=false;

}

function radice(){Txt1=document.forms.g.txt1.value;Rad=Math.sqrt(Txt1);document.forms.g.ris.value=Rad}



</script>



<title>Freecalc 1.0.1 </title>

</head>


<!--inserisco l'html del form-->


<body>

<form name="g">

<table width="150" border="0" cellpadding="2" cellspacing="2" bgcolor="#FF9900">

<caption align="top">

Freecalc1.0.1

</caption>

<tr>

<th colspan="4" align="left" scope="col"><input type="text" name="txt1" size="24"/></th>

</tr>

<tr bgcolor="#FFFFFF">

<td ><input name="Input" type="button" value=" 7 " onclick="set()" /></td>

<td ><input name="Input" type="button" value=" 8 " onclick="ott()"/></td>

<td ><input name="Input" type="button" value=" 9 " onclick="nov()"/></td>

<td ><input name="Input2" type="button" value=" + " onclick="piu()"/></td>

</tr>

<tr bgcolor="#FFFFFF">

<td ><input name="Input" type="button" value=" 4 " onclick="qua()"/></td>

<td ><input name="Input" type="button" value=" 5 " onclick="cin()"/></td>

<td ><input name="Input" type="button" value=" 6 " onclick="sei()"/></td>

<td><input name="Input3" type="button" value=" - " onclick="men()"/></td>

</tr>

<tr bgcolor="#FFFFFF">

<td ><input name="Input" type="button" value=" 1 " onclick="uno()" onkeypress=""/></td>

<td ><input name="Input" type="button" value=" 2 " onclick="due()"/></td>

<td ><input name="Input" type="button" value=" 3 " onclick="tre()"/></td>

<td><input name="Input4" type="button" value=" X " onclick="per()"/></td>

</tr>

<tr bgcolor="#FFFFFF">

<td ><input name="Input" type="button" value=" 0 " onclick="zer()"/></td>

<td ><input name="virgola" type="button" value=" , " onclick="vir()"/></td>

<td ><input name="Input" type="button" value=" = " onclick="ugu()"/></td>

<td><input name="Input5" type="button" value=" : " onclick="div()"/></td>

</tr>

<tr bgcolor="#FFFFFF">

<td colspan="4"><input name="txt2" type="text" value=" 0 " size="9" maxlength="9" readonly="true"/>

<input name="txtsegno" type="text" class="Stile1" size="2" maxlength="1" readonly="true" />

<input name="txt3" type="text" value="0" size="9" maxlength="9" readonly="true"/>

=</td>

</tr>

<tr bgcolor="#FFFFFF">

<td colspan="2"><input name="ris" type="text" size="10" maxlength="5" readonly="true"/></td>

<td><input name="Input6" type="reset" value=" C " /></td>

<td><input name="Input7" type="button" value=" Rq" title="Radice quadrata" onclick="radice()"/></td>

</tr>

</table>

</form>


</body><html>


Clicca quì per vedere l'esempio.


oppureC.I.P.(copia, incolla ,prova)

Nessun commento: