溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Javascript實(shí)現(xiàn)基本運(yùn)算器的方法

發(fā)布時(shí)間:2021-04-25 09:38:09 來(lái)源:億速云 閱讀:116 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹了Javascript實(shí)現(xiàn)基本運(yùn)算器的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

javascript是一種什么語(yǔ)言

javascript是一種動(dòng)態(tài)類型、弱類型的語(yǔ)言,基于對(duì)象和事件驅(qū)動(dòng)并具有相對(duì)安全性并廣泛用于客戶端網(wǎng)頁(yè)開(kāi)發(fā)的腳本語(yǔ)言,同時(shí)也是一種廣泛用于客戶端Web開(kāi)發(fā)的腳本語(yǔ)言。它主要用來(lái)給HTML網(wǎng)頁(yè)添加動(dòng)態(tài)功能,現(xiàn)在JavaScript也可被用于網(wǎng)絡(luò)服務(wù)器,如Node.js。

用Javascript實(shí)現(xiàn)一個(gè)基本的運(yùn)算器,具體內(nèi)容如下

使用表格布局,JS添加事件

<!DOCTYPE html>
<html>
<head lang="en">
 <meta charset="UTF-8">
 <title>計(jì)算器</title>
</head>
<style>
 *{
  margin: 0;
  padding: 0;
 }
 table{
  width:400px;
  height: 600px;
  border-collapse: collapse;
  margin: auto;
 }
 .trr{
  width: 400px;
  height: 100px;
 }
 .trr1{
  width: 400px;
  height: 50px;
 }
 .tdd{ width: 100px;
  height: 100px;
  border: 1px solid silver;
  text-align: center;
  line-height: 100px;

 }
 .btn{
  width: 100%;
  height: 100%;
  font-size: 2.5em;
 }
 .btn1{
  width: 100%;
  height: 100%;
  font-size: 2.5em;
 }
 .tdd1{
  width: 100px;
  height: 50px;
 }
 .text{
  height: 100%;
  font-size: 2.5em;
  text-align: right;
 }

</style>
<body>
<table>

 <tr class="trr text1">
  <td class="tdd" colspan="4"><input class="text" type="text" disabled value="0" /></td>
 </tr>

 <tr class="trr1">
  <td class="tdd1" colspan="2"><input class="btn1 btn" type="button" value="c"/></td>
  <td class="tdd1" colspan="2"><input class="btn1 btn" type="button" value="d"/></td>
 </tr>

 <tr class="trr">
  <td class="tdd"><input class="btn" type="button" value="7"/></td>
  <td class="tdd"><input class="btn" type="button" value="8"/></td>
  <td class="tdd"><input class="btn" type="button" value="9"/></td>
  <td class="tdd"><input class="btn" type="button" value="/"/></td>
 </tr>

 <tr class="trr">
  <td class="tdd"><input class="btn" type="button" value="4"/></td>
  <td class="tdd"><input class="btn" type="button" value="5"/></td>
  <td class="tdd"><input class="btn" type="button" value="6"/></td>
  <td class="tdd"><input class="btn" type="button" value="*"/></td>
 </tr>

 <tr class="trr">
  <td class="tdd"><input class="btn" type="button" value="1"/></td>
  <td class="tdd"><input class="btn" type="button" value="2"/></td>
  <td class="tdd"><input class="btn" type="button" value="3"/></td>
  <td class="tdd"><input class="btn" type="button" value="-"/></td>
 </tr>

 <tr class="trr">
  <td class="tdd"><input class="btn" type="button" value="0"/></td>
  <td class="tdd"><input class="btn" type="button" value="."/></td>
  <td class="tdd"><input class="btn" type="button" value="+"/></td>
  <td class="tdd"><input class="btn" type="button" value="="/></td>
 </tr>
