Home >Web Front-end >JS Tutorial >How to create a binary calculator using HTML, CSS, and JavaScript?

How to create a binary calculator using HTML, CSS, and JavaScript?

王林
王林forward
2023-09-05 23:25:051473browse

如何使用 HTML、CSS 和 JavaScript 创建二进制计算器?

Binary Calculator is a program that performs mathematical calculations on binary numbers. Now, you remember that a binary number is a number that consists of only two digits, namely 0 and 1. In this blog, we will use this program to calculate addition, subtraction, multiplication and division of binary numbers. This will be a basic calculator that will use basic concepts of HTML, CSS, and JavaScript to perform the same operations. So, let’s start understanding HTML structure.

HTML Structure

First, we will make a table, which will be divided into table rows, providing functions such as adding 1, adding 0, clearing the display and buttons of addition, subtraction, multiplication and division signs, etc.

<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>

As you can see, we have an input field with the ID "display" that is disabled. This field will be used to display input and calculation results. We also have a set of buttons for different binary numbers (0 and 1) and different mathematical operations ( , -, *, /). Each button has an onclick attribute that triggers a JavaScript function when clicked.

CSS Style

Next, we'll add some CSS styles to make our calculator look nicer.

<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 Function

Finally, we will add JavaScript functionality to the calculator.

<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>

Merge all the above codes into the index.html file

<!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>

In this tutorial, we learned how to create a binary calculator using HTML, CSS, and JavaScript. We've seen how to set up the HTML structure, add CSS styles, and JavaScript functionality to create a usable calculator. You can add more features like handling error conditions and add more actions as per your requirement. This project helps you understand how different languages ​​work together to create dynamic, interactive web applications.

The above is the detailed content of How to create a binary calculator using HTML, CSS, and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete