!急JS编写的计算器 如何实现 连续加减乘除四则运算 求大神指点 急帮我再看看小数点的问题
``````        var oper = ""
var isClickOper = false//是否点击了运算符
var num = 0;
var isClickPoint = false;//是否点击了小数点
//输入数字
var nowNum = myform.num.value;
if(nowNum == "0"||isClickOper){//直接赋值
myform.num.value = n;
isClickOper = false;
}else if(myform.num.value.length<9){//限制文本框按键输入数字长度最多9个
//myform.num.value = myform.num.value + n;//连接赋值
myform.num.value += n  //也可以这样写连接赋值
}
}
//添加运算符
function setOper(op){
isClickOper = true;//点击符号之后让改变量变为true  则在输入第二个运算数的第一个数字时才可以重新赋值
oper = op;
num = myform.num.value*1;
isClickPoint = false;//点击符号后让小数点状态恢复false  第二个运算数才可以输入小数点
}

//得到结果
function getCount(){
if(oper != ""){
if(oper == "+"){
num = num + myform.num.value*1;
}else if(oper == "-"){
num = num - myform.num.value*1;
}else if(oper == "*"){
num = num * myform.num.value*1
}else if(oper == "/"){
if(myform.num.value == 0){
num ="除数不能为0"}else{
num = num / myform.num.value*1
}
}else if(oper == "%"){
num = num % myform.num.value*1

}
myform.num.value = num;
}
}

//添加小数点
if(!isClickPoint){//判断是否点击过小数点
myform.num.value += ".";
isClickPoint = true;
}
}

//清楚
function cc(){
myform.num.value = "0";
isClickOper = false;
isClickPoint = false;
num = 0;
oper="0";

}
//删除
function del(){
var nowNum = myform.num.value;
if(nowNum.length==1){
myform.num.value = "0";
}else{
myform.num.value = nowNum.substr(0,nowNum.length-1)
}
}
``````

1个回答