|
Home
|
|
Indice Javascript
|
Clock
Browser
Finestre
Status Bar
Background
Calcolatrici
Grafica ed Effetti
Convertitori
Utility
Form
Menu e Link
Calcolatrice
~`~ Scripts By Spikeman ~`~
Usa Copia e incolla questa è la sorgente
<SCRIPT language=LiveScript> <!----- Script CopyRight © 1996 - 1997 S.Chris Brown (Spikeman) http://www.geocities.com/BourbonStreet/3843 /******************************************************* ** S.Christopher Brown's (Spikeman)'s ** ** java script Calculator ** ** CopyRight © 1996 - 1997 S.Chris Brown (Spikeman) ** ** http://www.geocities.com/BourbonStreet/3843 ** ** All Rights Reserved ** ** You can freely use this script, if this credit is ** ** given in the source. So that mean don't mess around ** ** with my ( Spikeman ) Script or Script's k ? ** ** Thank you ** *******************************************************/ // This JavaScript code Originally by S.Chris Brown (Spikeman) 1996 - 1997 ( http://www.geocities.com/BourbonStreet/3843 ) <!-- Hide the script from browsers which do not support it. // Global Variables var acc = 0; // accumulator for calculations var oper = " "; // the previous operation var startnum = true; // is this a new number var decpoint = false; // is there a decimal point var errorstate = false; // an error has occured so lock keys until "AC" is pressed // Global "Constants" (...) MAXLEN = 14; // // IsNumeric - returns true if a string is a valid numeric literal // function IsNumeric(num) { var idx = 0; var ch = ""; var foundpoint = false; for (idx = 0; idx < num.length; idx++) { ch = num.substring(idx, idx +1 ); if ((ch < "0") || (ch > "9")) { if ((ch == ".") && (!foundpoint)) { foundpoint = true; continue; } else { if ((ch == "-") && (idx == 0)) { continue; } } return false; } } // for idx return true; } // // ContainsChar - check if a string contains a certain char // function ContainsChar(str, ch) { var idx; for (idx = 0; idx < str.length; idx++) { if (str.substring(idx, idx + 1) == ch) { return true; } } // for idx return false; } // // ContainsDecimalPoint - check if a number string contains a decimal point // function ContainsDecimalPoint(num) { return ContainsChar(num, "."); } // // DisplayChanged - the display was explicitly changed, so check if it is valid // function DisplayChanged(form, display) { if (!errorstate) { if (IsNumeric(display.value)) { display.defaultValue = display.value; startnum = false; decpoint = ContainsDecimalPoint(display.value); } else { alert("You must enter a numeric value here."); display.value = display.defaultValue; } } else { // errorstate alert('An error has occured. Press the "AC" button.'); } } // // NumPressed - a digit button was pressed, add it to the current display // function NumPressed(form, digit) { if (!errorstate) { if ((digit !=0) || (form.display.value != "0")) { if (startnum) { // need to put a new number on the display (e.g. after "=") form.display.value = eval(digit); } else { if (form.display.value.length < MAXLEN) { form.display.value = form.display.value + digit; } // enough room to put new digit } // add the digit startnum = false; form.display.defaultValue = form.display.value; } // not trying to add leading zeros } else { //errorstate alert('An error has occured. Please press the "AC" button'); } } // // ChangeSign - "+/-" button was pressed, change sign of current display // function ChangeSign(form) { if (!errorstate) { if (form.display.value != "0") { if (eval(form.display.value) < 0) { // negative no problem form.display.value = 0 - eval(form.display.value); } else { // positive, make sure it's not too long if (form.display.value.length < MAXLEN) form.display.value = 0 - eval(form.display.value); } // positive form.display.defaultValue = form.display.value; startnum = false; } // not 0, can be negated } else { // errorstate alert('An error has occured. Please press the "AC" button'); } } // // PointPressed - the decimal dot was pressed, add it to number and start putting disits after it // function PointPressed(form) { if (!errorstate) { if ((!decpoint) && (form.display.value.length < MAXLEN - 1)) { if (startnum) { form.display.value = "0"; } // start a new number form.display.value = form.display.value + "."; form.display.defaultValue = form.display.value; decpoint = true; startnum = false; } // decimal point not already present } else { // errorstate alert('An error has occured. Please press the "AC" button'); } } // // ClearAll - "AC" button was pressed, restart the calculator // function ClearAll(display) { display.value = "0"; display.defaultValue = "0"; decpoint = false; acc = 0; oper = " "; startnum = true; errorstate = false; } // // ClearNum - "C" button was pressed, clear only current number on display // function ClearNum(display) { if (!errorstate) { display.value = "0"; display.defaultValue = "0"; decpoint = false; startnum = true; } else { // errorstate alert('An error has occured. Please press the "AC" button'); } } // // RemoveZeros - remove leading zeros after a decimal point from a number string // function RemoveZeros(num) { var idx; if (ContainsChar(num, ".")) { idx = num.length - 1; ch = num.substring(idx, idx + 1); while((idx > 0) && (ch == "0")) { idx--; ch = num.substring(idx, idx + 1); } num = num.substring(0, idx + 1); // get rid of a trailing decimal point, if it remained hanging after the torture ... if (num.substring(num.length - 1, num.length) == ".") { num = num.substring(0, num.length - 1) } } // num contains a decimal point return num; } // // CalcDisplay - calculate the new display using the accumulator and the previous operator // function CalcDisplay(display) { if (oper == "+") { display.value = acc + eval(display.value); } else if (oper == "-") { display.value = acc - eval(display.value); } else if (oper == "*") { display.value = acc * eval(display.value); } else if (oper == "/") { if (display.value == "0") { display.value = "ERROR !" errorstate = true; } else { display.value = acc / eval(display.value); } } if (!errorstate) { if ((eval(display.value) >= eval("1e" + MAXLEN)) || (eval(display.value) <= eval("-1e" + (MAXLEN - 1)))) { display.value = "ERROR !"; errorstate = true; } else { if (!ContainsChar(display.value, "e")) { // not exponential format if (display.value.length > MAXLEN) { // number is in permited range, so extra digits are after decimal point // truncate extra digits display.value = display.value.substring(0, MAXLEN); // get rid of a hanging decimal point if (display.value.substring(MAXLEN - 1, MAXLEN) == ".") { display.value = display.value.substring(0, MAXLEN - 1); } else { // get rid of trailing zeros display.value = RemoveZeros(display.value); } } // string too long } else { // exponential format // Numbers smaller than 0.00001 are represented in exponential notation by the browser. // A piece of code to translate them to fix notation should be put here. // This is getting *much* too boring for me, so I leave it as is. // If anyone has the will power to write it, I'll be glad to include it ... } // exponential format } // not too high or low } // no error yet (division by zero) display.defaultValue = display.value; } // // OperPressed - calculate the new display and move it to the accumulator // function OperPressed(form, newoper) { if (!errorstate) { CalcDisplay(form.display); oper = newoper; acc = eval(form.display.value); startnum = true; decpoint = false; } else { // errorstate alert('An error has occured. Please press the "AC" button'); } } // // CalcResult - the "=" button was pressed, put result on display // function CalcResult(form) { if (!errorstate) { CalcDisplay(form.display); oper = " "; acc = 0; startnum = true; decpoint = false; } else { // errorstate alert('An error has occured. Please press the "AC" button'); } } // End of hiding script from browsers which do not support it. --> </SCRIPT> </head> <body> <form> <table border=2> <tbody> <tr> <td align=middle> <input name=display onChange="DisplayChanged(this.form, this.form.display)" size=15 value=0> </td> </tr> <tr> <td> <table border=1> <tbody> <tr> <td> <input type=radio onClick=history.go(-1) checked> </td> <td></td> <td> <input onClick=ClearNum(this.form.display) type=button value=" C " name="button2"> </td> <td> <input onClick=ClearAll(this.form.display) type=button value=AC name="button2"> </td> </tr> <tr> <td> <input onClick="NumPressed(this.form, 1)" type=button value=" 1 " name="button2"> </td> <td> <input onClick="NumPressed(this.form, 2)" type=button value=" 2 " name="button2"> </td> <td> <input onClick="NumPressed(this.form, 3)" type=button value=" 3 " name="button2"> </td> <td> <input onClick='OperPressed(this.form, "+")' type=button value=" + " name="button2"> </td> </tr> <tr> <td> <input onClick="NumPressed(this.form, 4)" type=button value=" 4 " name="button2"> </td> <td> <input onClick="NumPressed(this.form, 5)" type=button value=" 5 " name="button2"> </td> <td> <input onClick="NumPressed(this.form, 6)" type=button value=" 6 " name="button2"> </td> <td> <input onClick='OperPressed(this.form, "-")' type=button value=" - " name="button2"> </td> </tr> <tr> <td> <input onClick="NumPressed(this.form, 7)" type=button value=" 7 " name="button2"> </td> <td> <input onClick="NumPressed(this.form, 8)" type=button value=" 8 " name="button2"> </td> <td> <input onClick="NumPressed(this.form, 9)" type=button value=" 9 " name="button2"> </td> <td> <input onClick='OperPressed(this.form, "*")' type=button value=" X " name="button2"> </td> </tr> <tr> <td> <input onClick=ChangeSign(this.form) type=button value=+/- name="button2"> </td> <td> <input onClick="NumPressed(this.form, 0)" type=button value=" 0 " name="button2"> </td> <td> <input onClick=PointPressed(this.form) type=button value=" . " name="button2"> </td> <td> <input onClick='OperPressed(this.form, "/")' type=button value=" / " name="button2"> </td> </tr> <tr align=middle> <td colspan=4> <input onClick=CalcResult(this.form) type=button value=" = " name="button2"> <hr> <h4>~`~ Scripts By Spikeman ~`~</h4> <hr> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </form> <p><center> <font face="Verdana" size="-2">Free JavaScripts da<br> <a href="http://www.massimo61.org/">Free Web Upgrade</a></font> </center><p>