首頁 >web前端 >js教程 >如何使用 HTML、CSS 和 JavaScript 建立二進位計算器?

如何使用 HTML、CSS 和 JavaScript 建立二進位計算器?

王林
王林轉載
2023-09-05 23:25:051468瀏覽

如何使用 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 應用程式。

以上是如何使用 HTML、CSS 和 JavaScript 建立二進位計算器?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除