1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
int taillenb1;
//demande à l'utilisateur d'entrer 2 nombres binaire
System.out.println("Bienvenue ce programme va calculer l'addition de 2 nombres binaire\n");
System.out.print("Entrez le 1er nombre binaire : ");
String nb1 = keyboard.nextLine();
System.out.print("\nEntrez le 2ème nombre binaire : ");
String nb2 = keyboard.nextLine();
taillenb1=nb1.length()-1;
int [] resultat=new int [taillenb1];
int [] report = new int [taillenb1];
int [] sol = new int [taillenb1];
//partie test du programme
report[taillenb1-1]=0;
for (int i=nb1.length()-1;i>=0;i--){
if ((nb1.charAt(i)+ nb2.charAt(i))==2){
report[i-1]=1;
}else{
report[i-1]=0;
}
resultat[i]=(nb1.charAt(i) + nb2.charAt(i))%2;
if ((resultat[i] + report[i])==2){
report[i-1]=1;
sol[i]=0;
}else if (resultat[i]+report[i]==1){
report[i-1]=0;
sol[i]=1;
}else{
report[i-1]=0;
sol[i]=0;
}
}
System.out.println("resultat");
for (int i=0;i<nb1.length();i++){
System.out.print(sol[i]);
}
}} |
Partager