|
|
|
@ -26,7 +26,7 @@
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 50px;
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
padding: 0px;
|
|
|
|
|
padding: 10px;
|
|
|
|
|
font-size: 24px;
|
|
|
|
|
text-align: right;
|
|
|
|
|
border: none;
|
|
|
|
@ -61,95 +61,89 @@
|
|
|
|
|
</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>
|
|
|
|
|
<button onclick="appendNumber('-')">±</button>
|
|
|
|
|
<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>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
let currentOperand = '';
|
|
|
|
|
let previousOperand = '';
|
|
|
|
|
let operation = undefined;
|
|
|
|
|
<script>
|
|
|
|
|
let currentOperand = '';
|
|
|
|
|
let previousOperand = '';
|
|
|
|
|
let operation = undefined;
|
|
|
|
|
|
|
|
|
|
const display = document.getElementById('display');
|
|
|
|
|
const display = document.getElementById('display');
|
|
|
|
|
|
|
|
|
|
function appendNumber(number) {
|
|
|
|
|
if (number === '.' && currentOperand.includes('.')) return;
|
|
|
|
|
if (number === '-' && currentOperand.length > 0) return; // 防止在数字中间添加负号
|
|
|
|
|
currentOperand += number;
|
|
|
|
|
updateDisplay();
|
|
|
|
|
}
|
|
|
|
|
function appendNumber(number) {
|
|
|
|
|
if (number === '.' && currentOperand.includes('.')) return;
|
|
|
|
|
currentOperand += number;
|
|
|
|
|
updateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function chooseOperation(op) {
|
|
|
|
|
if (currentOperand === '') return;
|
|
|
|
|
if (previousOperand !== '') {
|
|
|
|
|
compute();
|
|
|
|
|
function chooseOperation(op) {
|
|
|
|
|
if (currentOperand === '') return;
|
|
|
|
|
if (previousOperand !== '') {
|
|
|
|
|
compute();
|
|
|
|
|
}
|
|
|
|
|
operation = op;
|
|
|
|
|
previousOperand = currentOperand;
|
|
|
|
|
currentOperand = '';
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
currentOperand = computation.toString();
|
|
|
|
|
operation = undefined;
|
|
|
|
|
previousOperand = '';
|
|
|
|
|
updateDisplay();
|
|
|
|
|
}
|
|
|
|
|
currentOperand = computation.toString();
|
|
|
|
|
operation = undefined;
|
|
|
|
|
previousOperand = '';
|
|
|
|
|
updateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clearDisplay() {
|
|
|
|
|
currentOperand = '';
|
|
|
|
|
previousOperand = '';
|
|
|
|
|
operation = undefined;
|
|
|
|
|
updateDisplay();
|
|
|
|
|
}
|
|
|
|
|
function clearDisplay() {
|
|
|
|
|
currentOperand = '';
|
|
|
|
|
previousOperand = '';
|
|
|
|
|
operation = undefined;
|
|
|
|
|
updateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateDisplay() {
|
|
|
|
|
display.value = currentOperand;
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
function updateDisplay() {
|
|
|
|
|
display.value = currentOperand;
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
</html>
|