Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    sottrazione binaria in javascript

    Ciao a tutti ho un piccolo problema, devo realizzare uno script JavScript che permetta di eseguire la sottrazione tra due numeri binari di 4 cifre.
    qualcuno mi può dare una mano?

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Ciao e benvenuta/o
    Inizia a sviluppare lo script, poi dove ti blocchi vediamo d'aiutarti
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    eccolo, quando clicco il tasto "calcola" non mi fa la sottrazione
    <script language="javascript">
    function differenza()
    { var a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4,f;
    a1=document.form1.a1.value;
    a2=document.form1.a2.value;
    a3=document.form1.a3.value;
    a4=document.form1.a4.value;
    b1=document.form1.b1.value;
    b2=document.form1.b2.value;
    b3=document.form1.b3.value;
    b4=document.form1.b4.value;
    f= if (parseInt(document.form1.a4.value)==0)
    {
    if (parseInt(document.form1.b4.value)==1)
    {
    document.write("1");
    }
    else
    {
    document.write("25");
    }
    };


    document.form1.c4.value=f;
    }
    </script>
    </head>
    <body>
    <form name="form1">
    <input type="text" size=2 name="a1" value="0" >
    <input type="text" size=2 name="a2" value="0" >
    <input type="text" size=2 name="a3" value="0" >
    <input type="text" size=2 name="a4" value="0" >-


    <input type="text" size=2 name="b1" value="0" >
    <input type="text" size=2 name="b2" value="0" >
    <input type="text" size=2 name="b3" value="0" >
    <input type="text" size=2 name="b4" value="0" >=
    <hr>


    <input type="text" size=2 name="c1" value="0">
    <input type="text" size=2 name="c2" value="0">
    <input type="text" size=2 name="c3" value="0">
    <input type="text" size=2 name="c4" value="0">


    <input value="calcola" onclick="differenza()" type="button">
    </form>
    </body>

  4. #4
    vi prego aiutatemi D:

  5. #5
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Esempio</title>
    <script type="text/javascript">
    function binaryCheck (oToCheckField, oKeyEvent) {
    	return oKeyEvent.charCode === 0 || /[01]/.test(String.fromCharCode(oKeyEvent.charCode));
    }
    
    function subtraction (oForm) {
    
    	var
    		nFirst = parseInt(oForm.first1.value + oForm.first2.value + oForm.first3.value + oForm.first4.value, 2),
    		nSecond = parseInt(oForm.second1.value + oForm.second2.value + oForm.second3.value + oForm.second4.value, 2),
    		sResult = ("000" + (Math.abs(nFirst - nSecond)).toString(2)).substr(-4);
    
    	for (var nIdx = 1; nIdx < 5; oForm["result" + nIdx].value = sResult.charAt(nIdx++ - 1));
    
    }
    </script>
    </head>
    <body>
    <form name="form1">
    	<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="first1" value="0" >
    	<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="first2" value="0" >
    	<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="first3" value="0" >
    	<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="first4" value="0" >-
    	
    
    	<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="second1" value="0" >
    	<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="second2" value="0" >
    	<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="second3" value="0" >
    	<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="second4" value="0" >=
    	<hr>
    	
    
    	<input type="text" readonly size="1" name="result1" value="0">
    	<input type="text" readonly size="1" name="result2" value="0">
    	<input type="text" readonly size="1" name="result3" value="0">
    	<input type="text" readonly size="1" name="result4" value="0">
    	
    
    	<input value="calcola" onclick="subtraction(this.form);" type="button">
    </form>
    </body>
    </body>
    
    </html>
    Tutto ciò che fai con jQuery puoi farlo meglio e con la metà del codice in puro JavaScript.

  6. #6
    grazie, il codice funziona
    però ti posso chiedere la spiegazione di alcune delle funzioni che hai utilizzato? dato che non le conosco

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.