使用js写一个简单的计算器
时间: 2023-06-01 22:03:56 浏览: 115
以下是一个简单的计算器示例,使用JavaScript编写:
HTML代码:
```
<div id="calculator">
<input type="text" id="screen" readonly>
<table>
<tr>
<td><button onclick="clearScreen()">C</button></td>
<td><button onclick="toggleSign()">+/-</button></td>
<td><button onclick="backspace()">←</button></td>
<td><button onclick="operate('/')">÷</button></td>
</tr>
<tr>
<td><button onclick="appendNumber(7)">7</button></td>
<td><button onclick="appendNumber(8)">8</button></td>
<td><button onclick="appendNumber(9)">9</button></td>
<td><button onclick="operate('*')">x</button></td>
</tr>
<tr>
<td><button onclick="appendNumber(4)">4</button></td>
<td><button onclick="appendNumber(5)">5</button></td>
<td><button onclick="appendNumber(6)">6</button></td>
<td><button onclick="operate('-')">-</button></td>
</tr>
<tr>
<td><button onclick="appendNumber(1)">1</button></td>
<td><button onclick="appendNumber(2)">2</button></td>
<td><button onclick="appendNumber(3)">3</button></td>
<td><button onclick="operate('+')">+</button></td>
</tr>
<tr>
<td><button onclick="appendNumber(0)">0</button></td>
<td><button onclick="appendDecimal()">.</button></td>
<td><button onclick="calculate()">=</button></td>
</tr>
</table>
</div>
```
JavaScript代码:
```
let currentNumber = 0;
let operator = null;
let waitingForSecondNumber = false;
function appendNumber(number) {
const screen = document.getElementById('screen');
if (waitingForSecondNumber) {
screen.value = number;
waitingForSecondNumber = false;
} else {
if (screen.value === '0') {
screen.value = number;
} else {
screen.value += number;
}
}
currentNumber = parseFloat(screen.value);
}
function appendDecimal() {
const screen = document.getElementById('screen');
if (waitingForSecondNumber) {
screen.value = '0.';
waitingForSecondNumber = false;
} else {
if (screen.value.indexOf('.') === -1) {
screen.value += '.';
}
}
currentNumber = parseFloat(screen.value);
}
function clearScreen() {
const screen = document.getElementById('screen');
screen.value = '0';
currentNumber = 0;
operator = null;
waitingForSecondNumber = false;
}
function toggleSign() {
const screen = document.getElementById('screen');
screen.value = -parseFloat(screen.value);
currentNumber = parseFloat(screen.value);
}
function backspace() {
const screen = document.getElementById('screen');
screen.value = screen.value.slice(0, -1);
if (screen.value === '') {
screen.value = '0';
}
currentNumber = parseFloat(screen.value);
}
function operate(newOperator) {
const screen = document.getElementById('screen');
if (operator !== null && !waitingForSecondNumber) {
calculate();
}
waitingForSecondNumber = true;
operator = newOperator;
}
function calculate() {
const screen = document.getElementById('screen');
let result = 0;
switch (operator) {
case '+':
result = currentNumber + parseFloat(screen.value);
break;
case '-':
result = currentNumber - parseFloat(screen.value);
break;
case '*':
result = currentNumber * parseFloat(screen.value);
break;
case '/':
result = currentNumber / parseFloat(screen.value);
break;
}
screen.value = result.toString();
currentNumber = result;
waitingForSecondNumber = true;
}
```
该计算器包括数字、小数点、正负号、加、减、乘、除、清除、删除和等于按钮。用户可以通过单击数字和运算符按钮来输入表达式,然后单击等于按钮来计算结果。
阅读全文