网页计算器
作者:互联网
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 380px;
height: 500px;
margin: 0 auto;
margin-top: 100px;
border-radius: 5px;
background: rgba(177, 165, 165, 0.58);
border: 3px solid rgba(0, 0, 0, 0.20);
}
table {
margin-top: 20px;
margin-left: 30px;
border-spacing: 20px
}
table td {
width: 50px;
height: 40px;
border: 1px solid rgb(0, 0, 0);
border-radius: 5px;
text-align: center;
}
table input {
border-radius: 5px;
width: 50px;
height: 40px;
outline: none;
}
.top {
width: 330px;
height: 50px;
margin-left: 15px;
margin-top: 50px;
border-radius: 10px;
text-align: right;
background-color: rgb(167, 166, 161);
}
</style>
<body>
<div class="box">
<div>
<input type="text" id="content" disabled="disabled" class="top">
</div>
<div class="contain">
<table>
<!--add, subtract, multiply divide delete 加 减 乘 除 删除-->
<tr>
<td><input type="button" value="CE" id="clear" οnclick="appContent(this)"></td>
<td><input type="button" value="+" id="add" οnclick="appContent(this)"></td>
<td><input type="button" value="-" id="subtract" οnclick="appContent(this)"></td>
<td><input type="button" value="DEL" id="delect" οnclick="appContent(this)"></td>
</tr>
<tr>
<td><input type="button" value="7" id="number7" οnclick="appContent(this)"></td>
<td><input type="button" value="8" id="number8" οnclick="appContent(this)"></td>
<td><input type="button" value="9" id="number9" οnclick="appContent(this)"></td>
<td><input type="button" value="*" id="multiply" οnclick="appContent(this)"></td>
</tr>
<tr>
<td><input type="button" value="4" id="number4" οnclick="appContent(this)"></td>
<td><input type="button" value="5" id="number5" οnclick="appContent(this)"></td>
<td><input type="button" value="6" id="number6" οnclick="appContent(this)"></td>
<td><input type="button" value="/" id="divide" οnclick="appContent(this)"></td>
</tr>
<tr>
<td><input type="button" value="1" id="number1" οnclick="appContent(this)"></td>
<td><input type="button" value="2" id="number2" οnclick="appContent(this)"></td>
<td><input type="button" value="3" id="number3" οnclick="appContent(this)"></td>
<td><input type="button" value="." id="point" οnclick="appContent(this)"></td>
</tr>
<tr>
<td><input type="button" value="(" id="leftbracket" οnclick="appContent(this)"></td>
<td><input type="button" value="0" id="number0" οnclick="appContent(this)"></td>
<td><input type="button" value=")" id="rightbracket" οnclick="appContent(this)"></td>
<td><input type="button" value="=" id="equal" οnclick="appContent(this)"></td>
</tr>
</table>
</div>
</div>
</body>
<script>
function appContent(self) {
var content = document.getElementById('content');
if (self.value != "CE" && self.value != "DEL" && self.value != "=") {
content.value += self.value;
} else if (self.value == "CE") {
//清空
content.value = "";
} else if (self.value == "DEL") {
//删除:截取除最后一个字符串 substring
content.value = content.value.substring(0, content.value.length - 1);
} else if (self.value == "=") {
//判等
var resultText = calculate(content.value);
content.value = content.value + "=" + resultText;
}
}
function calculate(content) {
var index = content.lastIndexOf("(");
if (index > -1) {
var endIndex = content.indexOf(")", index);
if (endIndex > -1) {
var result = calculate(content.substring(index + 1, endIndex));
return calculate(content.substring(0, index) + ("" + result) + content.substring(endIndex + 1))
}
}
index = content.indexOf("+");
if (index > -1) {
return calculate(content.substring(0, index)) + calculate(content.substring(index + 1));
}
index = content.lastIndexOf("-");
if (index > -1) {
return calculate(content.substring(0, index)) - calculate(content.substring(index + 1));
}
index = content.lastIndexOf("*");
if (index > -1) {
return calculate(content.substring(0, index)) * calculate(content.substring(index + 1));
}
index = content.lastIndexOf("/");
if (index > -1) {
return calculate(content.substring(0, index)) / calculate(content.substring(index + 1));
}
if ("" == content) {
return 0;
} else {
return content - 1 + 1;
}
}
</script>
</html>
标签:index,网页,calculate,self,value,content,substring,计算器 来源: https://blog.csdn.net/m0_51443493/article/details/121599787