計算機2

檔案下載 : 1

將下列程式碼複製至<head>程式碼</head>區內

<div align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<script>
var NDIGITS = 16;
var STACKSIZE = 12;
var value = 0;
var memory =
Gookie("CalcMemory");
if (!memory) memory = 0;
var level = 0;
var entered = true;
var decimal = 0;
var fixed = 0;
var exponent = false;
var inverse = false;
function stackItem(){
this.value = 0;
this.op = "";
}
function array(length){
this[0] = 0;
for (i = 0; i < length; i++) {
this[i] = 0;
this[i] = new stackItem();
}
this.length = length;
}
stack = new array(STACKSIZE);
function push(value, op, prec){
if (level == STACKSIZE)
return false;
stack[level].value = value;
stack[level].op = op;
stack[level].prec = prec;
level++;
return true;
}
function pop(){
if (level == 0)
return false;
level--;
return true;
}
function format(value){
var valStr = "" + value;
if (valStr.indexOf("N") >= 0 ||
(value == 2*value && value == 1+value))
return "Error ";
var i = valStr.indexOf("e")
if (i >= 0) {
var expStr = valStr.substring(i+1, valStr.length);
if (i > 11) i = 11;
valStr = valStr.substring(0, i);
if (valStr.indexOf(".") < 0) valStr += ".";
valStr += " " + expStr;
}
else {
var valNeg = false;
if (value < 0) {
value = -value;
valNeg = true;
}
var valInt = Math.floor(value);
var valFrac = value - valInt;
var prec = NDIGITS - (""+valInt).length - 1;
if (! entered && fixed>0)
prec = fixed;
var mult = " 1000000000000000000".substring(1, prec+2);
var frac = Math.floor(valFrac * mult + 0.5);
valInt = Math.floor(Math.floor(value * mult + .5) / mult);
if (valNeg)
valStr = "-" + valInt;
else
valStr = "" + valInt;
var fracStr = "00000000000000" + frac;
fracStr = fracStr.substring(fracStr.length-prec, fracStr.length);
i = fracStr.length - 1;
if (entered || fixed==0) {
while (i >= 0 && fracStr.charAt(i) == "0")
--i;
fracStr = fracStr.substring(0, i+1);
}
if (i >= 0) valStr += "." + fracStr;
}
return valStr;
}
function refresh(){
var display = format(value);
if (exponent) {
if (expval < 0)
display += " " + expval;
else
display += " +" + expval;
}
if (display.indexOf(".") < 0 && display != "Error ") {
if (entered || decimal > 0)
display += '.';
else
display += ' ';
}
display = " " + display;
display = display.substring(display.length-NDIGITS-1, display.length);
document.result.result.value = display;
inverse = false;
}
function clearAll(){
level = 0;
clear();
}
function clear(){
exponent = false;
value = 0;
enter();
refresh();
}
function evalx(){
if (level==0)
return false;
op = stack[level-1].op;
sval = stack[level-1].value;
if (op == "+")
value = sval + value;
else if (op == '-')
value = sval - value;
else if (op == '*')
value = sval * value;
else if (op == '/')
value = sval / value;
else if (op == 'pow')
value = Math.pow(sval, value);
pop();
if (op == '(')
return false;
return true;
}
function openp(){
enter();
if (!push(0, '(', 0)) {
value = "NAN";
}
refresh();
}
function closep(){
enter();
while (evalx());
refresh();
}
function operator(op){
enter();
if (op == '+' || op == '-')
prec = 1;
else if (op == '*' || op == '/')
prec = 2;
else if (op == "pow")
prec = 3;
if (level > 0 && prec <= stack[level-1].prec)
evalx();
if (!push(value, op, prec))
value = "NAN";
refresh();
}
function enter(){
if (exponent)
value = value * Math.exp(expval * Math.LN10);
entered = true;
exponent = false;
decimal = 0;
fixed = 0;
}
function equals(){
enter();
while (level > 0)
evalx();
refresh();
}
function digit(n){
if (entered) {
value = 0;
digits = 0;
entered = false;
}
if (n == 0 && digits == 0) {
refresh();
return;
}
if (exponent) {
if (expval < 0)
n = -n;
if (digits < 3) {
expval = expval * 10 + n;
++digits;
refresh();
}
return;
}
if (value < 0)
n = -n;
if (digits < NDIGITS-1) {
++digits;
if (decimal > 0) {
decimal = decimal * 10;
value = value + (n/decimal);
++fixed;
}
else
value = value * 10 + n;
}
refresh();
}
function sign(){
if (exponent)
expval = -expval;
else
value = -value;
refresh();
}
function period(){
if (entered) {
value = 0;
digits = 1;
}
entered = false;
if (decimal == 0) {
decimal = 1;
}
refresh();
}
function exp(){
if (entered || exponent)
return;
exponent = true;
expval = 0;
digits = 0;
decimal = 0;
refresh();
}
function inv(){
inverse = !inverse;
}
function func(f){
enter();
if (f == "1/x")
value = 1/value;
else if (f == "swap") {
if (level == 0)
value = "NAN";
else {
var tmp = value;
value = stack[level-1].value;
stack[level-1].value = tmp;
}
}
else if (f == 'n!') {
if (value < 0 || value > 200 ||
value != Math.round(value))
value = "NAN";
else {
var n = 1;
var i;
for (i = 1; i <= value; i++)
n *= i;
value = n;
}
}
else if (f == "MR")
value = memory;
else if (f == "M+") {
memory += value;
Sookie("CalcMemory", memory);
}
else if (f == "Min") {
memory = value;
Sookie("CalcMemory", memory);
}
else if (inverse) {
if (f == "sin")
value = Math.asin(value)*180/Math.PI;
else if (f == "cos")
value = Math.acos(value)*180/Math.PI;
else if (f == "tan")
value = Math.atan(value)*180/Math.PI;
else if (f == "log")
value = Math.exp(value * Math.LN10);
else if (f == "log2")
value = Math.exp(value * Math.LN2);
else if (f == "ln")
value = Math.exp(value);
else if (f == "sqrt")
value = value*value;
else if (f == "pi")
value = Math.E;
}
else {
if (f == "sin")
value = Math.sin(value/180 * Math.PI);
else if (f == "cos")
value = Math.cos(value/180 * Math.PI);
else if (f == "tan")
value = Math.tan(value/180 * Math.PI);
else if (f == "log")
value = Math.log(value)/Math.LN10;
else if (f == "log2")
value = Math.log(value)/Math.LN2;
else if (f == "ln")
value = Math.log(value);
else if (f == "sqrt")
value = Math.sqrt(value);
else if (f == "pi")
value = Math.PI;
}
refresh();
}
function none(){
alert("Not implemented here!");
}
function GookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(
document.cookie.substring(offset, endstr));
}
function Gookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function Sookie(name, value) {
var argv = Sookie.arguments;
var argc = Sookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires="
+ expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function Dookie(name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = Gookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
document.write('<FORM name="result">')
document.write('<INPUT size=20 name="result" onFocus="refresh(); this.blur();">')
document.write('</FORM>')
refresh()
</script>
<p><map name="FPMap0">
<area href="javascript:digit(0)" shape="rect" coords="12, 191, 32, 209">
<area href="javascript:period()" shape="rect" coords="40, 191, 60, 209">
<area href="javascript:exp()" shape="rect" coords="68, 191, 88, 209">
<area href="javascript:sign()" shape="rect" coords="96, 191, 116, 209">
<area href="javascript:equals()" shape="rect" coords="124, 191, 144, 209">
<area href="javascript:digit(1)" shape="rect" coords="12, 166, 32, 184">
<area href="javascript:digit(2)" shape="rect" coords="40, 166, 60, 184">
<area href="javascript:digit(3)" shape="rect" coords="68, 166, 88, 184">
<area href="javascript:operator('+')" shape="rect" coords="96, 166, 116, 184">
<area href="javascript:operator('-')" shape="rect" coords="124, 166, 144, 184">
<area href="javascript:digit(4)" shape="rect" coords="12, 141, 32, 159">
<area href="javascript:digit(5)" shape="rect" coords="40, 141, 60, 159">
<area href="javascript:digit(6)" shape="rect" coords="68, 141, 88, 159">
<area href="javascript:operator('*')" shape="rect" coords="96, 141, 116, 159">
<area href="javascript:operator('/')" shape="rect" coords="124, 141, 144, 159">
<area href="javascript:digit(7)" shape="rect" coords="12, 116, 32, 134">
<area href="javascript:digit(8)" shape="rect" coords="40, 116, 60, 134">
<area href="javascript:digit(9)" shape="rect" coords="68, 116, 88, 134">
<area href="javascript:clear()" shape="rect" coords="96, 116, 116, 134">
<area href="javascript:clearAll()" shape="rect" coords="124, 116, 144, 134">
<area href="javascript:openp()" shape="rect" coords="12, 90, 32, 104">
<area href="javascript:closep()" shape="rect" coords="40, 90, 60, 104">
<area href="javascript:func('Min')" shape="rect" coords="68, 90, 88, 104">
<area href="javascript:func('MR')" shape="rect" coords="96, 90, 116, 104">
<area href="javascript:func('M+')" shape="rect" coords="124, 90, 144, 104">
<area href="javascript:operator('pow')" shape="rect" coords="12, 65, 32, 79">
<area href="javascript:func('n!')" shape="rect" coords="40, 65, 60, 79">
<area href="javascript:func('sqrt')" shape="rect" coords="68, 65, 88, 79">
<area href="javascript:func('1/x')" shape="rect" coords="96, 65, 116, 79">
<area href="javascript:func('swap')" shape="rect" coords="124, 65, 144, 79">
<area href="javascript:inv()" shape="rect" coords="12, 40, 32, 54">
<area href="javascript:func('sin')" shape="rect" coords="40, 40, 60, 54">
<area href="javascript:func('cos')" shape="rect" coords="68, 40, 88, 54">
<area href="javascript:func('tan')" shape="rect" coords="96, 40, 116, 54">
<area href="javascript:func('pi')" shape="rect" coords="124, 40, 144, 54">
<area href="javascript:func('log')" shape="rect" coords="12, 15, 32, 29">
<area href="javascript:func('ln')" shape="rect" coords="40, 15, 60, 29">
<area href="javascript:func('log2')" shape="rect" coords="68, 15, 88, 29">
<area href="javascript:none()" shape="rect" coords="96, 15, 116, 29">
<area href="javascript:none()" shape="rect" coords="124, 15, 144, 29"></map>
<img rectangle=" (68,191) (88, 209) javascript:exp()" rectangle=" (96,191) (116, 209) javascript:sign()" rectangle=" (124,191) (144, 209) javascript:equals()" rectangle=" (12,166) (32, 184) javascript:digit(1)" rectangle=" (40,166) (60, 184) javascript:digit(2)" rectangle=" (68,166) (88, 184) javascript:digit(3)" rectangle=" (96,166) (116, 184) javascript:operator('+')" rectangle=" (124,166) (144, 184) javascript:operator('-')" rectangle=" (12,141) (32, 159) javascript:digit(4)" rectangle=" (40,141) (60, 159) javascript:digit(5)" rectangle=" (68,141) (88, 159) javascript:digit(6)" rectangle=" (96,141) (116, 159) javascript:operator('*')" rectangle=" (124,141) (144, 159) javascript:operator('/')" rectangle=" (12,116) (32, 134) javascript:digit(7)" rectangle=" (40,116) (60, 134) javascript:digit(8)" rectangle=" (68,116) (88, 134) javascript:digit(9)" rectangle=" (96,116) (116, 134) javascript:clear()" rectangle=" (124,116) (144, 134) javascript:clearAll()" rectangle=" (12,90) (32, 104) javascript:openp()" rectangle=" (40,90) (60, 104) javascript:closep()" rectangle=" (68,90) (88, 104) javascript:func('Min')" rectangle=" (96,90) (116, 104) javascript:func('MR')" rectangle=" (124,90) (144, 104) javascript:func('M+')" rectangle=" (12,65) (32, 79) javascript:operator('pow')" rectangle=" (40,65) (60, 79) javascript:func('n!')" rectangle=" (68,65) (88, 79) javascript:func('sqrt')" rectangle=" (96,65) (116, 79) javascript:func('1/x')" rectangle=" (124,65) (144, 79) javascript:func('swap')" rectangle=" (12,40) (32, 54) javascript:inv()" rectangle=" (40,40) (60, 54) javascript:func('sin')" rectangle=" (68,40) (88, 54) javascript:func('cos')" rectangle=" (96,40) (116, 54) javascript:func('tan')" rectangle=" (124,40) (144, 54) javascript:func('pi')" rectangle=" (12,15) (32, 29) javascript:func('log')" rectangle=" (40,15) (60, 29) javascript:func('ln')" rectangle=" (68,15) (88, 29) javascript:func('log2')" rectangle=" (96,15) (116, 29) javascript:none()" rectangle=" (124,15) (144, 29) javascript:none()" src="caculator.gif" border="0" usemap="#FPMap0" width="159" height="224"></td>
</tr>
</table>
</center>
</div>