|
| 1 | +/** |
| 2 | + * Created by NewRed on 2017/8/26. |
| 3 | + */ |
| 4 | +var num=0,result=0,numshow="0"; |
| 5 | +var operate=0; //判断输入状态的标志 |
| 6 | +var calcul=0; //判断计算状态的标志 |
| 7 | +var quit=0; //防止重复按键的标志 |
| 8 | +function command(num){ |
| 9 | + var str=String(document.calculator.numScreen.value); //获得当前显示数据 |
| 10 | + str=(str!="0") ? ((operate==0) ? str : "") : ""; //如果当前值不是"0",且状态为0,则返回当前值,否则返回空值; |
| 11 | + str=str + String(num); //给当前值追加字符 |
| 12 | + document.calculator.numScreen.value=str; //刷新显示 |
| 13 | + operate=0; //重置输入状态 |
| 14 | + quit=0; //重置防止重复按键的标志 |
| 15 | +} |
| 16 | +function dzero(){ |
| 17 | + var str=String(document.calculator.numScreen.value); |
| 18 | + str=(str!="0") ? ((operate==0) ? str + "00" : "0") : "0"; //如果当前值不是"0",且状态为0,则返回当str+"00",否则返回"0"; |
| 19 | + document.calculator.numScreen.value=str; |
| 20 | + operate=0; |
| 21 | +} |
| 22 | +function dot(){ |
| 23 | + var str=String(document.calculator.numScreen.value); |
| 24 | + str=(str!="0") ? ((operate==0) ? str : "0") : "0"; //如果当前值不是"0",且状态为0,则返回当前值,否则返回"0"; |
| 25 | + for(i=0; i<=str.length;i++){ //判断是否已经有一个点号 |
| 26 | + if(str.substr(i,1)==".") return false; //如果有则不再插入 |
| 27 | + } |
| 28 | + str=str + "."; |
| 29 | + document.calculator.numScreen.value=str; |
| 30 | + operate=0; |
| 31 | +} |
| 32 | +function del(){ //退格 |
| 33 | + var str=String(document.calculator.numScreen.value); |
| 34 | + str=(str!="0") ? str : ""; |
| 35 | + str=str.substr(0,str.length-1); |
| 36 | + str=(str!="") ? str : "0"; |
| 37 | + document.calculator.numScreen.value=str; |
| 38 | +} |
| 39 | +function clearscreen(){ //清除数据 |
| 40 | + num=0; |
| 41 | + result=0; |
| 42 | + numshow="0"; |
| 43 | + document.calculator.numScreen.value="0"; |
| 44 | +} |
| 45 | +function plus(){ //加法 |
| 46 | + calculate(); //调用计算函数 |
| 47 | + operate=1; //更改输入状态 |
| 48 | + calcul=1; //更改计算状态为加 |
| 49 | +} |
| 50 | +function minus(){ //减法 |
| 51 | + calculate(); |
| 52 | + operate=1; |
| 53 | + calcul=2; |
| 54 | +} |
| 55 | +function times(){ //乘法 |
| 56 | + calculate(); |
| 57 | + operate=1; |
| 58 | + calcul=3; |
| 59 | +} |
| 60 | +function divide(){ //除法 |
| 61 | + calculate(); |
| 62 | + operate=1; |
| 63 | + calcul=4; |
| 64 | +} |
| 65 | +function persent(){ //求余 |
| 66 | + calculate(); |
| 67 | + operate=1; |
| 68 | + calcul=5; |
| 69 | +} |
| 70 | +function equal(){ |
| 71 | + calculate(); //等于 |
| 72 | + operate=1; |
| 73 | + num=0; |
| 74 | + result=0; |
| 75 | + numshow="0"; |
| 76 | +} |
| 77 | +// |
| 78 | +function calculate(){ |
| 79 | + numshow=Number(document.calculator.numScreen.value); |
| 80 | + if(num!=0 && quit!=1){ //判断前一个运算数是否为零以及防重复按键的状态 |
| 81 | + switch(calcul){ //判断要输入状态 |
| 82 | + case 1:result=num+numshow;break; //计算"+" |
| 83 | + case 2:result=num-numshow;break; //计算"-" |
| 84 | + case 3:result=num*numshow;break; |
| 85 | + case 4:if(numshow!=0){result=num/numshow;}else{document.getElementById("note").innerHTML="被除数不能为零!"; setTimeout(clearnote,4000)} break; |
| 86 | + case 5:result=num%numshow;break; |
| 87 | + } |
| 88 | + quit=1; //避免重复按键 |
| 89 | + } |
| 90 | + else{ |
| 91 | + result=numshow; |
| 92 | + } |
| 93 | + numshow=String(result); |
| 94 | + document.calculator.numScreen.value=numshow; |
| 95 | + num=result; //存储当前值 |
| 96 | +} |
| 97 | +function clearnote(){ //清空提示 |
| 98 | + document.getElementById("note").innerHTML=""; |
| 99 | +} |
0 commit comments