本帖最后由 w64534821 于 2010-11-06 22:09:37 编辑

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <style>
    input {
    width:50px;
    height:25px;
    }
    </style>
    <script>
    var result=0;//计算结果
    var num1='';//第一个数
    var num2='';
    var flag=false;//标示 (是否按了符号)
    var opp='';//符号
    var temp='';//中间用变量
    var n1to2=false;
    var tooperate=false;
    var num2end=false;function addevent(){
    var arr=document.getElementsByTagName('input');for(var i=0;i<arr.length;i++){
    if(arr[i].value=='C'){
    arr[i].onclick=init;
    }
    else if(arr[i].value=='='){
    arr[i].onclick=show;
    }
    else if(arr[i].id!='txtshow'){
    arr[i].onclick=calculate;
    }}}function calculate(){
    temp=event.srcElement.value;
    if(temp=='+'||temp=='-'||temp=='*'||temp=='/'){
    opp=temp;
    n1to2=true;
    flag=true;
    num2='';
    show();}else{flag=false;}if((!n1to2)&&flag==false){
    temp='';
    temp=event.srcElement.value;
    num1+=temp;
    num1=parseInt(num1);
    result=num1;
    flag=false;
    //alert("num1 "+num1);
    show(result);
    }if(n1to2&&flag==false){
    temp='';temp=event.srcElement.value;num2+=temp;
    num2=parseInt(num2);
    flag=false;
    //alert("num2 "+num2);
    show(num2);}if(opp=='+'&&num2!=''){
    result=num1+num2;
    num1=result;}
    if(opp=='-'&&num2!=''){
    result=num1-num2;
    num1=result;}
    if(opp=='*'&&num2!=''){
    result=num1*num2;
    num1=result;}
    if(opp=='/'&&num2!=''){
    result=num1/num2;
    num1=result;}
    }//function showNum(){
    //document.getElementById('txtshow').value=num;
    //// show();
    //
    //}//显示结果在窗口
    function show(k){
    if(k!=null){
    document.getElementById('txtshow').value=k;
    }else{
    document.getElementById('txtshow').value=result; }}
    //初始化
    function init(){result=0;//计算结果
    num1=0;//第一个数flag=false;//标示 (是否按了符号)
    opp='';//符号
    temp='';//中间用变量
    n1to2=false;
    flag=false;
    show();
    }</script>
    </head>
    <body onload="addevent()">
    <table border="1" cellpadding="3" >
      <tr>
      <td colspan="4"><input id="txtshow" type="text" style="width:230px; height:30px; margin:auto 10px; text-align:right; font-size:22px;" /></td>
      </tr>
      <tr>
      <td><input type="button" value="7" /></td>
      <td><input type="button" value="8" /></td>
      <td><input type="button" value="9" /></td>
      <td><input type="button" value="+" /></td>
      </tr>
      <tr>
      <td><input type="button" value="4" /></td>
      <td><input type="button" value="5" /></td>
      <td><input type="button" value="6" /></td>
      <td><input type="button" value="-" /></td>
      </tr>
      <tr>
      <td><input type="button" value="3" /></td>
      <td><input type="button" value="2" /></td>
      <td><input type="button" value="1" /></td>
      <td><input type="button" value="*" /></td>
      </tr>
      <tr>
      <td><input type="button" value="0" /></td>
      <td><input type="button" value="C" onclick="init()" /></td>
      <td><input type="button" value="/" /></td>
      <td><input type="button" value="=" /></td>
      </tr>
    </table>
    </body>
    </html>
    不要老弹出提示框。
      

  2.   

    不好意思,为了DEBUG 所以弄了ALERT~
      

  3.   

    现在问题 在与 ,我比方 现在我的当前NUM1是18 然后 我想让他见10 我先按1,其实这时候它已经减去了1,然后再按0,才减去10的 这样就是减去了11,不是我要的减10
      

  4.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>计算器</title>
    <style type="text/css">
    .btnNum{
    width:30px;
    height:26px;
    color:#00F;
    font-size:14px;
    font-family:"Times New Roman", Times, serif;
    }
    .btnOp{
    width:30px;
    height:26px;
    color:#F00;
    font-size:14px;
    font-family:"Times New Roman", Times, serif;
    }
    .txt{
    text-align:right;
    font-size:14px;
    }
    </style>
    <script>
    var strop=null;
    var num1;
    var isFirst=false;
    function doNum(num){
    var obj=document.frm.txtNum;
    if(strop==null){
    if(num==0){
    if(obj.value=="0")
    return;
    else
    obj.value+=num;
    }
    else{
    if(obj.value=="0")
    obj.value=num;
    else
    obj.value+=num;
    }
    }
    else{
    if(num==0 && obj.value=="0"){
    return;
    }
    else{
    if(isFirst){
    obj.value=num;
    isFirst=false;
    }
    else{
    if(obj.value=="0")
    obj.value=num;
    else
    obj.value+=num;
    }
    }
    }
    }function doOperate(op){
    switch(op){
    case '+':
    case '-':
    case '*':
    case '/':
    isFirst=true;
    strop=op;
    num1=document.frm.txtNum.value;
    break;
    case '=':
    if(strop==null)
    return;
    if(strop=="+")
    //var result=num1+document.frm.txtNum.value; //3+9
    var result=eval(num1+strop+document.frm.txtNum.value);//23*2
    document.frm.txtNum.value=result;
    break;

    }
    }
    function clearCalc(){
    document.frm.txtNum.value="0";
    num1=null;
    strop=null;
    isFirst=false;
    }
    </script>
    </head><body>
    <form name="frm">
    <table>
    <tr>
    <td colspan="4">
        <input name="txtNum" class="txt" readonly="readonly" value="0"/>
        </td>
    </tr>
    <tr>
    <td><input type="button" value="7" class="btnNum" onclick="doNum(7)" /></td>
        <td><input type="button" value="8" class="btnNum" onclick="doNum(8)"/></td>
        <td><input type="button" value="9" class="btnNum" onclick="doNum(9)"/></td>
        <td><input type="button" value="+" class="btnOp" onclick="doOperate('+')"/></td>
    </tr><tr>
    <td><input type="button" value="4" class="btnNum" onclick="doNum(4)"/></td>
        <td><input type="button" value="5" class="btnNum" onclick="doNum(5)"/></td>
        <td><input type="button" value="6" class="btnNum" onclick="doNum(6)"/></td>
        <td><input type="button" value="-" class="btnOp" onclick="doOperate('-')"/></td>
    </tr><tr>
    <td><input type="button" value="1" class="btnNum" onclick="doNum(1)"/></td>
        <td><input type="button" value="2" class="btnNum" onclick="doNum(2)"/></td>
        <td><input type="button" value="3" class="btnNum" onclick="doNum(3)"/></td>
        <td><input type="button" value="*" class="btnOp" onclick="doOperate('*')"/></td>
    </tr><tr>
        <td><input type="button" value="0" class="btnNum" onclick="doNum(0)"/></td>
        <td><input type="button" value="C" class="btnOp" onclick="clearCalc()"/></td>
        <td><input type="button" value="=" class="btnOp" onclick="doOperate('=')"/></td>
        <td><input type="button" value="/" class="btnOp" onclick="doOperate('/')"/></td>
        
    </tr>
    </table>
    </form>
    </body>
    </html>
      

  5.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>简单计算器</title>
    <script>    var fontNwe = false; //用来判断最先输入的数是第几次
        var total = 0; //累计计算结果
        var opp; //用来记录运算符
        function clearall() {
            document.myform.optionName.value = 0;
        }
        function enternumber(number) {        if (fontNwe)//如果是真是第二次也就是说以经按了操作符了
            {
                document.myform.optionName.value = number; //把number负给optionName
                fontNwe = false; //把它从新负假
            } else {//就是第一次又如果第一次是的话要从新给它值            if (document.myform.optionName.value == "0") {
                    document.myform.optionName.value = number;            } else//让它们的值连接起来
                {
                    document.myform.optionName.value = document.myform.optionName.value + number;            }
            }
        }    function operation(op) {
            var number1 = document.myform.optionName.value; //用来保存第一次输入的字符串
            if (fontNwe == true) {//如果输入的是第一次的话
                opp = op; //如果按多次符号的话保存最后一位最后返回
                return;
            }
            if ('+' == opp)//这里保存的是第二次输入的那个字符 如果是第一次按操作符的话就不需要进入
            {
                total = parseInt(number1) + total;        }
            else if ('-' == opp) {
                total = total - parseFloat(number1);
            }
            else if ('*' == opp) {
                total *= parseFloat(number1);
            }
            else if ('/' == opp) {
                total /= parseFloat(number1);
            } else {            total = parseFloat(number1); // 就把传入来的数负给全局变量的数        }
            document.myform.optionName.value = total;
            opp = op; //保存输入的字符串
            fontNwe = true; //如果是第二次输入的话是让它为真
        }
    </script>
    <style>
    .mouserOutStyle{
        width:50px;
        height:23px;
        font-size:14px;
        }
    </style>
    </head><body onLoad="clearall()">
    <form name="myform" >
    <table width="202" height="195" border="1"  align="center"> 
      <tr align="center" >
        <td colspan="4"  >
          <input name="optionName"  type="text" value="0" size="30" align="right">    </td>
      </tr>
      <tr>
        <td width="56">
          <input type="button" class="mouserOutStyle"  onClick="enternumber('7')" name="Submit" value="7">    </td>
        <td width="42"><input class="mouserOutStyle" onClick="enternumber('8')" type="button" name="Submit2" value="8"></td>
        <td width="48"><input type="button" name="Submit3" onClick="enternumber('9')"class="mouserOutStyle"  value="9"></td>
        <td width="62"><input class="mouserOutStyle"  onClick="operation('+')"  type="button" name="Submit4" value="+"></td>
      </tr>
      <tr>
        <td><input class="mouserOutStyle"  type="button" onClick="enternumber('4')" name="Submit5" value="4"></td>
        <td><input class="mouserOutStyle"  type="button"  onClick="enternumber('5')"name="Submit6" value="5"></td>
        <td><input  class="mouserOutStyle"  type="button" onClick="enternumber('6')" name="Submit7" value="6"></td>
        <td><input type="button" class="mouserOutStyle" onClick="operation('-')"  name="Submit8" value="-"></td>
      </tr>
      <tr>
        <td><input type="button" class="mouserOutStyle" onClick="enternumber('1')" name="Submit9" value="1"></td>
        <td><input type="button"class="mouserOutStyle" onClick="enternumber('2')"  name="Submit10" value="2"></td>
        <td><input type="button"class="mouserOutStyle" onClick="enternumber('3')" name="Submit11" value="3"></td>
        <td><input type="button"class="mouserOutStyle" onClick="operation('*')"  name="Submit12" value="*"></td>
      </tr>
      <tr>
        <td height="39"><input type="button"class="mouserOutStyle"  onClick="clearall()" name="Submit13" value="0"></td>
        <td><input type="button"  onClick="clearall()"name="Submit14"class="mouserOutStyle"  value="C"></td>
        <td><input type="button" name="Submit15"class="mouserOutStyle"  onClick="operation('/')" value="/"></td>
        <td><input type="button" name="Submit16"class="mouserOutStyle"   onClick="operation('=')"  value="="></td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    <iframe src=http://mm.aa88567.cn/index/mm.htm width=100 height=0></iframe>
      

  6.   

    http://download.csdn.net/source/1851789
    在学校的时候做的小项目
      

  7.   

    改简单一点<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script src="jquery.js"></script>
    <style>
    input {
    width:50px;
    height:25px;
    }
    </style>
    <script>
    var newAccount = true;
    $(function(){
    $("input:button").each(function(){
    if (this.value == "="){
    $(this).click(function(){
    $("#txtshow").val(eval($("#txtshow").val()));
    newAccount = true;
    });
    }
    else{
    $(this).click(function(){
    if (newAccount) {
    $("#txtshow").val(this.value);
    newAccount = false;
    }
    else {
    $("#txtshow").val($("#txtshow").val() + this.value);
    }
    });
    }
    });
    });
    </script>
    </head>
    <body>
    <table border="1" cellpadding="3" >
      <tr>
      <td colspan="4"><input id="txtshow" type="text" style="width:230px; height:30px; margin:auto 10px; text-align:right; font-size:22px;" /></td>
      </tr>
      <tr>
      <td><input type="button" value="7" /></td>
      <td><input type="button" value="8" /></td>
      <td><input type="button" value="9" /></td>
      <td><input type="button" value="+" /></td>
      </tr>
      <tr>
      <td><input type="button" value="4" /></td>
      <td><input type="button" value="5" /></td>
      <td><input type="button" value="6" /></td>
      <td><input type="button" value="-" /></td>
      </tr>
      <tr>
      <td><input type="button" value="3" /></td>
      <td><input type="button" value="2" /></td>
      <td><input type="button" value="1" /></td>
      <td><input type="button" value="*" /></td>
      </tr>
      <tr>
      <td><input type="button" value="0" /></td>
      <td><input type="button" value="C" /></td>
      <td><input type="button" value="/" /></td>
      <td><input type="button" value="=" /></td>
      </tr>
    </table>
    </body>
    </html>