求最大公约比例、最大公约数、最小公倍数:
链接:
语法:
comNum(num, mand, check)
参数:
<num> 数值组(必要)
[数值1, 数值2]
:代入值。
<mand> 求值命令
空 | "ratio"
:求最大公约比例(返回数值1:数值2
比例的数组)。"max" | "gcd"
:求最大公约数。"min" | "scm"
:求最小公倍数。"all"
:求全部(返回一个数组分别为最大公约比例、最大公约数、最小公倍数)。
<check> 是否检查数值正确
空 | true
:检查(可防止死循环)。false
:不检查。
注释:
- <num>与<mand>为数值时可分别充当原<num>的数值组内的值, 原<mand>将克隆为<check>的功能,其它不变。
兼容性:
Chrome, Firefox, Opera, Safari, Edge, IE :全兼容
函数:
function comNum(num, mand, check) {
if (!Array.isArray) {Array.isArray = function(a) { return Object.prototype.toString.call(a) === "[object Array]" }}
if ((mand === false && (!check || check === false) && Array.isArray(num)) ? false : (check == undefined ? true : check)) {
var checkVal = function(val, mode) {
if (mode == "str_NOnum") { return typeof val == "string" && /\D/.test(val) && !/\d/.test(val) }
else if (mode == "str_ONLYnum") { return typeof val == "string" && /^\d*?\.?\d+$/.test(val) }
else if (mode == "num_ORstrNum") { return typeof val === "number" || (typeof val == "string" && /^\d*?\.?\d+$/.test(val)) }
}
if (mand && typeof mand == "string") {
if (checkVal(mand, "str_NOnum")) { mand = mand.toLowerCase() }
else if (!/^\d*?\.?\d+$/.test(mand)) { return false }
}
if (!Array.isArray(num)) {
if (checkVal(num, "str_ONLYnum")) { num = Number(num) }
else if (typeof num != "number") { return false }
if (!mand) { return false }
else if (checkVal(mand, "num_ORstrNum")) { num = [num, Number(mand)], mand = "all" }
} else {
if (checkVal(num[0], "num_ORstrNum")) { num[0] = Number(num[0]) } else { return false }
if (checkVal(num[1], "num_ORstrNum")) { num[1] = Number(num[1]) } else { return false }
}
} else if (!Array.isArray(num)) { num = [num, mand], mand = "all" }
// 不需要检查数值则将以上代码删除即可
var gcd = function(a, b, r) { while(b!=0) { r=a%b; a=b; b=r } return a }
var scm = function(a, b) { return (a*b) / gcd(a, b) }
var ratio = function(a, b) { var g = gcd(a, b); return [a/g, b/g] }
if (mand == "max" || mand == "gcd") { return gcd(num[0], num[1]) }
else if (mand == "min" || mand == "scm") { return scm(num[0], num[1]) }
else if (mand == "all") { return [ratio(num[0], num[1]), gcd(num[0], num[1]), scm(num[0], num[1])] }
else if (!mand || mand == "ratio") { return ratio(num[0], num[1]) }
}
示例:
<html><head><meta charset="utf-8"></head><body>
<div>
<input type="text" placeholder="数值1" id="a">
<input type="text" placeholder="数值2" id="b">
</div>
<div>
<select id="mand">
<option value="all">求全部</option>
<option value="ratio">求最大公约比例</option>
<option value="gcd">求最大公约数</option>
<option value="scm">求最小公倍数</option>
</select>
<input type="checkbox" checked id="check">检查数值
</div>
<input type="button" value="执行" onclick="run()">
<p id="result"></p>
<script>
function run() {
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var mand = document.getElementById("mand").value;
var check = document.getElementById("check").checked;
var res = comNum([a,b], mand, check); console.log(res);
if (res) {
var r1 = r2 = r3 = "";
if (mand == "ratio") { r1 = res[0]+":"+res[1] }
else if (mand == "gcd") { r2 = res }
else if (mand == "scm") { r3 = res }
else if (mand == "all") {
r1 = res[0][0]+":"+res[0][1];
r2 = res[1];
r3 = res[2];
}
r1 = r1 ? "最大公约比例 = " + r1 + "<br>" : "";
r2 = r2 ? "最大公约数 = " + r2 + "<br>" : "";
r3 = r3 ? "最小公倍数 = " + r3 : "";
document.getElementById("result").innerHTML = r1 + r2 + r3;
} else {
document.getElementById("result").innerHTML = "数值出错";
}
}
function comNum(num, mand, check) {
// ...
}
</script>
</body></html>