如何使用 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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

本文讨论了在浏览器中优化JavaScript性能的策略,重点是减少执行时间并最大程度地减少对页面负载速度的影响。

本文讨论了使用浏览器开发人员工具的有效JavaScript调试,专注于设置断点,使用控制台和分析性能。

Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

本文说明了如何使用源地图通过将其映射回原始代码来调试JAVASCRIPT。它讨论了启用源地图,设置断点以及使用Chrome DevTools和WebPack之类的工具。

如何在JavaScript中将具有相同ID的数组元素合并到一个对象中?在处理数据时,我们常常会遇到需要将具有相同ID�...

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。
