2017-02-18 52 views
0

我爲java中的二進制加法編寫了一個程序。但結果有時並不正確。
例如,如果我添加1110 + 111。結果應該是10101.
但是我的程序拋出10001.
也許你們中的一個人發現了錯誤。java中的二進制加法

import java.util.Scanner; 

public class BinaryAdder { 
public static String add(String binary1, String binary2) { 
    int a = binary1.length()-1; 
    int b = binary2.length()-1; 
    int sum = 0; 
    int carry = 0; 


    StringBuffer sb = new StringBuffer(); 
    while (a >= 0 || b >= 0) { 
     int help1 = 0; 
     int help2 = 0; 
     if(a >=0){ 
     help1 = binary1.charAt(a) == '0' ? 0 : 1; 
     a--; 
    } if(b >=0){ 
     help2 = binary2.charAt(b) == '0' ? 0 : 1; 
     b--; 
    } 
     sum = help1 +help2 +carry; 
     if(sum >=2){ 
      sb.append("0"); 
      carry = 1; 
     } else { 
      sb.append(String.valueOf(sum)); 
      carry = 0; 
     } 

    } 
    if(carry == 1){ 
     sb.append("1"); 
    } 

    sb.reverse(); 
    String s = sb.toString(); 
    s = s.replaceFirst("^0*", ""); 

    return s; 
} 


public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.print("First: "); 
    String input1 = scan.next("(0|1)*"); 
    System.out.print("Second: "); 
    String input2 = scan.next("(0|1)*"); 
    scan.close(); 
    System.out.println("Result: " + add(input1, input2)); 
} 

}

回答

0

這個功能要簡單得多:

public static String binaryAdd(String binary1,String binary2){ 
    return Long.toBinaryString(Long.parseLong(binary1,2)+Long.parseLong(binary2,2)); 
} 

你可以改變Long.parseLongInteger.parseInt如果你不希望非常大的數字,你也可以用parseUnsigned(Long/Int)取代parse(Long/Int)因爲你不要指望你的字符串有一個負號嗎?

+0

沒有教授特別要求非常大的數字。但我不期望減號 –

+0

@EC然後只需使用'Long.parseUnsignedLong',如果數字比'Long'更大(比'9223372036854775807'更大) – niceman

0

你是不考慮的情況下,當

help1 + help2 = 3 

所以你的方法String add(String binary1, String binary2)應該是這樣的:

public static String add(String binary1, String binary2) { 
    int a = binary1.length()-1; 
    int b = binary2.length()-1; 
    int sum = 0; 
    int carry = 0; 


    StringBuffer sb = new StringBuffer(); 
    while (a >= 0 || b >= 0) { 
     int help1 = 0; 
     int help2 = 0; 
     if(a >=0){ 
     help1 = binary1.charAt(a) == '0' ? 0 : 1; 
     a--; 
    } if(b >=0){ 
     help2 = binary2.charAt(b) == '0' ? 0 : 1; 
     b--; 
    } 
     sum = help1 +help2 +carry; 
     if (sum == 3){ 
      sb.append("1"); 
      carry = 1; 
     } 
     else if(sum ==2){ 
      sb.append("0"); 
      carry = 1; 
     } else { 
      sb.append(String.valueOf(sum)); 
      carry = 0; 
     } 

    } 
    if(carry == 1){ 
     sb.append("1"); 
    } 

    sb.reverse(); 
    String s = sb.toString(); 
    s = s.replaceFirst("^0*", ""); 

    return s; 
} 

我希望這能幫助你!

+0

非常感謝!我真的忘了那個條件 –

+0

不客氣!我希望你能接受我的回答 – delca85

0
sum = help1 +help2 +carry; 
     if(sum >=2){ 
      sb.append("0"); 
      carry = 1; 
     } else { 
      sb.append(String.valueOf(sum)); 
      carry = 0; 
     } 

如果總和爲2,則附加 「0」 和攜帶= 1

什麼當總和是3時,附加 「1」 和攜帶= 1

將永遠不會被4或更大

0

知道我是一個有點晚,但我只是做了類似的任務,所以在我的位置上的人,這裏是我如何解決它...

import java.util.Scanner; 

public class Binary_Aids { 
public static void main(String args[]) { 
    System.out.println("Enter the value you want to be converted"); 
    Scanner inp = new Scanner(System.in); 
    int num = inp.nextInt(); 

    String result = ""; 

    while(num > 0) { 
     result = result + Math.floorMod(num, 2); 
     num = Math.round(num/2); 

    } 

    String flippedresult = ""; 
    for(int i = 0; i < result.length(); i++) { 
     flippedresult = result.charAt(i) + flippedresult; 
    } 

    System.out.println(flippedresult); 


} 
} 

這需要一個輸入並轉換爲二進制。一旦在這裏,我用這個程序來添加數字,然後轉換回...

import java.util.Scanner; 

public class Binary_Aids { 
public static void main(String args[]) { 
    Scanner inp = new Scanner(System.in); 

    String decimalToBinaryString = new String(); 

    System.out.println("First decimal number to be added"); 
    int num1 = inp.nextInt(); 
    String binary1 = decimalToBinaryString(num1); 




    System.out.println("Input decimal number 2"); 
    int num2 = inp.nextInt(); 
    String binary2 = decimalToBinaryString(num2); 

    int patternlength = Math.max[binary1.length[], binary2.length[]]; 

    while(binary1.length() < patternlength) { 
     binary1 = "0" + binary2; 
    } 

    System.out.println(binary1); 
    System.out.println(binary2); 

    int carry = 0; 
    int frequency_of_one; 
    String result = ""; 
    for(int i = patternlength -i; i >= 0; i--) { 
     frequency_of_one = carry; 
     if(binary1.charAt(i) == '1') { 
      frequency_of_one++; 
     } 
     if(binary2.charAt(i) == '1') { 
      frequency_of_one++; 

     } 

     switch(frequency_of_one) { 
     case 0 ; 
     carry = 0; 
     result = "1" + result; 
     break; 

     case 1 ; 
     carry = 0; 
     result = "1" + result; 
     break; 

     case 2; 
     carry = 1; 
     result = "0" + result; 
     breake; 

     case 3; 
     carry = 1; 
     result = "1" + result; 
     breake; 
     } 
    } 

    if(carry == 1) { 
     result = "1" + result; 

    } 
    System.out.println(result); 

    } 



public static String decimalToBinaryString(int decimal1) { 
String result = ""; 

while(decimal1 > 0) { 
    result = result + Math.floorMod(decimal1, 2); 
    decimal = Math.round(decimal1/2); 
} 

String flipresult = ""; 
for(int i = 0; i < result.length[]; i++) { 
    flipresult = result.charAt(i) + flippedresult; 
} 
return flippedresult; 
} 






}