以一元二次方程计算器简单举例
<!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>一元二次方程计算器</title>
<script>
function print() {
var a1 = document.getElementById("aa").value
var b1 = document.getElementById("bb").value
var c1 = document.getElementById("cc").value
m = b1 ** 2 - 4 * a1 * c1
if (a1 == "" || b1 == "" || c1 == "") {
alert('请输入方程系数')
} else {
if (m >= 0) {
x1 = (-b1 + Math.sqrt(m)) / (2 * a1)
x2 = (-b1 - Math.sqrt(m)) / (2 * a1)
document.getElementById("x1").value = x1;
document.getElementById("x2").value = x2;
} else {
alert('该方程无解')
}
}
}
</script>
</head>
<body>
<h4 align="center">一元二次方程计算器</h4>
<form action="">
<div align="center">
<input type="number" id="aa" style="width: 80px;">
x2+
<input type="number" id="bb" style="width: 80px;">
x+
<input type="number" id="cc" style="width: 80px;">
=0
</div>
<table align="center">
<tr>
<td>计算结果为如下</td>
</tr>
<tr>
<td>第一个解为:</td>
<td><input type="number" id="x1" style="width: 100px;"></td>
</tr>
<tr>
<td>第二个解为:</td>
<td><input type="number" id="x2" style="width: 100px;"></td>
</tr>
<tr>
<td><input type="button" value="计算结果" onclick="print()" /></td>
<td><input type="reset" value="重新填写"></td>
</tr>
</table>
</form>
</body>
</html>
输出结果图如下