设计和编写一个简易的计算器:实现计算器的界面;实现计算按钮不同的计算功能;显示计算器按钮运算后的结果。要求:按钮C在输入框旁边,剩下的按钮顺序为 1 2 3 + 4 5 6 - 7 8 9 * 0 . = /
时间: 2023-11-27 19:49:40 浏览: 120
以下是一个简易的计算器的界面和实现:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>简易计算器</title>
<style type="text/css">
#calculator {
width: 300px;
margin: 0 auto;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 5px #ccc;
}
#display {
width: 100%;
height: 50px;
margin-bottom: 10px;
padding: 5px;
font-size: 24px;
text-align: right;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: inset 0 0 5px #ccc;
background-color: #eee;
}
.btn {
display: inline-block;
width: 60px;
height: 40px;
margin: 5px;
padding: 5px;
font-size: 18px;
text-align: center;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 5px #ccc;
cursor: pointer;
background-color: #fff;
}
.btn:hover {
background-color: #f5f5f5;
}
.btn.active {
background-color: #ccc;
}
</style>
</head>
<body>
<div id="calculator">
<div id="display"></div>
<div>
<div class="btn" id="btn-c">C</div>
<div class="btn" id="btn-1">1</div>
<div class="btn" id="btn-2">2</div>
<div class="btn" id="btn-3">3</div>
<div class="btn" id="btn-plus">+</div>
</div>
<div>
<div class="btn" id="btn-4">4</div>
<div class="btn" id="btn-5">5</div>
<div class="btn" id="btn-6">6</div>
<div class="btn" id="btn-minus">-</div>
</div>
<div>
<div class="btn" id="btn-7">7</div>
<div class="btn" id="btn-8">8</div>
<div class="btn" id="btn-9">9</div>
<div class="btn" id="btn-multiply">*</div>
</div>
<div>
<div class="btn" id="btn-0">0</div>
<div class="btn" id="btn-dot">.</div>
<div class="btn" id="btn-equals">=</div>
<div class="btn" id="btn-divide">/</div>
</div>
</div>
<script type="text/javascript">
// 获取元素
var display = document.getElementById('display');
var btnC = document.getElementById('btn-c');
var btn1 = document.getElementById('btn-1');
var btn2 = document.getElementById('btn-2');
var btn3 = document.getElementById('btn-3');
var btnPlus = document.getElementById('btn-plus');
var btn4 = document.getElementById('btn-4');
var btn5 = document.getElementById('btn-5');
var btn6 = document.getElementById('btn-6');
var btnMinus = document.getElementById('btn-minus');
var btn7 = document.getElementById('btn-7');
var btn8 = document.getElementById('btn-8');
var btn9 = document.getElementById('btn-9');
var btnMultiply = document.getElementById('btn-multiply');
var btn0 = document.getElementById('btn-0');
var btnDot = document.getElementById('btn-dot');
var btnEquals = document.getElementById('btn-equals');
var btnDivide = document.getElementById('btn-divide');
// 定义变量
var num1 = '';
var num2 = '';
var operator = '';
var result = '';
// 按钮点击事件
btnC.addEventListener('click', function() {
num1 = '';
num2 = '';
operator = '';
result = '';
display.innerHTML = '';
});
btn1.addEventListener('click', function() {
if (operator === '') {
num1 += '1';
} else {
num2 += '1';
}
display.innerHTML += '1';
});
btn2.addEventListener('click', function() {
if (operator === '') {
num1 += '2';
} else {
num2 += '2';
}
display.innerHTML += '2';
});
btn3.addEventListener('click', function() {
if (operator === '') {
num1 += '3';
} else {
num2 += '3';
}
display.innerHTML += '3';
});
btnPlus.addEventListener('click', function() {
operator = '+';
display.innerHTML += '+';
});
btn4.addEventListener('click', function() {
if (operator === '') {
num1 += '4';
} else {
num2 += '4';
}
display.innerHTML += '4';
});
btn5.addEventListener('click', function() {
if (operator === '') {
num1 += '5';
} else {
num2 += '5';
}
display.innerHTML += '5';
});
btn6.addEventListener('click', function() {
if (operator === '') {
num1 += '6';
} else {
num2 += '6';
}
display.innerHTML += '6';
});
btnMinus.addEventListener('click', function() {
operator = '-';
display.innerHTML += '-';
});
btn7.addEventListener('click', function() {
if (operator === '') {
num1 += '7';
} else {
num2 += '7';
}
display.innerHTML += '7';
});
btn8.addEventListener('click', function() {
if (operator === '') {
num1 += '8';
} else {
num2 += '8';
}
display.innerHTML += '8';
});
btn9.addEventListener('click', function() {
if (operator === '') {
num1 += '9';
} else {
num2 += '9';
}
display.innerHTML += '9';
});
btnMultiply.addEventListener('click', function() {
operator = '*';
display.innerHTML += '*';
});
btn0.addEventListener('click', function() {
if (operator === '') {
num1 += '0';
} else {
num2 += '0';
}
display.innerHTML += '0';
});
btnDot.addEventListener('click', function() {
if (operator === '') {
if (num1.indexOf('.') === -1) {
num1 += '.';
display.innerHTML += '.';
}
} else {
if (num2.indexOf('.') === -1) {
num2 += '.';
display.innerHTML += '.';
}
}
});
btnEquals.addEventListener('click', function() {
if (operator === '+') {
result = parseFloat(num1) + parseFloat(num2);
} else if (operator === '-') {
result = parseFloat(num1) - parseFloat(num2);
} else if (operator === '*') {
result = parseFloat(num1) * parseFloat(num2);
} else if (operator === '/') {
result = parseFloat(num1) / parseFloat(num2);
}
display.innerHTML = result;
num1 = result.toString();
num2 = '';
operator = '';
result = '';
});
btnDivide.addEventListener('click', function() {
operator = '/';
display.innerHTML += '/';
});
</script>
</body>
</html>
```
这个计算器界面比较简陋,只有基本的按钮和显示框,但是已经能够实现简单的加减乘除运算了。
阅读全文