<!doctype html> <html> <head> <meta charset="utf-8"> <title>我的计算器</title> <style> *{margin:0px; padding:0px;} #jsq{ width:300px; height:330px; background:#CCC; margin:0 auto; margin-top:10%; } #showv{width:298px;height:50px;display:block; font-size:16px; line-height:50px;} input{width:56px; height:56px;margin-top:10px;} </style> <script type="text/javascript" src="CalcEval.js"></script> </head> <body> <div id="jsq"> <input type='text' id="showv"/> <input type="button" value="7" onClick="jsqthis.value);" /> <input type="button" value="8" onClick="jsqthis.value);"/> <input type="button" value="9" onClick="jsqthis.value);"/> <input type="button" value="back" onClick="tuige);"/> <input type="button" value="C" onClick="qingling);"/> <input type="button" value="4" onClick="jsqthis.value);"/> <input type="button" value="5" onClick="jsqthis.value);"/> <input type="button" value="6" onClick="jsqthis.value);"/> <input type="button" value="*" onClick="jsqthis.value);"/> <input type="button" value="/" onClick="jsqthis.value);"/> <input type="button" value="1" onClick="jsqthis.value);"/> <input type="button" value="2" onClick="jsqthis.value);"/> <input type="button" value="3" onClick="jsqthis.value);"/> <input type="button" value="+" onClick="jsqthis.value);"/> <input type="button" value="-" onClick="jsqthis.value);"/> <input type="button" value="0" onClick="jsqthis.value);"/> <input type="button" value="00" onClick="jsqthis.value);"/> <input type="button" value="." onClick="jsqthis.value);"/> <input type="button" value="%" onClick="jsqthis.value);"/> <input type="button" value="=" onClick="dengyu);"/> </div> <script> var cal = new CalcEval); var showv=document.getElementById'showv'); showv.focus); function jsqv){ a=showv.value.substr-1,1); zz1=/^[0-9]$/; //重复运算符 if!zz1.testa)&&!zz1.testv)){ console.log'请输入正确格式'); return false; } str=showv.value+v; var zz2=/^[0]+/; //多个0 ifzz2.teststr)){ str=str.replacezz2,0); } //运算符之后的多个0 var zz3=/[+-*/%]{1}[0]{2}/; ifzz3.teststr)){ str=str.substr0,str.length-1); } //开始多个点0.0.0.01 var zz4=/^[0-9]+.[0-9]+.{1,}/; ifzz4.teststr)){ str=str.substr0,str.length-1); } //运算符之后多给点 var zz5=/[+-*/%][0-9]+.[0-9]+.{1,}/; ifzz5.teststr)){ str=str.substr0,str.length-1); } //开始为0 var zz6=/^[0]{1}[0-9]{1,}/; ifzz6.teststr)){ str=str.substr1,str.length-1); } //运算符之后为0开始 var zz7=/[+-*/%][0]{1}[0-9]{1,}/; ifzz7.teststr)){ str=str.substr0,str.length-2)+str.substr-1,1); } showv.value=str; } function dengyu){ var zz=/^[+-*/%.]+$/; var a=showv.value.substr-1,1); ifzz.testa)){//最后输入的是运算符 showv.value=showv.value.substr0,showv.value.length-1); } showv.value=cal.evalshowv.value); showv.focus); } function qingling){ showv.value=""; showv.focus); } function tuige){ showv.value=showv.value.substr0,showv.value.length-1); showv.focus); } //键盘输入 步骤同上 document.onkeyup=function keya){ var str = showv.value; var zz = /[^+-*/%.0-9]{1,}/; str = str.replacezz,""); var zz2 = /[+-*/%.][+-*/%.]{1,}/; var arr = str.matchzz2); ifarr!=null){ str = str.replacezz2,arr[0].substr0,1)); } var zz3 = /^[0][0]{1,}/; str = str.replacezz3,0); var zz4 = /[+-*/%][0][0]{1,}/; var arr4 = str.matchzz4); ifarr4!=null){ str = str.replacezz4,arr4[0].substr0,2)); } var zz5 = /[0-9]+.[0-9]+.{1,}/; var arr5 = str.matchzz5); ifarr5!=null){ str = str.replacezz5,arr5[0].substr0,arr5[0].length-1)); } var zz6 = /[0][1-9]{1,}/; var arr6 = str.matchzz6); ifarr6!=null){ str = str.replacezz6,arr6[0].substr1,arr6[0].length)); } showv.value = str; ifa.keyCode==13){ dengyu); } } </script> </body> </html>