用vscode制作一个可以加减乘除的计算器,要求使用button标签
时间: 2024-05-25 14:15:30 浏览: 75
VB制作加减乘除小计算器
<!DOCTYPE html>
<html>
<head>
<title>计算器</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.calculator {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0,0,0,0.3);
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
width: 300px;
}
.calculator-display {
grid-column: 1 / -1;
background-color: #eee;
height: 50px;
display: flex;
justify-content: flex-end;
align-items: center;
padding: 0 10px;
font-size: 24px;
font-weight: bold;
}
button {
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<div class="calculator">
<div class="calculator-display" id="display"></div>
<button id="clear">C</button>
<button id="sign">+/-</button>
<button id="percent">%</button>
<button id="divide">/</button>
<button id="seven">7</button>
<button id="eight">8</button>
<button id="nine">9</button>
<button id="multiply">*</button>
<button id="four">4</button>
<button id="five">5</button>
<button id="six">6</button>
<button id="subtract">-</button>
<button id="one">1</button>
<button id="two">2</button>
<button id="three">3</button>
<button id="add">+</button>
<button id="zero">0</button>
<button id="decimal">.</button>
<button id="equals">=</button>
</div>
<script>
const display = document.getElementById('display');
const buttons = document.querySelectorAll('button');
let firstOperand = null;
let operator = null;
let secondOperand = null;
function clear() {
display.textContent = '0';
firstOperand = null;
operator = null;
secondOperand = null;
}
function sign() {
display.textContent = Number(display.textContent) * -1;
}
function percent() {
display.textContent = Number(display.textContent) / 100;
}
function handleNumberClick(event) {
const number = event.target.textContent;
if (display.textContent === '0' || operator !== null) {
display.textContent = number;
} else {
display.textContent += number;
}
}
function handleOperatorClick(event) {
const selectedOperator = event.target.textContent;
if (firstOperand === null) {
firstOperand = Number(display.textContent);
operator = selectedOperator;
display.textContent = '0';
} else if (operator !== null && secondOperand === null) {
operator = selectedOperator;
} else if (operator !== null && secondOperand !== null) {
const result = calculate();
firstOperand = result;
operator = selectedOperator;
secondOperand = null;
display.textContent = result;
}
}
function handleDecimalClick() {
if (!display.textContent.includes('.')) {
display.textContent += '.';
}
}
function calculate() {
const operand1 = firstOperand;
const operand2 = Number(display.textContent);
let result;
switch (operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
default:
result = operand2;
}
return result;
}
function handleEqualsClick() {
if (firstOperand !== null && operator !== null) {
secondOperand = Number(display.textContent);
const result = calculate();
firstOperand = result;
operator = null;
secondOperand = null;
display.textContent = result;
}
}
document.getElementById('clear').addEventListener('click', clear);
document.getElementById('sign').addEventListener('click', sign);
document.getElementById('percent').addEventListener('click', percent);
document.getElementById('zero').addEventListener('click', handleNumberClick);
document.getElementById('one').addEventListener('click', handleNumberClick);
document.getElementById('two').addEventListener('click', handleNumberClick);
document.getElementById('three').addEventListener('click', handleNumberClick);
document.getElementById('four').addEventListener('click', handleNumberClick);
document.getElementById('five').addEventListener('click', handleNumberClick);
document.getElementById('six').addEventListener('click', handleNumberClick);
document.getElementById('seven').addEventListener('click', handleNumberClick);
document.getElementById('eight').addEventListener('click', handleNumberClick);
document.getElementById('nine').addEventListener('click', handleNumberClick);
document.getElementById('add').addEventListener('click', handleOperatorClick);
document.getElementById('subtract').addEventListener('click', handleOperatorClick);
document.getElementById('multiply').addEventListener('click', handleOperatorClick);
document.getElementById('divide').addEventListener('click', handleOperatorClick);
document.getElementById('decimal').addEventListener('click', handleDecimalClick);
document.getElementById('equals').addEventListener('click', handleEqualsClick);
</script>
</body>
</html>
阅读全文