Niestety nie bardzo rozumiem zadanie, które powinienem wykonać (może dlatego, że javy uczę się od października).
Chodzi o to, że dla mnie najprościej byłoby dodać dwie liczby binarne zamieniając je na dziesiętne i potem sumę znowu zamieniając na binarną.
Ale w zadaniu chyba chodzi o coś innego, tzn. żeby tak na piechotę dodawać kolejne cyfry liczb binarnych do siebie.
package BinDecimal;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class BinDecimal {
private static final int base = 2;
private static final String pattern = "01";
public static String convertTo(int n) {
String result = "";
//n is equal to 0, do not process, return "0"
if (n == 0)
return "0";
//process until n > 0
while (n > 0) {
result = pattern.charAt(n % base) + result;
n /= base;
}
return result;
}
public static int convertFrom(String n) {
int decimal = 0;
return Integer.parseInt(n, 2);
}
public static void main(String[] args) {
int choice;
Scanner input = new Scanner(System.in);
do {
try {
System.out.println("What do you want to do?");
System.out.println("1. Convert binary to decimal");
System.out.println("2. Convert decimal to binary");
System.out.println("3. Sum of two binary");
System.out.println("4. Multiplication of two binary");
choice = input.nextInt();
break;
} catch (InputMismatchException ex) {
System.out.println("Wrong format! Try again!");
input.next();
}
} while (true);
if (choice == 1) {
System.out.println("Enter binary number: ");
int n = input.nextInt();
String number = String.valueOf(n);
System.out.println(number + " w systemie dwojkowym to " + convertFrom(number) + " w systemie dziesietnym");
} else if (choice == 2) {
System.out.println("Enter decimal number: ");
int number = input.nextInt();
System.out.println(number + " w systemie dwojkowym = " + convertTo(number));
} else if (choice == 3) {
ArrayList<Character> list1 = new ArrayList<>();
System.out.println("Enter binary number: ");
int a = input.nextInt();
String number1 = String.valueOf(a);
for (int i = 0; i < number1.length(); i++) {
list1.add(number1.charAt(i));
}
for (Character element : list1) {
System.out.print(element + ", ");
}
System.out.println("Enter binary number: ");
ArrayList<Character> list2 = new ArrayList<>();
int b = input.nextInt();
String number2 = String.valueOf(b);
for (int i = 0; i < number2.length(); i++) {
list2.add(number2.charAt(i));
}
for (Character element : list2) {
System.out.print(element + ", ");
int carry = 0;
Character[] result = new Character[50];
for (int i = 0; i <= list1.size(); i++) {
if (list1.get(i) + list2.get(i) + carry == 0) {
result[i] = 0;
carry = 0;
}
if (list1.get(i) + list2.get(i) + carry == 1) {
result[i] = 0;
carry = 0;
}
if (list1.get(i) + list2.get(i) + carry == 2) {
result[i] = 0;
carry = 1;
}
if (list1.get(i) + list2.get(i) + carry > 2) {
result[i] = 1;
carry = 1;
}
}
}
System.out.println(number1 + " + " + number2 + "=" + ());
} else if (choice == 4) {
} else
System.out.println("Bad choice!");
}
}
No to tu nie mam jeszcze doświadczenia, bo lista z cyfr jednej i drugiej liczby może być różnej długości, a lista z sumy jeszcze innej.
Czy ktoś z Państwa miałby jakiś pomysł na to, żebym z zadaniem dobrnął do celu?
Jeśli by się to udało, to może potem dałbym już radę z mnożeniem...
Oto, co zrobiłem do tej pory:
Z góry bardzo dziękuję za każdą chęć pomocy.