程序代码:
<script language=\"javascript\">
r=new Array(2); function initialize(){ currOper=\"start\"; r[0]=\"0\"; r[1]=\"0\"; operand=\"\"; ix=0; } function addDigit(n){ if(currOper==\"Int\" || currOper==\"Float\") r[ix]=appendDigit(r[ix],n) else{ r[ix]=\"\"+n; currOper=\"Int\"; } showinForm(r[ix]); } function appendDigit(n1,n2){ if(n1==\"0\") return \"\"+n2; var s=\"\"; s+=n1; s+=n2; return s; } function showinForm(s){ document.calculator.total.value=s; } function addDecimalPoint(){ if(currOper!=\"Float\"){ decimal=true; r[ix]+=\".\"; if(currOper==\"Operator\" || currOper==\"getoperand\") r[ix]=\"0.\" currOper=\"Float\"; showinForm(r[ix]); } } function clearDisplay(){ initialize(); showinForm(r[0]); } function changeSign(){ if(r[ix].charAt(0)==\"-\") r[ix]=r[ix].substring(1,r[ix].length); else if(parseFloat(r[ix])!=0) r[ix]=\"-\"+r[ix]; showinForm(r[ix]); } function setTo(n){ r[ix]=\"\"+n; currOper=\"Operator\"; decimal=false; showinForm(r[ix]); } function Count(){ if(currOper==\"Int\" || currOper==\"Float\" || currOper==\"Operator\"){ if(ix==1){ r[0]=doCounting(operand,r[0],r[1]); ix=0; } } else if(currOper==\"getoperand2\"){ r[0]=doCounting(operand,r[1],r[0]); ix=0; } currOper=\"Operator\"; decimal==false; showinForm(r[ix]); } function doCounting(op,x,y){ var result=\"\"; switch(op){ case \"+\":result=\"\"+(parseFloat(x)+parseFloat(y));break; case \"-\":result=\"\"+(parseFloat(x)-parseFloat(y));break; case \"*\":result=\"\"+(parseFloat(x)*parseFloat(y));break; case \"/\":if(parseFloat(y)==0){ alert(\"分子为0,错误!\"); result=0; } else result=\"\"+(parseFloat(x)/parseFloat(y)); break; } return result; } function performOp(op){ if(currOper==\"start\"){ ++ix; operand=op; } else if(currOper==\"Int\" || currOper==\"Float\" || currOper==\"Operator\"){ if(ix==0){ ++ix; operand=op; } else{ r[0]=doCounting(operand,r[0],r[1]); showinForm(r[0]); operand=op; } } currOper=\"getoperand2\"; decimal=false; } function doFunction(){ var selectionList=document.calculator.functions; var selIX=selectionList.selectedIndex; var sel=selectionList.options[selIX].value; switch(sel){ case\"abs\":r[ix]=Math.abs(r[ix]);break; case\"acos\":r[ix]=Math.acos(r[ix]);break; case\"asin\":r[ix]=Math.asin(r[ix]);break; case\"atan\":r[ix]=Math.atan(r[ix]);break; case\"ceil\":r[ix]=Math.ceil(r[ix]);break; case\"cos\":r[ix]=Math.cos(r[ix]);break; case\"exp\":r[ix]=Math.exp(r[ix]);break; case\"floor\":r[ix]=Math.floor(r[ix]);break; case\"log\":r[ix]=Math.log(r[ix]);break; case\"sin\":r[ix]=Math.sin(r[ix]);break; case\"sqrt\":r[ix]=Math.sqrt(r[ix]);break; case\"tan\":r[ix]=Math.tan(r[ix]);break; } decimal=false; showinForm(r[ix]); }