Compare commits

..

No commits in common. 'master' and 'main' have entirely different histories.
master ... main

@ -0,0 +1,2 @@
# jisuanqi

@ -0,0 +1,4 @@
public class Test {
}

@ -26,7 +26,7 @@
width: 100%; width: 100%;
height: 50px; height: 50px;
margin-bottom: 10px; margin-bottom: 10px;
padding: 10px; padding: 0px;
font-size: 24px; font-size: 24px;
text-align: right; text-align: right;
border: none; border: none;
@ -61,89 +61,95 @@
</style> </style>
</head> </head>
<body> <body>
<div class="calculator"> <div class="calculator">
<input type="text" id="display" disabled> <input type="text" id="display" disabled>
<div class="buttons"> <div class="buttons">
<button onclick="clearDisplay()">C</button> <button onclick="clearDisplay()">C</button>
<button onclick="appendNumber('7')">7</button> <button onclick="appendNumber('7')">7</button>
<button onclick="appendNumber('8')">8</button> <button onclick="appendNumber('8')">8</button>
<button onclick="appendNumber('9')">9</button> <button onclick="appendNumber('9')">9</button>
<button onclick="chooseOperation('/')">/</button> <button onclick="chooseOperation('/')">/</button>
<button onclick="appendNumber('4')">4</button> <button onclick="appendNumber('4')">4</button>
<button onclick="appendNumber('5')">5</button> <button onclick="appendNumber('5')">5</button>
<button onclick="appendNumber('6')">6</button> <button onclick="appendNumber('6')">6</button>
<button onclick="chooseOperation('*')">*</button> <button onclick="chooseOperation('*')">*</button>
<button onclick="appendNumber('1')">1</button> <button onclick="appendNumber('1')">1</button>
<button onclick="appendNumber('2')">2</button> <button onclick="appendNumber('2')">2</button>
<button onclick="appendNumber('3')">3</button> <button onclick="appendNumber('3')">3</button>
<button onclick="chooseOperation('-')">-</button> <button onclick="chooseOperation('-')">-</button>
<button onclick="appendNumber('0')">0</button> <button onclick="appendNumber('0')">0</button>
<button onclick="appendNumber('.')">.</button> <button onclick="appendNumber('.')">.</button>
<button onclick="compute()">=</button> <button onclick="compute()">=</button>
<button onclick="chooseOperation('+')">+</button> <button onclick="chooseOperation('+')">+</button>
</div> <button onclick="chooseOperation('%')">%</button>
<button onclick="appendNumber('-')">±</button>
</div> </div>
</div>
<script> <script>
let currentOperand = ''; let currentOperand = '';
let previousOperand = ''; let previousOperand = '';
let operation = undefined; let operation = undefined;
const display = document.getElementById('display'); const display = document.getElementById('display');
function appendNumber(number) { function appendNumber(number) {
if (number === '.' && currentOperand.includes('.')) return; if (number === '.' && currentOperand.includes('.')) return;
currentOperand += number; if (number === '-' && currentOperand.length > 0) return; // 防止在数字中间添加负号
updateDisplay(); currentOperand += number;
} updateDisplay();
}
function chooseOperation(op) { function chooseOperation(op) {
if (currentOperand === '') return; if (currentOperand === '') return;
if (previousOperand !== '') { if (previousOperand !== '') {
compute(); compute();
}
operation = op;
previousOperand = currentOperand;
currentOperand = '';
} }
operation = op;
previousOperand = currentOperand;
currentOperand = '';
}
function compute() { function compute() {
let computation; let computation;
const prev = parseFloat(previousOperand); const prev = parseFloat(previousOperand);
const current = parseFloat(currentOperand); const current = parseFloat(currentOperand);
if (isNaN(prev) || isNaN(current)) return; if (isNaN(prev) || isNaN(current)) return;
switch (operation) { switch (operation) {
case '+': case '+':
computation = prev + current; computation = prev + current;
break; break;
case '-': case '-':
computation = prev - current; computation = prev - current;
break; break;
case '*': case '*':
computation = prev * current; computation = prev * current;
break; break;
case '/': case '/':
computation = prev / current; computation = prev / current;
break; break;
default: case '%':
return; computation = prev % current;
} break;
currentOperand = computation.toString(); default:
operation = undefined; return;
previousOperand = '';
updateDisplay();
} }
currentOperand = computation.toString();
operation = undefined;
previousOperand = '';
updateDisplay();
}
function clearDisplay() { function clearDisplay() {
currentOperand = ''; currentOperand = '';
previousOperand = ''; previousOperand = '';
operation = undefined; operation = undefined;
updateDisplay(); updateDisplay();
} }
function updateDisplay() { function updateDisplay() {
display.value = currentOperand; display.value = currentOperand;
} }
</script> </script>
</body> </body>
</html> </html>
Loading…
Cancel
Save