使用简单的html+css+js写出一个计算器程序
时间: 2023-08-07 15:03:25 浏览: 144
HTML+CSS+JS制作一个简单的计算器
以下是一个简单的计算器程序的HTML、CSS和JS代码:
HTML代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="calculator">
<div class="display"></div>
<button class="button" id="clear">AC</button>
<button class="button" id="sign">+/-</button>
<button class="button" id="percent">%</button>
<button class="button operator" id="divide">÷</button>
<button class="button" id="7">7</button>
<button class="button" id="8">8</button>
<button class="button" id="9">9</button>
<button class="button operator" id="multiply">×</button>
<button class="button" id="4">4</button>
<button class="button" id="5">5</button>
<button class="button" id="6">6</button>
<button class="button operator" id="subtract">-</button>
<button class="button" id="1">1</button>
<button class="button" id="2">2</button>
<button class="button" id="3">3</button>
<button class="button operator" id="add">+</button>
<button class="button zero" id="0">0</button>
<button class="button" id="decimal">.</button>
<button class="button operator" id="equals">=</button>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.calculator {
width: 300px;
margin: 50px auto;
background-color: #eee;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
padding: 20px;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
}
.display {
grid-column: 1 / -1;
background-color: #fff;
border-radius: 5px;
padding: 10px;
font-size: 24px;
text-align: right;
}
.button {
background-color: #ccc;
border: none;
border-radius: 5px;
padding: 10px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.2s ease;
}
.button:hover {
background-color: #bbb;
}
.operator {
background-color: #ff9500;
color: #fff;
}
.zero {
grid-column: 1 / span 2;
}
#equals {
background-color: #ff9500;
color: #fff;
grid-row: 5 / span 2;
}
#clear {
background-color: #d9534f;
color: #fff;
}
#sign {
background-color: #f0ad4e;
color: #fff;
}
#percent {
background-color: #5bc0de;
color: #fff;
}
```
JS代码:
```js
let display = document.querySelector('.display');
let clear = document.querySelector('#clear');
let sign = document.querySelector('#sign');
let percent = document.querySelector('#percent');
let divide = document.querySelector('#divide');
let multiply = document.querySelector('#multiply');
let subtract = document.querySelector('#subtract');
let add = document.querySelector('#add');
let equals = document.querySelector('#equals');
let decimal = document.querySelector('#decimal');
let buttons = document.querySelectorAll('.button:not(#clear):not(#sign):not(#percent):not(#divide):not(#multiply):not(#subtract):not(#add):not(#equals):not(#decimal)');
let input = '';
let operator = '';
let result = '';
buttons.forEach(button => {
button.addEventListener('click', () => {
input += button.textContent;
display.textContent = input;
});
});
clear.addEventListener('click', () => {
input = '';
operator = '';
result = '';
display.textContent = '0';
});
sign.addEventListener('click', () => {
input = input * -1;
display.textContent = input;
});
percent.addEventListener('click', () => {
input = input / 100;
display.textContent = input;
});
divide.addEventListener('click', () => {
operator = '/';
result = input;
input = '';
});
multiply.addEventListener('click', () => {
operator = '*';
result = input;
input = '';
});
subtract.addEventListener('click', () => {
operator = '-';
result = input;
input = '';
});
add.addEventListener('click', () => {
operator = '+';
result = input;
input = '';
});
equals.addEventListener('click', () => {
switch(operator) {
case '/':
input = result / input;
break;
case '*':
input = result * input;
break;
case '-':
input = Number(result) - Number(input);
break;
case '+':
input = Number(result) + Number(input);
break;
}
display.textContent = input;
});
decimal.addEventListener('click', () => {
if (!input.includes('.')) {
input += '.';
display.textContent = input;
}
});
```
上述代码实现了一个简单的四则运算计算器,包含基本的功能按钮、数字按钮、小数点按钮、清空按钮、正负号按钮、百分号按钮等。用户可以通过点击按钮来输入数字和运算符,计算器可以实时显示用户输入的结果,并且支持实现四则运算。
阅读全文