分享
 
 
 

javascript计算器

王朝html/css/js·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

<html>

<head>

<title>计算器</title>

</head>

<body bgcolor="#ffffff" onload="FKeyPad.ReadOut.focus();FKeyPad.ReadOut.select();">

<FORM name="Keypad" action="">

<TABLE align="center">

<B>

<TABLE align="center" border=2 width=50 height=60 cellpadding=1 cellspacing=5>

<TR>

<TD colspan=3 align=middle><input name="ReadOut" type="Text" onkeypress="CheckOut()" size=24 value="0" width=100%></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 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><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 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><input name="btnZero" type="Button" value=" 0 " onclick="NumPressed(0)"></TD>

<TD><input name="btnDecimal" type="Button" value=" . " onclick="Decimal()"></TD>

<TD colspan=2></TD>

<TD><input name="btnEquals" type="Button" value=" = " onclick="Operation('=')"></TD>

<TD><input name="btnReturn" type="Button" value="返 回" onclick="goReturn()"></TD>

</TR>

</TABLE>

</TABLE>

</B>

</FORM>

</CENTER>

<font face="Verdana, Arial, Helvetica" size=2>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

var FKeyPad = document.forms['Keypad'];

var Accum = "0";

var FlagNewNum = false;

var PendingOp = "";

//===============================================================================

//[描述] 浮点数精确计算

//[参数] str1 - 第一个数

// str2 - 第二个数

// type - 运算符

// precision - 小数位精度

//[调用方式] longCount(str1,str2,type,precision);

//[返回值] 计算结果

//===============================================================================

function longCount(str1,str2,type) {

var comma1 = 0;

if (str1.indexOf(".")!=-1) {

str1 = str1.replace(/0*$/,"");

comma1 = str1.length - str1.indexOf(".")-1;

}

var comma2 = 0;

if (str2.indexOf(".")!=-1) {

str2 = str2.replace(/0*$/,"");

comma2 = str2.length - str2.indexOf(".")-1;

}

str1 = str1.replace(/\./,"");

str2 = str2.replace(/\./,"");

var value,comma;

if (type!="*") {

if (comma1>comma2) {

for (var i=0;i<comma1-comma2;i++) str2 += "0";

comma = (type=="/")?0:comma1;

}else {

for (var i=0;i<comma2-comma1;i++) str1 += "0";

comma = (type=="/")?0:comma2;

}

}else {

comma = comma1 + comma2;

}

if (type=="+") {

value = parseInt(str1,10) + parseInt(str2,10);

}else if (type=="-") {

value = parseInt(str1,10) - parseInt(str2,10);

}else if (type=="*") {

value = parseInt(str1,10) * parseInt(str2,10);

}else if (type=="/") {

value = parseInt(str1,10) / parseInt(str2,10);

}

value = String(value);

if (comma>0) value = value.substring(0,value.length-comma)+"."+value.substring(value.length-comma,value.length);

if (value.indexOf(".")!=-1)

value = value.replace(/0*$/,"");

return value;

}

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 || '-' == PendingOp || '/' == PendingOp || '*' == PendingOp)

Accum = longCount(Accum,Readout,PendingOp);

else

Accum = Readout;

FKeyPad.ReadOut.value = Accum;

PendingOp = Op;

FKeyPad.ReadOut.focus();

FKeyPad.ReadOut.select();

}

}

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 () {

alert(FKeyPad.ReadOut.value);

FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,"-1","*");

}

function Percent () {

FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,Accum,"*");

FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,100,"/");

}

function goReturn() {

top.returnValue = FKeyPad.ReadOut.value;

self.close();

}

function CheckOut() {

var keyCode = window.event.keyCode;

if (keyCode>=48 && keyCode<=57) {

if (FlagNewNum) {

FKeyPad.ReadOut.value = "";

//window.event.keyCode = null;

FlagNewNum = false;

}

return true;

}else if (keyCode==43 || keyCode==45 || keyCode==42 || keyCode==47 || keyCode==61) {

Operation(String.fromCharCode(keyCode));

}else if (keyCode==46) {//.

if (FKeyPad.ReadOut.value.indexOf(".") == -1)

return true;

}else if (keyCode==13) goReturn();

window.event.returnValue = false;

return false;

}

// End -->

</SCRIPT>

</body>

</html>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有