You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculator/calculator.html

116 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body,html{
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
button {
width: 40px;
height: 40px;
font-size: 16px;
margin-top: 2px;
}
#back {
width: 85px;
height: 40px;
}
#t1 {
width: 170px;
margin-bottom: 5px;
padding: 3px 0px 3px 0px;
}
.all {
border: 1px solid black;
width: 175px;
padding:0px 2px 2px 2px;
}
p{
text-align: center;
}
</style>
<body>
<div class="all">
<p>计算器</p>
<div>
<input type="text" id="t1">
</div>
<div>
<button onclick="cls()" id="C">C</button>
<button onclick="addStr('%')">%</button>
<button onclick="del()" id="back"></button>
</div>
<div>
<div>
<button onclick="addStr(7)">7</button>
<button onclick="addStr(8)">8</button>
<button onclick="addStr(9)">9</button>
<button onclick="addStr('/')">÷</button>
</div>
<div>
<button onclick="addStr(4)">4</button>
<button onclick="addStr(5)">5</button>
<button onclick="addStr(6)">6</button>
<button onclick="addStr('*')">×</button>
</div>
<div>
<button onclick="addStr(1)">1</button>
<button onclick="addStr(2)">2</button>
<button onclick="addStr(3)">3</button>
<button onclick="addStr('-')">-</button>
</div>
<div>
<button onclick="addStr('.')">.</button>
<button onclick="addStr(0)" id="zero">0</button>
<button onclick="calc()" id="equal">=</button>
<button onclick="addStr('+')">+</button>
</div>
</div>
</div>
<script>
function addStr(s) {
var str = document.getElementById("t1");
str.value = str.value + s;
}
function calc() {
var str = document.getElementById("t1");
try {
var res = eval(str.value);
if (!isNaN(res) && isFinite(res)) {
str.value = res;
} else {
str.value = "错误";
}
} catch (e) {
str.value = "错误";
}
}
function cls() {
var str = document.getElementById("t1");
str.value = "";
}
function del() {
var str = document.getElementById("t1");
var str1 = str.value.substr(0, str.value.length - 1);
str.value = str1;
}
</script>
</body>
</html>