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.

107 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简易计算器</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
}
.calculator {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #100909;
border-radius: 5px;
text-align: right;
font-size: 1.5em;
}
button {
width: 60px;
height: 60px;
margin: 5px;
border: none;
border-radius: 5px;
cursor: pointer;
background-color: #eaedf1;
color: rgb(145, 10, 10);
font-size: 1.5em;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" id="display" placeholder="0" disabled>
<div>
<button onclick="appendToDisplay('7')">7</button>
<button onclick="appendToDisplay('8')">8</button>
<button onclick="appendToDisplay('9')">9</button>
<button class="operator" onclick="appendToDisplay('/')">÷</button>
</div>
<div>
<button onclick="appendToDisplay('4')">4</button>
<button onclick="appendToDisplay('5')">5</button>
<button onclick="appendToDisplay('6')">6</button>
<button class="operator" onclick="appendToDisplay('*')">×</button>
</div>
<div>
<button onclick="appendToDisplay('1')">1</button>
<button onclick="appendToDisplay('2')">2</button>
<button onclick="appendToDisplay('3')">3</button>
<button class="operator" onclick="appendToDisplay('-')"></button>
</div>
<div>
<button onclick="appendToDisplay('0')">0</button>
<button class="clear" onclick="clearDisplay()">C</button>
<button class="operator" onclick="calculateResult()">=</button>
<button class="operator" onclick="appendToDisplay('+')">+</button>
</div>
<div>
<button class="operator" onclick="appendToDisplay('%')">%</button>
</div>
</div>
<script>
function appendToDisplay(value) {
const display = document.getElementById('display');
if (display.value === "0") {
display.value = value; // Replace zero if it's the first number
} else {
display.value += value; // Append the value
}
}
function clearDisplay() {
document.getElementById('display').value = '0';
}
function calculateResult() {
const display = document.getElementById('display');
try {
// Replace symbols for eval
let expression = display.value.replace(/×/, '*').replace(/÷/, '/');
display.value = eval(expression);
} catch (error) {
display.value = "错误";
}
}
</script>
</body>
</html>