如何使用 HTML、CSS 和 JavaScript 建立二進位制計算器?
二進位制計算器是一個對二進位制數進行數學運算的程式。現在,如果您還記得,二進位制數是由兩個數字(即 0 和 1)組成的數字。在本博文中,我們將使用此程式來計算二進位制數的加法、減法、乘法和除法。這將是一個基本的計算器,它將使用 HTML、CSS 和 JavaScript 的基本概念來執行相同的操作。因此,讓我們從 HTML 結構開始。
HTML 結構
首先,我們將建立一個表單,該表單將被劃分為表格行,提供諸如新增 1、新增 0、清除顯示以及加號、減號、乘號和除號按鈕以及等於號等功能。
<form> <table> <tr> <td colspan="4"> <input type="text" id="display" disabled /> </td> </tr> <tr> <td> <input type="button" value="1" onclick="addToDisplay(1)" /> </td> <td> <input type="button" value="0" onclick="addToDisplay(0)" /> </td> <td> <input type="button" value="C" onclick="clearDisplay()" /> </td> <td> <input type="button" value="+" onclick="addToDisplay('+')" /> </td> <td> <input type="button" value="-" onclick="addToDisplay('-')" /> </td> <td> <input type="button" value="*" onclick="addToDisplay('*')" /> </td> <td> <input type="button" value="/" onclick="addToDisplay('/')" /> </td> <td> <input type="button" value="=" onclick="calculate()" /> </td> </tr> <tr> <td colspan="4"> Equivalent Decimal is: <p id="toDecimal"></p> </td> </tr> <tr> <td colspan="4"> <p id="previousCalculation"></p> </td> </tr> <!-- more buttons for the other operations --> </table> </form>
如您所見,我們有一個 ID 為“display”的輸入欄位,該欄位被停用。此欄位將用於顯示輸入和計算結果。我們還有一組用於不同二進位制數字(0 和 1)和不同數學運算(+、-、*、/)的按鈕。每個按鈕都有一個 onclick 屬性,當單擊時會觸發一個 JavaScript 函式。
CSS 樣式
接下來,我們將新增一些 CSS 樣式,使我們的計算器看起來更美觀。
<style> /* Center the calculator on the page */ table { margin: 0 auto; padding: 20px; } /* Style the display */ #display { background-color: #f2f2f2; /* gray */ text-align: right; padding: 12px 20px; font-size: 20px; border: none; width: 100%; } /* Add some spacing between the buttons */ input[type="button"] { margin: 5px; } /* Give the buttons a consistent size and appearance */ input[type="button"] { width: 50px; height: 50px; font-size: 18px; background-color: #f2f2f2; border: none; cursor: pointer; } #toDecimal { font-size: 30px; } /* Add hover effect to the buttons */ input[type="button"]:hover { background-color: #e6e6e6; } /* Add a different style for the operator buttons */ input[type="button"][value="+"], input[type="button"][value="-"], input[type="button"][value="*"], input[type="button"][value="/"] { background-color: #4caf50; color: white; } /* Add a different style for the clear button */ input[type="button"][value="C"] { background-color: #f44336; color: white; } /* Add a different style for the equal button */ input[type="button"][value="="] { background-color: #2196f3; color: white; } </style>
JavaScript 功能
最後,我們將向計算器新增 JavaScript 功能。
<script> function addToDisplay(val) { var display = document.getElementById("display"); display.value += val; } function clearDisplay() { var display = document.getElementById("display"); display.value = ""; document.getElementById("toDecimal").innerHTML = ""; } function calculate() { var display = document.getElementById("display"); var result = eval(display.value); display.value = result; var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; } function calculate() { var display = document.getElementById("display"); var input = display.value; var result; //splitting the input by operator var numbers = input.split(/[+\-*/]/); var operator = input.replace(numbers[0], "").replace(numbers[1], ""); //converting strings to binary var num1 = parseInt(numbers[0], 2); var num2 = parseInt(numbers[1], 2); //checking the operator and performing the corresponding operation switch (operator) { case "+": result = (num1 + num2).toString(2); var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; break; case "-": result = (num1 - num2).toString(2); var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; break; case "*": result = (num1 * num2).toString(2); var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; break; case "/": result = (num1 / num2).toString(2); var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; break; default: result = "Invalid operator"; var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; } display.value = result; } </script>
將以上所有程式碼組合到 index.html 檔案中
<!DOCTYPE html> <html> <head> <title>Calculator</title> <style> /* Center the calculator on the page */ table { margin: 0 auto; padding: 20px; } /* Style the display */ #display { background-color: #f2f2f2; /* gray */ text-align: right; padding: 12px 20px; font-size: 20px; border: none; width: 100%; } /* Add some spacing between the buttons */ input[type="button"] { margin: 5px; } /* Give the buttons a consistent size and appearance */ input[type="button"] { width: 50px; height: 50px; font-size: 18px; background-color: #f2f2f2; border: none; cursor: pointer; } #toDecimal { font-size: 30px; } /* Add hover effect to the buttons */ input[type="button"]:hover { background-color: #e6e6e6; } /* Add a different style for the operator buttons */ input[type="button"][value="+"], input[type="button"][value="-"], input[type="button"][value="*"], input[type="button"][value="/"] { background-color: #4caf50; color: white; } /* Add a different style for the clear button */ input[type="button"][value="C"] { background-color: #f44336; color: white; } /* Add a different style for the equal button */ input[type="button"][value="="] { background-color: #2196f3; color: white; } </style> </head> <body> <form> <table> <tr> <td colspan="4"> <input type="text" id="display" disabled /> </td> </tr> <tr> <td> <input type="button" value="1" onclick="addToDisplay(1)" /> </td> <td> <input type="button" value="0" onclick="addToDisplay(0)" /> </td> <td> <input type="button" value="C" onclick="clearDisplay()" /> </td> <td> <input type="button" value="+" onclick="addToDisplay('+')" /> </td> <td> <input type="button" value="-" onclick="addToDisplay('-')" /> </td> <td> <input type="button" value="*" onclick="addToDisplay('*')" /> </td> <td> <input type="button" value="/" onclick="addToDisplay('/')" /> </td> <td> <input type="button" value="=" onclick="calculate()" /> </td> </tr> <tr> <td colspan="4"> Equivalent Decimal is: <p id="toDecimal"></p> </td> </tr> <tr> <td colspan="4"> <p id="previousCalculation"></p> </td> </tr> <!-- more buttons for the other operations --> </table> </form> <script> function addToDisplay(val) { var display = document.getElementById("display"); display.value += val; } function clearDisplay() { var display = document.getElementById("display"); display.value = ""; document.getElementById("toDecimal").innerHTML = ""; } function calculate() { var display = document.getElementById("display"); var result = eval(display.value); display.value = result; var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; } function calculate() { var display = document.getElementById("display"); var input = display.value; var result; //splitting the input by operator var numbers = input.split(/[+\-*/]/); var operator = input.replace(numbers[0], "").replace(numbers[1], ""); //converting strings to binary var num1 = parseInt(numbers[0], 2); var num2 = parseInt(numbers[1], 2); //checking the operator and performing the corresponding operation switch (operator) { case "+": result = (num1 + num2).toString(2); var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; break; case "-": result = (num1 - num2).toString(2); var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; break; case "*": result = (num1 * num2).toString(2); var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; break; case "/": result = (num1 / num2).toString(2); var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; break; default: result = "Invalid operator"; var decimalNumber = parseInt(result, 2); document.getElementById("toDecimal").innerHTML = decimalNumber; } display.value = result; localStorage.setItem("previousCalculation", input + " = " + result); var previousCalculation = localStorage.getItem("previousCalculation"); document.getElementById("previousCalculation").innerHTML = previousCalculation; } </script> </body> </html>
在本教程中,我們學習瞭如何使用 HTML、CSS 和 JavaScript 建立二進位制計算器。我們瞭解瞭如何設定 HTML 結構、新增 CSS 樣式和 JavaScript 功能以建立可工作的計算器。您可以根據需要新增更多功能,例如處理錯誤情況和新增更多操作。此專案可以幫助您瞭解不同的語言如何協同工作以建立動態且互動式的 Web 應用程式。
廣告