使用 https://beautifier.io/ 美化js代码
This commit is contained in:
parent
cd9dcb4452
commit
bfc500385f
@ -951,4 +951,142 @@
|
|||||||
var n, o
|
var n, o
|
||||||
});
|
});
|
||||||
|
|
||||||
var clipboard=new ClipboardJS('.btn-copy');var xelem=document.getElementById("d");var sel1elem=document.getElementById("form");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&¶ms.d!=""){xelem.value=params.d;sel1elem.selectedIndex=params.form-2;sel2elem.selectedIndex=params.to-2;onconvert()}});function GetURLParams(){var url=window.location.href;var regex=/[?&]([^=#]+)=([^&#]*)/g,params={},match;while(match=regex.exec(url)){params[match[1]]=match[2]}if(url.indexOf('8jz')>-1){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+" \u00D7 "+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="\u5c06\u76ee\u6807\u57fa\u6570\u4e58\u4ee5\u7ed3\u679c\u7684\u5c0f\u6570\u4f4d\u6570\u0028\u6700\u9ad8\u53ef\u8fbe\u0036\u4f4d\u6570\u0029\u003a<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="\u8f6c\u6362\u7ed3\u679c\uff1a";if(yd!=null)ylabel+=" ("+yd.length+((yd.length==1)?" \u4f4d)":" \u4f4d)");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"}
|
|
||||||
|
var clipboard = new ClipboardJS('.btn-copy');
|
||||||
|
var xelem = document.getElementById("d");
|
||||||
|
var sel1elem = document.getElementById("form");
|
||||||
|
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.form - 2;
|
||||||
|
sel2elem.selectedIndex = params.to - 2;
|
||||||
|
onconvert()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function GetURLParams() {
|
||||||
|
var url = window.location.href;
|
||||||
|
var regex = /[?&]([^=#]+)=([^&#]*)/g,
|
||||||
|
params = {},
|
||||||
|
match;
|
||||||
|
while (match = regex.exec(url)) {
|
||||||
|
params[match[1]] = match[2]
|
||||||
|
}
|
||||||
|
if (url.indexOf('8jz') > -1) {
|
||||||
|
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 + " \u00D7 " + 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 = "\u5c06\u76ee\u6807\u57fa\u6570\u4e58\u4ee5\u7ed3\u679c\u7684\u5c0f\u6570\u4f4d\u6570\u0028\u6700\u9ad8\u53ef\u8fbe\u0036\u4f4d\u6570\u0029\u003a<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 = "\u8f6c\u6362\u7ed3\u679c\uff1a";
|
||||||
|
if (yd != null) ylabel += " (" + yd.length + ((yd.length == 1) ? " \u4f4d)" : " \u4f4d)");
|
||||||
|
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"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user