</table>
<script>
 var obtn=document.getElementsByClassName("btn");
 var otext=document.getElementsByClassName("text")[0];
 var arr=[];//定義一個(gè)數(shù)組,向其中存入數(shù)字和運(yùn)算符。

 for(var i=0;i<obtn.length;i++){
  obtn[i].onclick= function () { 
   if(!isNaN(this.value)||this.value=="."){ //this:代表鼠標(biāo)點(diǎn)擊的obtn
     if(otext.value.indexOf(".")==-1){ //消除重復(fù)"."的BUG  
      if(otext.value.length==0){ 
      if(this.value!="0"){       //----------------------
       otext.value+=this.value;     //|
      }            //|
      }            //|
      else if(otext.value.length==1&&otext.value=="0"){//|
      otext.value=this.value;      //|
      }            //|
      else if(otext.value.length==1&&otext.value!="0"){//初始狀態(tài)時(shí),若計(jì)算器屏幕為"0",
      otext.value+=this.value;      //實(shí)現(xiàn)輸入一個(gè)非零數(shù)字的時(shí)候,計(jì)算器
      }            //上的數(shù)值替換為輸入的非零值
      else if(otext.value.length>1){     //|
       otext.value+=this.value;      //|
      }            //--------------------
     }
     else
     {
      if(this.value!="."){ //消除重復(fù)"."的BUG
       if(otext.value.length==0){
        if(obtn[i].value!="0"){
        otext.value+=this.value;
       }
       }
       if(otext.value.length>=1){
        otext.value+=this.value;
       }            
      }
     }
   }
   if(this.value=="/"||this.value=="*"||this.value=="+"||this.value=="-"){


    if(otext.value!="0"&&otext.value!=""&&otext.value!="-"){ 
              //消除輸入重復(fù)運(yùn)算符的BUG,
     arr[arr.length]=otext.value;  //當(dāng)輸入一個(gè)運(yùn)算符的時(shí)候,otext內(nèi)的value值
     arr[arr.length]=this.value;   //為""(空),所以判斷條件為若otext內(nèi)的value值不為空
     otext.value="";      //則向數(shù)字中傳值。
    }          //此時(shí)出現(xiàn)無(wú)法輸入負(fù)數(shù)值運(yùn)算的BUG


    else if(otext.value==""&&this.value=="-"){ //消除無(wú)法輸入負(fù)數(shù)值運(yùn)算的BUG
     otext.value=this.value;    //當(dāng)點(diǎn)擊運(yùn)算符后otext的value值為空,
    }           //此時(shí)判斷若this的值為"-",就替換進(jìn)去。
    else if(otext.value=="0"&&this.value=="-"){//此時(shí)出現(xiàn)無(wú)法執(zhí)行類似"3--3"的雙減法運(yùn)算BUG,
     otext.value=this.value;    //因?yàn)閑val()無(wú)法識(shí)別有雙減的字符串值。
    }           //若初始時(shí),otext值為"0",并且this的值為
   }            //"-",則用"-"替換otext中的值。



   if(this.value=="="){
    if(otext.value.length>=1){      //--------------------
     var string="";        //|
      if(arr[arr.length-1]=="-"&&otext.value<0){ //→消除無(wú)法執(zhí)行類似"3--3"的雙減法運(yùn)算的BUG
      arr[arr.length-1]="+";     //→當(dāng)輸入負(fù)數(shù)值的時(shí)候,判斷arr數(shù)組中的
      otext.value=Math.abs(otext.value);  //→末尾值是否為"-",若為"-"則把其改為"+",
      }           //→并且讓otext.value值取絕對(duì)值。
     arr[arr.length]=otext.value;     //|
     for(var i=0;i<arr.length;i++){    //|
      string+=arr[i];       //|
     }           //|把存入數(shù)組中的數(shù)字和運(yùn)算符遍歷存儲(chǔ)到一個(gè)字符串中,
     otext.value=eval(string);     //|直接使用eval()方法就可以識(shí)別一個(gè)的字符串,執(zhí)行
     arr=[];          //|該字符串中的運(yùn)算
    }            //|
   }             //---------------------
   if(this.value=="c"){
    otext.value="0";
    arr=[];
   }
   if(this.value=="d"){
    otext.value=otext.value.substr(0,otext.value.length-1);//每一次刪除otext中的末尾值
    if(otext.value==""){         //當(dāng)把otext中的值刪除完后,給
     otext.value="0";         //otext復(fù)值"0".
    }
   }
  }
 }
</script>
</body>
</html>

Javascript實(shí)現(xiàn)基本運(yùn)算器的方法

計(jì)算器的”c”功能為清屏;”d”功能為刪除一個(gè)數(shù);

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Javascript實(shí)現(xiàn)基本運(yùn)算器的方法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI