DIV CSS 佈局教程網

用 jQuery 打造支援連續運算的簡易計算機
編輯:JQuery特效代碼     

這是一個以 jQuery 實作的簡易計算機,示範的重點不是功能多寡,而是「連續運算」的狀態管理:按下 3 + 5 × 2 時,要在按下下一個運算子之前先把前一步的結果算出來。

頁面結構

<!DOCTYPE html>
<html>
<head>
<meta charset="utf8"/>
<title>Javascript 計算機</title>
</head>
<body>
<table>
 <tr><td colspan="4"><input id="show" value="0"/></td></tr>
 <tr>
  <td><button onclick="number(this)" value="7">7</button></td>
  <td><button onclick="number(this)" value="8">8</button></td>
  <td><button onclick="number(this)" value="9">9</button></td>
  <td><button onclick="calsym(this)" value="+">+</button></td>
 </tr>
 <tr>
  <td><button onclick="number(this)" value="4">4</button></td>
  <td><button onclick="number(this)" value="5">5</button></td>
  <td><button onclick="number(this)" value="6">6</button></td>
  <td><button onclick="calsym(this)" value="-">-</button></td>
 </tr>
 <tr>
  <td><button onclick="number(this)" value="1">1</button></td>
  <td><button onclick="number(this)" value="2">2</button></td>
  <td><button onclick="number(this)" value="3">3</button></td>
  <td><button onclick="calsym(this)" value="*">*</button></td>
 </tr>
 <tr>
  <td><button onclick="number(this)" value="0">0</button></td>
  <td><button onclick="calsym(this)" value="=">=</button></td>
  <td><button onclick="clearCal()" value="c">c</button></td>
  <td><button onclick="calsym(this)" value="/">/</button></td>
 </tr>
</table>
</body>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var num0 = null;      // 第一個運算元
var num1 = null;      // 第二個運算元
var symble = null;    // 目前運算子
var lastclick = null; // 上一次點擊的值
var symarr = new Array();
symarr[0] = '+'; symarr[1] = '/'; symarr[2] = '*'; symarr[3] = '-'; symarr[4] = '=';

function number(n) {
    var numnow = $('#show');
    var _exist = $.inArray(lastclick, symarr);   // 判斷上次點擊是否為運算子
    if (numnow.val() == 0 || _exist != -1) {
        numnow.val($(n).val());                  // 顯示框為 0 或上次是運算子,重新記錄
    } else {
        numnow.val(parseInt(numnow.val()) * 10 + parseInt($(n).val()));
    }
    lastclick = $(n).val();
}

function calsym(cs) {
    var numnow = $('#show');
    var _exist = $.inArray(lastclick, symarr);
    if (num0 == null && symble == null) {
        num0 = numnow.val();                     // 初始狀態
        symble = $(cs).val();
    } else if (num0 != null && num1 == null && _exist != -1) {
        symble = $(cs).val();                    // 連續按運算子,只更新運算子
    } else {
        num1 = numnow.val();
        var result = calculate(symble, num0, num1);
        symble = $(cs).val();                    // 先結算舊運算子,再換成新的
        num0 = result;
        numnow.val(result);
        num1 = null;
    }
    lastclick = $(cs).val();
}

function clearCal() {
    $('#show').val('0');
    num0 = null; num1 = null; symble = null; lastclick = null;
}

function calculate(sym, m, n) {
    var res = null;
    m = parseInt(m); n = parseInt(n);
    switch (sym) {
        case '+': res = m + n; break;
        case '-': res = m - n; break;
        case '*': res = m * n; break;
        case '/': if (n == 0) { alert("false"); break; } res = m / n; break;
        default: break;
    }
    return res;
}
</script>
</html>

幾個關鍵設計

用 lastclick 記錄上一次輸入。判斷使用者現在是在「輸入數字」還是「剛按完運算子」,靠的就是 $.inArray(lastclick, symarr) 回傳 -1 與否。

按下運算子就結算。calsym() 裡的 else 分支是連續運算的核心:先把上一組 num0 與 num1 丟給 calculate() 算出結果,把結果變成新的 num0,再把運算子換成這次按下的符號。

除以零的防護。calculate() 裡針對 n == 0 直接攔下並提示,避免回傳 Infinity。

這個例子刻意沒有做運算優先序(先乘除後加減),它是「照按下順序逐次計算」的簡單計算機。若要支援優先序,需要改成先把整串表達式收集起來,再交給解析器處理,那又是另一個題目了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved