| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1248 人关注过本帖
标题:速求javascript实现windows标准计算器的代码
只看楼主 加入收藏
sunfeiliao
Rank: 1
等 级:新手上路
帖 子:24
专家分:6
注 册:2009-9-2
结帖率:50%
收藏
 问题点数:0 回复次数:2 
速求javascript实现windows标准计算器的代码
各位高手帮帮忙!
搜索更多相关主题的帖子: javascript 代码 计算器 windows 
2009-11-02 11:05
sunfeiliao
Rank: 1
等 级:新手上路
帖 子:24
专家分:6
注 册:2009-9-2
收藏
得分:0 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style type="text/css">
.button
{
    width:42px;
    font-weight:normal;
}
.button1
{
    width:86px;
    font-weight:normal
}
.button2
{
    width:42px;
    height:55px;
    font-weight:normal;
}
div{
    position:absolute;
}
a{
    text-decoration:none;
}
</style>

<script language="JavaScript">
<!--
var other="";
var rember=0;
var j=0;
var y=0;
var rember2=0;
var bool=false;
function input(){
    var key=event.keyCode;
    if (key>=48&&key<=57)
    {
        show(String.fromCharCode(key));
    }
    if (key==43||key==45||key==42||key==47)
    {
        ysf(String.fromCharCode(key));
    }
    if (key==13)
    {
        doc.focus();//将光标移到文本框
        js();
    }
    if (key==46)
    {
        dh();
    }
}

//----------控制键盘输入------------

function show(n){
    if(doc.value=="0" || bool==true){
         doc.value=n;
        bool=false;
    }
    else
    {
        doc.value+=n;
    }
}
//-------------显示框--------;
function CE(){
    doc.value="0";//清除当前数
}

function C(){
    j=0;
    y=0;
    rember2=0;
    other="";
    bool=false;
    doc.value="0";
    rember=0;     //全部清除
}

function back(){
    if (bool==false)
    {
         if (doc.value.length==1)
        {
            doc.value="0";
        }
        else if (doc.value.length==2)
        {
            if (doc.value.substring(0,1)=="-")
            {
                doc.value="0";
            }
            else
            {
                doc.value=doc.value.substring(0,doc.value.length-1);// 带负号的退格
            }
        }
        else
        {
            doc.value=doc.value.substring(0,doc.value.length-1);// 常规退格
        }
    }
}

function ysf(s){
    y++;        //记录函数内部运算符的输入次数
    if (y>1 && bool==false)
    {
        if(other=="+")
        {
            doc.value=rember+parseFloat(doc.value);
        }
        if(other=="-")
        {
            doc.value=rember-parseFloat(doc.value);
        }
        if(other=="/")
        {
            if (parseFloat(doc.value) != 0)
            {
                doc.value=rember/parseFloat(doc.value);
            }
            else
            {
                doc.value="除数不能为0";
            }
            

        }
        if(other=="*")
        {
            doc.value=rember*parseFloat(doc.value);
        }
    }
    rember=parseFloat(doc.value);
    other=s;
    bool=true;
}
//////////////
function teshu (m){
     if (m=="%")
    {
        doc.value=rember*parseFloat(doc.value)/100;
    }
    if (m=="sqrt")
    {
        if (parseFloat(doc.value)>=0)
        {
            doc.value=Math.sqrt(parseFloat(doc.value));
        }
        else
        {
            doc.value="负数不能开方";
            bool=true;
        }
    }
    if (m=="1/x")
    {
        if (rember==0)
        {
            doc.value="0没有倒数";
            bool=true;
        }
        else
        {
            doc.value=1/parseFloat(doc.value);
        }        
    }
    if (m=="+/-")
    {
        doc.value=parseFloat(doc.value)*(-1);
    }   
}

