Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Binaeraddition {
  6.  
  7.     public static String addition(String zahl1, String zahl2) {
  8.         String ergebnis = "";
  9.  
  10.         // mit der Methode prüfeZahl wird überprüft ob die Zahl
  11.         // wirklich nur 1 und 0 enthält
  12.         // Ansonsten wird eine Fehlermeldung ausgegeben(weiter unten)
  13.         if (prüfeZahl(zahl1)) {
  14.             if (prüfeZahl(zahl2)) {
  15.  
  16.                 int laengeZahl1 = zahl1.length();
  17.                 int laengeZahl2 = zahl2.length();
  18.                 int überzahl = 0;
  19.                 String hilfsString = "0";
  20.  
  21.                 // Es wird überprüft, ob eine Zahl länger ist als die andere
  22.                 // Die kleinere Zahl wird mit 0en am Anfang aufgefüllt
  23.                 if (laengeZahl1 < laengeZahl2) {
  24.                     for (int i = 0; i < (laengeZahl2 - laengeZahl1); i++) {
  25.                         zahl1 = hilfsString + zahl1;
  26.                     }
  27.                 } else if (laengeZahl2 < laengeZahl1) {
  28.                     for (int i = 0; i < (laengeZahl1 - laengeZahl2); i++) {
  29.                         zahl2 = hilfsString + zahl2;
  30.                     }
  31.                 }
  32.  
  33.                 // Der eigentliche Rechenprozess
  34.                 for (int i = (laengeZahl1 - 1); i >= 0; i--) {
  35.                     int c1 = (zahl1.charAt(i)) - 48;
  36.                     int c2 = (zahl2.charAt(i)) - 48;
  37.                     int c3 = c1 + c2 + überzahl;
  38.                     überzahl = 0;
  39.                     if (c3 == 2) {
  40.                         c3 = 0;
  41.                         überzahl = 1;
  42.                     }
  43.                     if (c3 == 3) {
  44.                         c3 = 1;
  45.                         überzahl = 1;
  46.                     }
  47.                     ergebnis = c3 + ergebnis;
  48.  
  49.                 }
  50.  
  51.             } else {
  52.                 ergebnis = "Fehler bei Eingabe";
  53.             }
  54.         } else {
  55.             ergebnis = "Fehler bei Eingabe";
  56.         }
  57.  
  58.         return ergebnis;
  59.     }
  60.  
  61.     private static boolean prüfeZahl(String zahl) {
  62.         boolean b = true;
  63.         char c;
  64.         for (int i = 0; i < zahl.length(); i++) {
  65.             c = zahl.charAt(i);
  66.             if (c == '1' || c == '0') {
  67.             } else {
  68.                 b = false;
  69.             }
  70.         }
  71.         return b;
  72.     }
  73.  
  74.     private static String formatiereZahl(String zahl, int stellen) {
  75.         String ergebnis = zahl;
  76.         for (int i = 0; i < stellen - zahl.length(); i++) {
  77.             ergebnis = " " + ergebnis;
  78.         }
  79.         return ergebnis;
  80.     }
  81.  
  82.     public static void main(String[] args) {
  83.         String zahl1;
  84.         String zahl2;
  85.         String ergebnis;
  86.         int maxLaenge;
  87.  
  88.         // Eingabe
  89.         char choice = 'R';
  90.         while (choice != 'Q') {
  91.             System.out.print("Erste Zahl eingeben: ");
  92.             zahl1 = stringLesen();
  93.             System.out.print("Zweite Zahl eingeben: ");
  94.             zahl2 = stringLesen();
  95.             ergebnis = Binaeraddition.addition(zahl1, zahl2);
  96.             maxLaenge = zahl1.length();
  97.             if (zahl2.length() > maxLaenge)
  98.                 maxLaenge = zahl2.length();
  99.             if (ergebnis.length() > maxLaenge)
  100.                 maxLaenge = ergebnis.length();
  101.             System.out.println(Binaeraddition.formatiereZahl(zahl1, maxLaenge));
  102.             System.out.println(Binaeraddition.formatiereZahl(zahl2, maxLaenge));
  103.             System.out.println(Binaeraddition.formatiereZahl(ergebnis,
  104.                     maxLaenge));
  105.             System.out.print("Neue Rechnung starten (R) oder beenden (Q)? ");
  106.             choice = charLesen();
  107.         }
  108.  
  109.     }
  110.  
  111.     private static String stringLesen() {
  112.         BufferedReader reader = new BufferedReader(new InputStreamReader(
  113.                 System.in));
  114.         try {
  115.             return reader.readLine();
  116.         } catch (IOException e) {
  117.             System.out.println("Fehler beim Lesen von Tastatur");
  118.             System.exit(1);
  119.         }
  120.         return "";
  121.     }
  122.  
  123.     private static char charLesen() {
  124.         BufferedReader reader = new BufferedReader(new InputStreamReader(
  125.                 System.in));
  126.         try {
  127.             return (char) reader.read();
  128.         } catch (IOException e) {
  129.             System.out.println("Fehler beim Lesen von Tastatur");
  130.             System.exit(1);
  131.         }
  132.         return ' ';
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement