作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class="calculator"><label for="side1">输入边长 a:</label> <input id="side1" step="0.01" type="number" placeholder="边长 a"> <label for="side2">输入边长 b:</label> <input id="side2" step="0.01" type="number" placeholder="边长 b"> <label for="angleC">输入夹角 C(度):</label> <input id="angleC" step="0.01" type="number" placeholder="夹角 C"><button onclick="calculateSide()">计算第三边</button><div id="resultSide" class="result">结果将显示在这里</div><label for="sideA">输入边长 a:</label> <input id="sideA" step="0.01" type="number" placeholder="边长 a"> <label for="sideB">输入边长 b:</label> <input id="sideB" step="0.01" type="number" placeholder="边长 b"> <label for="sideC">输入边长 c:</label> <input id="sideC" step="0.01" type="number" placeholder="边长 c"><button onclick="calculateAngle()">计算夹角</button><div id="resultAngle" class="result">结果将显示在这里</div></div>
JS:
function calculateSide() {
const a = parseFloat(document.getElementById('side1').value);
const b = parseFloat(document.getElementById('side2').value);
const C = parseFloat(document.getElementById('angleC').value);
if (isNaN(a) || isNaN(b) || isNaN(C) || C < 0 || C > 180) {
document.getElementById('resultSide').textContent = "请输入有效的边长和夹角。";
return;
}
// 将角度转换为弧度
const C_rad = C * (Math.PI / 180);
// 使用余弦定律计算第三边
const c = Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(C_rad));
document.getElementById('resultSide').textContent = `第三边 c 的长度是:${c.toFixed(4)}`;
}
function calculateAngle() {
const a = parseFloat(document.getElementById('sideA').value);
const b = parseFloat(document.getElementById('sideB').value);
const c = parseFloat(document.getElementById('sideC').value);
if (isNaN(a) || isNaN(b) || isNaN(c) || a <= 0 || b <= 0 || c <= 0) {
document.getElementById('resultAngle').textContent = "请输入有效的边长。";
return;
}
// 使用余弦定律计算夹角
const C_rad = Math.acos((a * a + b * b - c * c) / (2 * a * b));
// 将弧度转换为角度
const C_deg = C_rad * (180 / Math.PI);
document.getElementById('resultAngle').textContent = `夹角 C 的度数是:${C_deg.toFixed(4)}°`;
}
CSS:
.calculator {
width: 100%;
background-color: #333;
color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}
label {
display: block;
margin-bottom: 10px;
font-size: 16px;
}
input, select {
width: 100%!important;
padding: 10px!important;
margin-bottom: 20px;
color: #000000;
border-radius: 5px;
border: 1px solid #555;
font-size: 16px!important;
background-color: #ffffff!important;
}
button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
display: block;
margin: 0px;
margin-bottom: 20px;
margin-left: 0px!important;
}
button:hover {
background-color: orange;
}
.result {
margin-top: 20px;
text-align: center;
}
option {
background-color: #ffffff;
}
p {
font-size: 18px;
margin-top: 5px!important;
}