//////////////
function js(){
    j++;            //纪录等号的输入次数
    if (j<=1)
    {
        rember2=parseFloat(doc.value);
    }
    if (j>1)
    {
        rember=rember2;        //用于连等运算
    }
    if (other=="+")
    {
        doc.value=rember+parseFloat(doc.value);
    }
    if (other=="-")
    {
        if (j<=1)
        {                     
            doc.value=rember-parseFloat(doc.value);
        }

        else
        {
            doc.value=parseFloat(doc.value)-rember;
        }
    }

    if (other=="*")
    {
        doc.value=rember*parseFloat(doc.value);
    }
    if (other=="/")
    {
        if (j<=1)
        {
            if (parseFloat(doc.value)!=0)
            {
                doc.value=rember*Math.pow(10,2)/(parseFloat(doc.value)*Math.pow(10,2));
            }
            else
            {
                doc.value="除数不能为0";
                bool=true;
            }
        }
        else
        {
            doc.value=doc.value*Math.pow(10,2)/(rember*Math.pow(10,2));
        }
    }
     if (other=="x^y")
    {
        doc.value=Math.pow(rember,parseFloat(doc.value));
    }
    y=0;
    bool=true;
   
}
//----------------等号--------------------
var save;
function ms(){
    if (doc.value!=0)
    {
        jilu.value="M";
        save=doc.value;
        bool=true;
    }
    else
    {
        jilu.value="";
    }
}
function mc(){
    jilu.value="";
    save="";
}
function mr(){
    if (jilu.value=="")
    {
        doc.value="0";
    }
    else
    {
        doc.value=save;   
    }

}
function mj(){
    if (doc.value!="0" && jilu.value=="")
    {
        jilu.value="M";
        save=doc.value;
    }
    else
    {
        save=parseFloat(save)+parseFloat(doc.value);
    }
}
//-----------------数字的存取---------------
function dh(){
    j=0;
    if (doc.value.indexOf(".")>-1 || bool==true)
    {
       doc.value="0.";
       bool=false;
    }
    else if(doc.value.indexOf(".") < 0)
    {
        doc.value+=".";
    }
}
//-------------------点号控制----------------
function meun(){
    for (var i=1;i<4 ;i++ )
    {
        eval("div"+i).style.display="none";
    }
   
}
function xiala(n) {
    var obj = document.getElementById("div"+n);
    if (obj.style.display=="none")
    {
        meun();
        obj.style.display="inline";
    }
    else
    {
        obj.style.display="none";
    }
}
//--------------菜单-------------------
function showtime(){
    var arr=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var now=new Date();
    var mons=now.getMonth()+1;
    var today=now.getDate();
    var hours=now.getHours();
    var mins=now.getMinutes();
    var secs=now.getSeconds();
    var days=now.getDay();
    var week=arr[days];
    if (mins<10)
    {
        mins="0"+mins;
    }
    if (secs<10)
    {
        secs="0"+secs;
    }
    time.value=week+" "+hours+":"+mins+":"+secs;
}
setInterval("showtime()",1000);
//------------------时间---------------------
//-->
</SCRIPT>
</HEAD>

<BODY onKeypress="input()">
  <table border="1" align="center" cellspacing="0" cellpadding="0">
    <tr bgcolor="#E4E0CD">
      <td colspan="2">
      <a onClick="xiala(1)" href="#">编辑(E)</a><br>
      <div id="div1" style="display:none"><a href="#">复制</a><br><a href="#">粘贴</a></div></td>
      <td colspan="2">
      <a onClick="xiala(2)" href="#">查看(V)</a><br>
      <div id="div2" style="display:none"><a href="#">标准型</a><br><a href="#">科学型</a></div></td>
      <td colspan="2">
      <a onClick="xiala(3)" href="#">帮助(H)</a><br>
      <div id="div3" style="display:none"><a href="#">帮助主题</a><br><a href="#">关于计算器</a></div></td>
    </tr>
    <tr>
      <td colspan="6"><input type="text" name="doc" readonly="readonly" size="39" style="text-align:right" value="0"></td>
    </tr>
    <tr>
      <td colspan="3"><input type="text" name="time" size="17" readonly=readonly></td>
      <td colspan="2"><input type="button" value="backspace" class="button1" onClick="back()"></td>
      <td><input name="button10" type="button" class="button" value="CE" onClick="CE()"></td>
    </tr>
    <tr>
      <td><input type="text" name="jilu" class="button" readonly="readonly" style="text-align:center"></td>
      <td><input type="button" value="1" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="2" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="3" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="+" class="button" onClick="ysf(this.value)"></td>
      <td><input type="button" value="C" class="button" onClick="C()">      </td>
    </tr>
    <tr>
      <td><input type="button" class="button" value="MC" onClick="mc()"></td>
      <td><input type="button" value="4" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="5" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="6" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="*" class="button" onClick="ysf(this.value)"></td>
      <td><input name="button3" type="button" class="button" onClick="teshu(this.value)" value="+/-"></td>
    </tr>
    <tr>
      <td><input type="button" class="button" value="MR" onClick="mr()"></td>
      <td><input type="button" value="7" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="8" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="9" class="button" onClick="show(this.value)"></td>
      <td><input type="button" value="/" class="button" onClick="ysf(this.value)"></td>
      <td><input type="button" value="sqrt" class="button" onClick="teshu(this.value)"></td>
    </tr>
    <tr>
      <td><input type="button" class="button" value="MS" onClick="ms()"></td>
      <td><input type="button" class="button" onClick="show(this.value)" value="0"></td>
      <td><input type="button" class="button" value="." onClick="dh()"></td>
      <td><input type="button" class="button" value="%" onClick="teshu(this.value)"></td>
      <td><input type="button" class="button" value="-" onClick="ysf(this.value)"></td>
      <td rowspan="2"><input type="button" class="button2" value="close" onClick="window.close()"></td>
    </tr>
    <tr>
      <td><input type="button" class="button" value="M+" onClick="mj()"></td>
      <td><input type="button" class="button" value="x^y" onClick="ysf(this.value)"></td>
      <td><input type="button" class="button" value="1/x" onClick="teshu(this.value)"></td>
      <td colspan="2"><input type="button" class="button1" onClick="js()" value="="></td>
    </tr>
  </table>
</BODY>
</HTML>
2010-06-09 11:34
sunfeiliao
Rank: 1
等 级:新手上路
帖 子:24
专家分:6
注 册:2009-9-2
收藏
得分:0 
我自己结
2010-06-09 11:34
快速回复:速求javascript实现windows标准计算器的代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047148 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved