用JS实现了一个加法器。。




    
        TODO supply a title
        
        
        
        
    
    
        
TODO write content
JS代码为:
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function prepareComputer(){
    var arr=new Array("del","clear","(",")","sqrt",
    "7","8","9","/","%",
    "4","5","6","*","1/x",
    "1","2","3","-","=",
    "0","0",".","+","=");
    if(!document.createElement) return false;
    var body=document.body;
    var showcanvas=document.createElement("div");
    showcanvas.setAttribute("id","showcanvas");
    var rownum=Math.sqrt(arr.length);//一个表格有多少行
    
    //表达式展示
    var express=document.createElement("div");
    express.setAttribute("id","express");
    
    //表达式结果
    var result=document.createElement("div");
    result.setAttribute("id","result");
    var result_text=document.createTextNode("0");
    result.appendChild(result_text);
    
    //键盘
    var caltable=document.createElement("table");
    caltable.setAttribute("id","caltable");
    var len=0;
    var row=null;
    for(var i=0,len=arr.length;i0)
            {
                express.className="show";
                express.innerHTML=express_str;
            }else{
                express.className="hidden";                
            }
    }
    function pushonly(text){
        if(tempstr!="")
        {
            ex_stack.push(tempstr);
            tempstr="";
        }
        if(text!="")
        ex_stack.push(text);
    }
    //添加(,)
   function pushtext_squote(text){
       if(text.indexOf("(")!=-1)
       {
           pushonly(text);
       }
       else{
           //正常来讲,左括号的次数要大于已经存在的右括号次数,不然不允许添加
           var count_left_sqote=0;//左括号的次数为0
           var count_right_sqote=0;//右括号的次数为0
           for(var i=0;i0){
                pop_item=in_stack.shift();
                if(pop_item=="sqrt"){
                    temp_stack.push((Math.sqrt(parseFloat(temp_stack.pop()))).toString());
                }
                else{
                    temp_stack.push(pop_item);
                }
            }
            return cal_ltwo(temp_stack);
        }else{
        return cal_ltwo(in_stack);
        }
    }
    //计算值,只含有*/%+-
    function cal_ltwo(in_stack){
        if(/[%*/]/ .test(in_stack.join('')))
        {
            var pop_item=null;
            var the_first_actor=null;
            var the_second_actor=null;
            var act_result=null;
            var temp_stack=new Array();
            while(in_stack.length>0){
                pop_item=in_stack.shift();
                switch(pop_item){
                    case "*":
                        the_first_actor=parseFloat(temp_stack.pop());
                        if(in_stack.length==0) return false;
                        the_second_actor=parseFloat(in_stack.shift());
                        act_result=(the_first_actor*the_second_actor).toString();
                        temp_stack.push(act_result);
                        break;
                    case "/":
                        the_first_actor=parseFloat(temp_stack.pop());
                        if(in_stack.length==0) return false;
                        the_second_actor=parseFloat(in_stack.shift());
                        act_result=(the_first_actor/the_second_actor).toString();
                        temp_stack.push(act_result);
                        break;
                    case "%":
                        the_first_actor=parseFloat(temp_stack.pop());
                        if(in_stack.length==0) return false;
                        the_second_actor=parseFloat(in_stack.shift());
                        act_result=(the_first_actor*the_second_actor).toString();
                        temp_stack.push(act_result);
                        break;
                    default:
                        temp_stack.push(pop_item);
                        break;
                }
            }
            return cal_lone(temp_stack);
            
        }
        else{
            return cal_lone(in_stack);
        }
    }
    //计算值,只含有+-
    function cal_lone(in_stack){
        var  cal_result=parseFloat(in_stack.shift());
        var operator=null;
        var the_second_act=null;
        while(in_stack.length>0){
            operator=in_stack.shift();
            switch (operator){
                case "+":
                    if(in_stack.length==0){
                        alert("Error");
                        return false;
                    }
                    the_second_act=parseFloat(in_stack.shift());
                    cal_result+=the_second_act;
                    break;
                case "-":
                    if(in_stack.length==0){
                        alert("Error");
                        return false;
                    }
                    the_second_act=parseFloat(in_stack.shift());
                    cal_result-=the_second_act;
                    break;
                default:alert("Error");
                    return false;
            }
        }
        return cal_result;
    }
    function calculate(){
        if(tempstr!="")
            ex_stack.push(tempstr);
        var temp_stack=new Array();
        var temp_item=null;
        var inner_stack=null;
        var matched=false;
        //处理结果
        express.innerHTML="";
    if(ex_stack.indexOf(")")!=-1)
    {
        while(ex_stack.length>0){
            temp_item=ex_stack.shift();
            if(temp_item==")"){
                //找出与之匹配的(
                inner_stack=new Array();
                matched=false;
                while(temp_stack.length>0){
                    var checkitem=temp_stack.pop();
                    if(checkitem=="(")
                    {
                        matched=true;
                        break;
                    }
                    else{
                        inner_stack.unshift(checkitem);
                    }
                    
                }
                //是否匹配到(
                if(matched==false){
                    alert("表达式不正确");
                    return false;
                }
                else{
                    //()内的内容为空
                    if(inner_stack.length==0){
                        alert(表达式不正确);
                        return false;
                    }else{
                        temp_stack.push(calculate_sqrt(inner_stack));
                    }
                }
            }
            else{
                temp_stack.push(temp_item);
            }
            
        }
        return calculate_sqrt(temp_stack);
    }
    else{
        return calculate_sqrt(ex_stack);
    }
        
    }
    switch(text)
    {
        case ".":
            if(isNullOrEmpty(tempstr)){
                alert("操作有误");
                break;
            }
            tempstr+=".";
            break;
        case "=":
            var cal_result=calculate();
            ex_stack=new Array();
            result.innerHTML=cal_result;
            break;
        case "+":  
        case "-":  
        case "*":
        case "/":
        case "%":
            case "sqrt":
            pushtext(text);
        break;
        case "del":
            if(tempstr.length==0){
                if(ex_stack.length==0)
                    break;
                tempstr=ex_stack.pop();
            }
            if(seciallist.indexOf(tempstr)==-1){
                tempstr=tempstr.substring(0,tempstr.length-1);
            }
            else{
                tempstr="";
            }
            tempstr=tempstr.substring(0,-1);
            break;
        case "clear":
            ex_stack=new Array();
            tempstr="";
            express.className="hidden";
            result.innerHTML="0";
            break;
        
        case "(":
        case ")":
            pushtext_squote(text);
        tempstr="";
        break;
        case "1":
        case "2":
        case "3":
        case "4":
        case "5":
        case "6":
        case "7":
        case "8":
        case "9":
        case "0":
            tempstr+=text;
            break;
            
    }
    outstr();
}
function isNullOrEmpty(str){
    return str==null||str=="";
}
//生成数组的字符串表示
function showstring(arr){
    return arr.join('');
}
//监听按键的onclick事件
function bindEvent(){
    if(!document.getElementById) return false;
    var table=document.getElementById("caltable");
    if(!table) return false;
    
    registerEvent(document.body,'click',function(){
        var e=event||window.event;
        var source=e.target||e.srcElement;
        if(source.nodeName.toLowerCase()=="td"){
            var text=source.lastChild.nodeValue;
            pushstack(text);
        }
    });
   
   
}
function registerEvent(tar,type,func){
    if(tar.addEventListener){
        tar.addEventListener(type,func);
    }else if(tar.attachEvent){
        tar.attachEvent("on"+type,func);
    }else{
        tar["on"+type]=func;
    }
}
loadEvent(prepareComputer);
loadEvent(bindEvent);
一个简单的计数器代码,我能写到500行。而且还一堆BUG。。。。我果然是个SB。。还是Jq吧。。

你可能感兴趣的:(JS以及JQ)