155 lines
4.7 KiB
JavaScript
155 lines
4.7 KiB
JavaScript
var clipboard = new ClipboardJS('.btn-copy');
|
||
var xelem = document.getElementById("d");
|
||
var sel1elem = document.getElementById("from");
|
||
var sel2elem = document.getElementById("to");
|
||
var yelem = document.getElementById("y");
|
||
var calgroup = document.getElementById("calgroup");
|
||
var cal1elem = document.getElementById("cal1");
|
||
var cal2elem = document.getElementById("cal2");
|
||
window.addEventListener("DOMContentLoaded", function () {
|
||
calgroup.style.display = "none";
|
||
var params = GetURLParams();
|
||
/*
|
||
if (Object.keys(params).length > 0 && params.d != "") {
|
||
xelem.value = params.d;
|
||
sel1elem.selectedIndex = params.from - 2;
|
||
sel2elem.selectedIndex = params.to - 2;
|
||
onconvert()
|
||
}
|
||
*/
|
||
params = JSON.parse(JSON.stringify(params))
|
||
if (Object.keys(params).length > 0) {
|
||
console.log('params', params)
|
||
if (params.d) {
|
||
xelem.value = params.d;
|
||
}
|
||
if (params.from) {
|
||
sel1elem.selectedIndex = params.from - 2;
|
||
}
|
||
if (params.to) {
|
||
sel2elem.selectedIndex = params.to - 2;
|
||
}
|
||
|
||
if (params.d && params.from && params.to) {
|
||
onconvert()
|
||
}
|
||
}
|
||
});
|
||
|
||
function GetURLParams() {
|
||
var url = window.location.href;
|
||
var regex = /[?&]([^=#]+)=([^&#]*)/g,
|
||
params = {},
|
||
match;
|
||
while (match = regex.exec(url)) {
|
||
params[match[1]] = match[2]
|
||
}
|
||
return params
|
||
}
|
||
|
||
function swap() {
|
||
var i1 = sel1elem.selectedIndex;
|
||
var i2 = sel2elem.selectedIndex;
|
||
sel1elem.selectedIndex = i2;
|
||
sel2elem.selectedIndex = i1;
|
||
xelem.value = yelem.value;
|
||
yelem.value = ""
|
||
}
|
||
|
||
function base2decimal(x, dec, b) {
|
||
var txt = "(" + x + ")<sub>" + b.toString() + "</sub> = ";
|
||
var d, e, minus = false;
|
||
var len = x.length;
|
||
if (x[0] == "-") {
|
||
txt += "-[";
|
||
x = x.substr(1);
|
||
len--;
|
||
minus = true
|
||
}
|
||
var idot = x.indexOf(".");
|
||
if (idot >= 0) {
|
||
x = x.substring(0, idot) + x.substring(idot + 1, len);
|
||
len--
|
||
} else idot = len;
|
||
for (var i = 0; i < len; i++) {
|
||
d = parseInt(x[i], b);
|
||
e = idot - i - 1;
|
||
e = e.toString();
|
||
txt += "(" + d + " × " + b + "<sup>" + e + "</sup>)";
|
||
if (i < len - 1) txt += " + "
|
||
}
|
||
if (minus) txt += "]";
|
||
txt += " = (" + dec + ")<sub>10</sub>";
|
||
document.getElementById("cal1txt").innerHTML = txt
|
||
}
|
||
|
||
function digits_after_period(x) {
|
||
f = x.toString();
|
||
i = f.indexOf('.');
|
||
len = f.length - i - 1;
|
||
return len
|
||
}
|
||
|
||
function decimal2base(dec, y, b) {
|
||
var row, txt = "";
|
||
document.getElementById("cal2tbl").getElementsByTagName('tbody')[0].innerHTML = "";
|
||
if (dec < 0) dec = -dec;
|
||
dec = dec.toString();
|
||
var id = dec.indexOf(".");
|
||
var nd = y.length - y.indexOf(".") - 1;
|
||
if (nd > 6) nd = 6;
|
||
if (id >= 0 && nd > 0) {
|
||
txt = "将目标基数乘以结果的小数位数(最高可达6位数):<br>";
|
||
txt += "floor(" + dec + "×" + b + "<sup>" + nd + "</sup>) = ";
|
||
dec = Math.floor(dec * Math.pow(b, nd));
|
||
txt += dec
|
||
}
|
||
document.getElementById("cal2dec").innerHTML = txt;
|
||
for (var n = dec, k = 0; n >= 1; k++) {
|
||
row = "<tr>";
|
||
row += "<td>" + n + "/" + b + "</td>";
|
||
row += "<td>" + Math.floor(n / b) + "</td>";
|
||
row += "<td>" + (n % b).toFixed() + "</td>";
|
||
row += "<td>" + k + "</td>";
|
||
row += "</tr>";
|
||
n = Math.floor(n / b);
|
||
var tableRef = document.getElementById('cal2tbl').getElementsByTagName('tbody')[0];
|
||
var newRow = tableRef.insertRow(k);
|
||
newRow.innerHTML = row
|
||
}
|
||
document.getElementById("cal2result").innerHTML = "= (" + y + ")<sub>" + b + "</sub>"
|
||
}
|
||
|
||
function onconvert() {
|
||
var x = xelem.value;
|
||
var b1 = sel1elem.selectedIndex + 2;
|
||
var b2 = sel2elem.selectedIndex + 2;
|
||
try {
|
||
var y = new BigNumber(x, b1);
|
||
xelem.style.background = "white"
|
||
} catch (err) {
|
||
xelem.style.background = "#fff0f0";
|
||
yelem.value = "";
|
||
return
|
||
}
|
||
var dec = y.toString(10);
|
||
y = y.toString(b2).toUpperCase();
|
||
yelem.value = y;
|
||
var yd = yelem.value.match(/[\dA-Z]/g);
|
||
var ylabel = "转换结果:";
|
||
if (yd != null) ylabel += " (" + yd.length + " 位)";
|
||
document.getElementById("ylabel").innerHTML = ylabel;
|
||
document.getElementById("b1txt").innerHTML = b1;
|
||
document.getElementById("b2txt").innerHTML = b2;
|
||
base2decimal(x, dec, b1);
|
||
decimal2base(dec, y, b2);
|
||
if (b1 == 10) cal1elem.style.display = "none";
|
||
else cal1elem.style.display = "block";
|
||
if (b2 == 10) cal2elem.style.display = "none";
|
||
else cal2elem.style.display = "block";
|
||
calgroup.style.display = "block"
|
||
}
|
||
|
||
function onrst() {
|
||
calgroup.style.display = "none"
|
||
} |