写一个科学计算器的web前端 html格式的
时间: 2023-06-26 14:05:16 浏览: 81
以下是一个简单的科学计算器的Web前端HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>科学计算器</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
}
h1 {
text-align: center;
margin-top: 50px;
color: #333;
}
.calculator {
margin: 0 auto;
width: 400px;
padding: 20px;
background-color: #fff;
box-shadow: 2px 2px 8px rgba(0,0,0,0.3);
}
input[type="text"] {
width: 100%;
height: 40px;
font-size: 24px;
padding: 5px 10px;
border-radius: 5px;
border: none;
margin-bottom: 20px;
box-shadow: 1px 1px 3px rgba(0,0,0,0.1);
}
input[type="button"] {
width: 30%;
height: 40px;
font-size: 18px;
margin-right: 5%;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
box-shadow: 1px 1px 3px rgba(0,0,0,0.1);
}
input[type="button"]:hover {
background-color: #3e8e41;
}
.clear {
width: 60%;
height: 40px;
font-size: 18px;
margin-right: 5%;
background-color: #f44336;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
box-shadow: 1px 1px 3px rgba(0,0,0,0.1);
}
.clear:hover {
background-color: #d32f2f;
}
.row:after {
content: "";
display: table;
clear: both;
}
.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}
.col-50 {
float: left;
width: 50%;
margin-top: 6px;
}
.col-75 {
float: left;
width: 75%;
margin-top: 6px;
}
@media screen and (max-width: 768px) {
.col-25, .col-50, .col-75 {
width: 100%;
margin-top: 0;
}
input[type="button"], .clear {
width: 48%;
margin-right: 2%;
}
}
</style>
</head>
<body>
<h1>科学计算器</h1>
<div class="calculator">
<form>
<div class="row">
<div class="col-75">
<input type="text" id="result" name="result" placeholder="0" disabled>
</div>
</div>
<div class="row">
<div class="col-25">
<input type="button" value="(" onclick="insert('(', 'result')">
</div>
<div class="col-25">
<input type="button" value=")" onclick="insert(')', 'result')">
</div>
<div class="col-25">
<input type="button" value="CE" class="clear" onclick="clearResult('result')">
</div>
<div class="col-25">
<input type="button" value="C" class="clear" onclick="clearAll('result')">
</div>
</div>
<div class="row">
<div class="col-25">
<input type="button" value="7" onclick="insert('7', 'result')">
</div>
<div class="col-25">
<input type="button" value="8" onclick="insert('8', 'result')">
</div>
<div class="col-25">
<input type="button" value="9" onclick="insert('9', 'result')">
</div>
<div class="col-25">
<input type="button" value="/" onclick="insert('/', 'result')">
</div>
</div>
<div class="row">
<div class="col-25">
<input type="button" value="4" onclick="insert('4', 'result')">
</div>
<div class="col-25">
<input type="button" value="5" onclick="insert('5', 'result')">
</div>
<div class="col-25">
<input type="button" value="6" onclick="insert('6', 'result')">
</div>
<div class="col-25">
<input type="button" value="*" onclick="insert('*', 'result')">
</div>
</div>
<div class="row">
<div class="col-25">
<input type="button" value="1" onclick="insert('1', 'result')">
</div>
<div class="col-25">
<input type="button" value="2" onclick="insert('2', 'result')">
</div>
<div class="col-25">
<input type="button" value="3" onclick="insert('3', 'result')">
</div>
<div class="col-25">
<input type="button" value="-" onclick="insert('-', 'result')">
</div>
</div>
<div class="row">
<div class="col-25">
<input type="button" value="0" onclick="insert('0', 'result')">
</div>
<div class="col-25">
<input type="button" value="." onclick="insert('.', 'result')">
</div>
<div class="col-25">
<input type="button" value="=" onclick="calculate('result')">
</div>
<div class="col-25">
<input type="button" value="+" onclick="insert('+', 'result')">
</div>
</div>
</form>
</div>
<script>
function insert(value, id) {
document.getElementById(id).value += value;
}
function clearResult(id) {
var result = document.getElementById(id);
result.value = result.value.slice(0, -1);
}
function clearAll(id) {
document.getElementById(id).value = "";
}
function calculate(id) {
var result = document.getElementById(id);
result.value = eval(result.value);
}
</script>
</body>
</html>
```
这段代码定义了一个简单的科学计算器,包括数字、运算符、括号等基本功能。用户可以通过在输入框中输入表达式,然后按下“=”按钮计算结果。同时,用户还可以使用“CE”按钮删除最后一个字符,使用“C”按钮清空输入框。
阅读全文