作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class="calculator"><div class="input-group"><label for="equation1">输入第一个方程 (例如: 2x + 3y = 5):</label> <input id="equation1" type="text" placeholder="例如: 2x + 3y = 5"></div><div class="input-group"><label for="equation2">输入第二个方程 (例如: 4x - y = 2):</label> <input id="equation2" type="text" placeholder="例如: 4x - y = 2"></div><div class="operations"><button onclick="solveEquations()">计算</button></div><div class="result"><p>计算结果:</p><p id="result-output">结果: -</p></div></div>
JS:
function parseEquation(equation) {
const match = equation.match(/([+-]?\d*)x\s*([+-]\s*\d*)y\s*=\s*([+-]?\d+)/);
if (match) {
return {
a: parseFloat(match[1] || 1),
b: parseFloat(match[2].replace(/\s+/g, '') || 1),
c: parseFloat(match[3])
};
} else {
throw new Error('方程格式错误');
}
}
function solveEquations() {
const eq1 = document.getElementById('equation1').value;
const eq2 = document.getElementById('equation2').value;
try {
const { a: a1, b: b1, c: c1 } = parseEquation(eq1);
const { a: a2, b: b2, c: c2 } = parseEquation(eq2);
const det = a1 * b2 - a2 * b1;
if (det === 0) {
throw new Error('方程组无解或有无穷多解');
}
const x = (c1 * b2 - c2 * b1) / det;
const y = (a1 * c2 - a2 * c1) / det;
document.getElementById('result-output').textContent = `解: x = ${x}, y = ${y}`;
} catch (e) {
document.getElementById('result-output').textContent = `错误: ${e.message}`;
}
}
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;
}
标签:const,方程组,color,important,match,background,计算器,margin,就是
From: https://blog.csdn.net/yuchangchenTT/article/details/143678961