新增jsq.html

main
黄伊雯 2 months ago
parent cf1fa9fd13
commit 6715221fd9

@ -0,0 +1,153 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单计算器</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.calculator {
background-color: #444;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
#display {
width: 100%;
height: 50px;
margin-bottom: 10px;
padding: 0px;
font-size: 24px;
text-align: right;
border: none;
background-color: #fff;
border-radius: 5px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
padding: 20px;
font-size: 20px;
border: none;
border-radius: 5px;
background-color: #666;
color: white;
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: #888;
}
button:active {
background-color: #555;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled>
<div class="buttons">
<button onclick="clearDisplay()">C</button>
<button onclick="appendNumber('7')">7</button>
<button onclick="appendNumber('8')">8</button>
<button onclick="appendNumber('9')">9</button>
<button onclick="chooseOperation('/')">/</button>
<button onclick="appendNumber('4')">4</button>
<button onclick="appendNumber('5')">5</button>
<button onclick="appendNumber('6')">6</button>
<button onclick="chooseOperation('*')">*</button>
<button onclick="appendNumber('1')">1</button>
<button onclick="appendNumber('2')">2</button>
<button onclick="appendNumber('3')">3</button>
<button onclick="chooseOperation('-')">-</button>
<button onclick="appendNumber('0')">0</button>
<button onclick="appendNumber('.')">.</button>
<button onclick="compute()">=</button>
<button onclick="chooseOperation('+')">+</button>
<button onclick="chooseOperation('%')">%</button>
</div>
</div>
<script>
let currentOperand = '';
let previousOperand = '';
let operation = undefined;
const display = document.getElementById('display');
function appendNumber(number) {
if (number === '.' && currentOperand.includes('.')) return;
currentOperand += number;
updateDisplay();
}
function chooseOperation(op) {
if (currentOperand === '') return;
if (previousOperand !== '') {
compute();
}
operation = op;
previousOperand = currentOperand;
currentOperand = '';
}
function compute() {
let computation;
const prev = parseFloat(previousOperand);
const current = parseFloat(currentOperand);
if (isNaN(prev) || isNaN(current)) return;
switch (operation) {
case '+':
computation = prev + current;
break;
case '-':
computation = prev - current;
break;
case '*':
computation = prev * current;
break;
case '/':
computation = prev / current;
break;
case '%':
computation = prev % current;
break;
default:
return;
}
currentOperand = computation.toString();
operation = undefined;
previousOperand = '';
updateDisplay();
}
function clearDisplay() {
currentOperand = '';
previousOperand = '';
operation = undefined;
updateDisplay();
}
function updateDisplay() {
display.value = currentOperand;
}
</script>
</body>
</html>
Loading…
Cancel
Save