- 论坛徽章:
- 0
|
js 计算器
var a='';
var b='';
var opentor ='';
var action ='';
var m = '' ;
var n = '' ;
function set_num(number)//数字
{
if(opentor=='')
{
a = ''+a + number;
document.getElementById('res').value = a;
}
else{
b = ''+b + number;
document.getElementById('res').value = b;
}
//alert(a);
//alert(b);
}
function backspace()//撤销
{
//alert("hhhhh");
if(b!='')
{
b=b.substring(0,b.length-1);
document.getElementById('res').value = b;
}
else{
a=a.substring(0,a.length-1);
document.getElementById('res').value = a;
}
}
function ce()//只记录第一次输入的值
{
a = document.getElementById('res').value = a;
b='';
//opentor='';
}
function set_op(op)//运算符
{
opentor = op;
if(action!='')//连加
{
c =eval(a+opentor+b);
document.getElementById('res').value = c;
}
action = op;
a=c;
b=0;
opentor=='+';
if(opentor=op)
{
b='';
//document.getElementById('res').value = b;
//b=b;
}
}
function sqrtt()//平方根
{
//alert("hhhhh");
a =Math.sqrt(a);
document.getElementById('res').value = a;
}
function compte()//计算结果
{
/* c =eval(a+opentor+b);
document.getElementById('res').value = c;
a =eval(a+opentor+b);*/
document.getElementById('res').value = a = eval(a+opentor+b);
if(b/a==0)
{
document.getElementById('res').value = "除数不能为零";
}
if(opentor!="")
{
b='';
//document.getElementById('res').value = b;
//b=b;
}
}
function mc()
{
m =''
//alert(m);
document.getElementById('res').value ="";
document.getElementById('m').value ="";
}
function mr()
{
//m =m;
document.getElementById('res').value = m;
}
function ms()
{
m =document.getElementById('res').value;
//alert(m);
document.getElementById('m').value ="m";
}
function mds()
{
alert(m);
alert(document.getElementById('res').value);
alert(typeof(m));
alert(typeof(parseFloat(document.getElementById('res').value)));
//m = parseFloat(m) + parseFloat(document.getElementById('res').value);
m = eval(m + '+'+document.getElementById('res').value);
alert(m)
}
function ep()//清0
{
if(opentor=='')
{
alert(typeof(a));
a = -parseFloat(a);
document.getElementById('res').value = a
}
else{
b = -parseFloat(b);
document.getElementById('res').value = b;
}
}
function ept()//清0
{
//alert("hhhhh");
a = eval(1+'/'+document.getElementById('res').value);
document.getElementById('res').value=a;
}
function pp()//清0
{
//alert("hhhhh");
document.getElementById('res').value = '',a='',b='',opentor='';
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104536/showart_2090207.html |
|