You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
doctor/计算器.html

99 lines
3.1 KiB

<!DOCTYPE html>
<html lang="en">
<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;
background-color: #f0f0f0;
}
#calculator {
border: 1px solid #ccc;
border-radius: 10px;
padding: 20px;
background-color: #fff;
}
#display {
width: 100%;
height: 50px;
margin-bottom: 10px;
font-size: 1.5em;
text-align: right;
padding: 10px;
box-sizing: border-box;
}
.button {
width: 23%;
padding: 15px;
margin: 1%;
font-size: 1.2em;
cursor: pointer;
border: none;
border-radius: 5px;
background-color: #ddd;
}
.button:hover {
background-color: #ccc;
}
</style>
</head>
<body>
<div id="calculator">
<input type="text" id="display" disabled>
<div>
<button class="button" onclick="clearDisplay()">C</button>
<button class="button" onclick="appendToDisplay('%')">%</button>
<button class="button" onclick="appendToDisplay('/')">/</button>
<button class="button" onclick="appendToDisplay('*')">*</button>
</div>
<div>
<button class="button" onclick="appendToDisplay('7')">7</button>
<button class="button" onclick="appendToDisplay('8')">8</button>
<button class="button" onclick="appendToDisplay('9')">9</button>
<button class="button" onclick="appendToDisplay('-')">-</button>
</div>
<div>
<button class="button" onclick="appendToDisplay('4')">4</button>
<button class="button" onclick="appendToDisplay('5')">5</button>
<button class="button" onclick="appendToDisplay('6')">6</button>
<button class="button" onclick="appendToDisplay('+')">+</button>
</div>
<div>
<button class="button" onclick="appendToDisplay('1')">1</button>
<button class="button" onclick="appendToDisplay('2')">2</button>
<button class="button" onclick="appendToDisplay('3')">3</button>
<button class="button" onclick="calculateResult()">=</button>
</div>
<div>
<button class="button" onclick="appendToDisplay('0')">0</button>
<button class="button" onclick="appendToDisplay('.')">.</button>
</div>
</div>
<script>
function appendToDisplay(value) {
document.getElementById('display').value += value;
}
function clearDisplay() {
document.getElementById('display').value = '';
}
function calculateResult() {
try {
let result = eval(document.getElementById('display').value);
document.getElementById('display').value = result;
} catch (e) {
document.getElementById('display').value = '错误';
}
}
</script>
</body>
</html>