![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
14/11/2005, 12:08
|
| | Fecha de Ingreso: enero-2005 Ubicación: Sitges
Mensajes: 65
Antigüedad: 20 años Puntos: 0 | |
en teoría lo pongo bien Código PHP: <HTML><HEAD><TITLE>Conversor decimal/binario</TITLE> <META NAME="author" VALUE="[email protected]"> <SCRIPT LANGUAGE="JavaScript"> <!-- Hide scripts from old browsers
function reload() { location.reload(); } function Decimal_to_binary(x) { answer=new Object(); x2=x; log2=0; while(x2>=2){ x2=x2/2; log2=log2+1; }
for(l2=log2; l2>=0; l2--){ power=Math.pow(2,l2); if (x>=power) { answer[l2]="1"; x=x-power; } else answer[l2]="0"; } for (i=log2; i>=0; i--){ document.forms[0].elements[1].value+=(answer[i]);
} } function Binary_to_decimal(x) {
y=parseInt(x,2); if (isNaN(y)) alert(x + " no es un nº binario!"); else document.forms[1].elements[1].value=y; }
function explaindec(x) {
var ex = open("","explanation","width=300,height=150,scrollbars=yes");
ex.document.write("El nº <FONT COLOR=BLUE>" + x +"</FONT>"); answer=new Object(); x2=x; log2=0; while(x2>=2){ x2=x2/2; log2=log2+1; } ex.document.write(" Se puede expresar: <BR><FONT COLOR=BLUE>"); for(l2=log2; l2>=0; l2--){ power=Math.pow(2,l2); if (x>=power) { answer[l2]="1"; x=x-power; if (l2<log2) ex.document.write(" " + "+" + " "); ex.document.write(power); } else answer[l2]="0"; } ex.document.write("</FONT><BR>La solución es: "); ex.document.write("<FONT COLOR=RED>"); for (i=log2; i>=0; i--){ ex.document.write(answer[i]); }
ex.document.write("</FONT> "); ex.document.write("<P>"); ex.document.write("<FORM>"); ex.document.write("<INPUT TYPE=BUTTON VALUE='Continuar' onClick='window.close()'>"); ex.document.write("</FORM>"); }
function explainbin(x) {
y=parseInt(x,2); if (isNaN(y)){ alert(x + " no es un nº binario"); return; }
var ex = open("","explanation","width=300,height=150,scrollbars=yes"); ex.document.write("El nº <FONT COLOR=BLUE>" + x +"</FONT>"); ex.document.write(" representa: <BR><FONT COLOR=BLUE>"); hipow=x.length-1; for(l=0;l<x.length;l++){ digit=x.substring(l,l+1); if (digit=='1'){ power=Math.pow(2,hipow-l); if (l>0) ex.document.write(" " + "+" + " "); ex.document.write(power); } } ex.document.write("</FONT><BR>la solución es: "); ex.document.write("<FONT COLOR=RED>"); ex.document.write(y); ex.document.write("</FONT> "); ex.document.write("<P>"); ex.document.write("<FORM>"); ex.document.write("<INPUT TYPE=BUTTON VALUE='Continuar' onClick='window.close()'>"); ex.document.write("</FORM>"); }
function answer(correct,which) { if (document.forms[0].elements[which].value == correct) { alert("Correcto!"); } else { alert("Prueba otra vez."); } } function give(correct,which) {
document.forms[0].elements[which].value = correct; } //End hide --> </SCRIPT>
</HEAD> <BODY BACKGROUND="images/data02.jpg"> <H3> Javascript, calculadora binario/decimal </H3> <HR> <FONT SIZE=2> <P> </FONT>
<FORM NAME="dectobin"> Nº Decimal a convertir: <INPUT name="dec" size=7 MAXLENGTH=7 > <BR>
Representación Binaria: <INPUT name="bin" size=25 MAXLENGTH=25 input type="bin" readonly="readonly" /> <BR> <INPUT TYPE=button VALUE="Convertir" onClick=Decimal_to_binary(document.forms[0].elements[0].value)> <INPUT TYPE=button VALUE="¿Porque?" onClick=explaindec(document.forms[0].elements[0].value)>
<BR><BR> <INPUT TYPE=Reset VALUE="Otra Vez!" onClick=reload()>
</FORM>
<FORM NAME="bintodec">
Nº Binario a convertir: <INPUT name="dec" size=24 MAXLENGTH=24 > <BR>
Represenación Decimal: <INPUT name="bin" size=7 MAXLENGTH=7 input type="bin" readonly="readonly" /> <BR> <INPUT TYPE=button VALUE="Convertir" onClick=Binary_to_decimal(document.forms[1].elements[0].value)> <INPUT TYPE=button VALUE="¿Porqué?" onClick=explainbin(document.forms[1].elements[0].value)> <BR><BR>
<INPUT TYPE=Reset VALUE="Otra Vez!">
</FORM> <p><a href="mailto:[email protected]">Dessign</a></p>
|