Coder Social home page Coder Social logo

uebung-037's Introduction

Uebung-037 -- FindAllBits

Lernziele:

  • Vertiefung Schleifen

Aufgabenstellung:

Das Programm „FindAllBits.cs“ soll:

  • für mindestens eine gegebene Zahl > 0 vom Typ uint eine Binärumwandlung durchführen.

    uint : ein vorzeichenloser Datentyp für ganze positive Zahlen zwischen 0 und UInt32.MaxValue

  • und das Ergebnis wie im folgenden Beispiel dargestellt ausgeben.

Beispielausgabe:

Beispiel

Rechenvorschrift für die Binärumwandlung:

  • Teile die Dezimalzahl solange durch 2, bis sie 0 ist.

  • Die Anzahl der möglichen Divisionen ergibt die Anzahl der Binärstellen.

    • Beispiel:
      Die Dezimalzahl 6 entspricht der Binärzahl 110: das sind 3 Binärstellen, weil* 6 insgesamt 3 Mal durch 2 teilbar ist.
      • 6 : 2 = 3, Rest 0
      • 3 : 2 = 1, Rest 1
      • 1 : 2 = 0, Rest 1
  • Bei jeder dieser Divisionen durch 2 ergibt der Rest der Division den binären Bitwert „0“ oder „1“ der gesuchten Binärzahl von rechts nach links gelesen.

    beginnend also rechts mit dem Bitwert für die niedrigste Binärstelle


SPOILER

Ausgabe:

ausgabe

Tip

[C#] Visual Studio :

namespace FindAllBits       // 
{                           //
 public class Program      // 
 {                         //
   static void Main()      //
   {
     /*------------------------- console_settings --------------------------*/
     const int cWidth = 53;                     //  console width
     const int cHeight = 46;                    //  & height
     Console.SetWindowSize(cWidth, cHeight);    //
     Console.OutputEncoding = Encoding.UTF8;    //  Unicode Symbols

     /*----------------------------- VARIABLES -----------------------------*/
     string userInput;      //  
     int index,
         formattedIndexed;  //  ✏
     double sumBitValue;
     UInt32 unsignedInput,
            savedInput;

     bool[] indexedBinary = new bool[32];
     double[] bitValue = new double[32];

     bool abort;

     /*-------------------------------- HEAD -------------------------------*/
     Console.Clear();
     Console.Write("\n                    Find All Bits                    " +
     /* cWidth: */ "\n=====================================================");

     /*---[in:]-------------------- PROMPT_USER ----------------------------*/
     do
     {
       Console.Write($"\n Geben Sie eine Zahl zwischen 0 und {UInt32.MaxValue} ein." +
                      "\n [0] - zum abbrechen ");
       /*---------------------------- GET_INPUT ----------------------------*/
       userInput = Console.ReadLine();
       UInt32.TryParse(userInput, out unsignedInput);

       abort = (unsignedInput == 0) ? true : false;
       savedInput = unsignedInput;
       /*---[calc:]---------------------------------------------------------*/
       if (abort == false)
       {
         index = 0;        // reset index 
         sumBitValue = 0;  // reset sum

         /* while input can be devided by 2 with a remainder remaining: */
         while (unsignedInput > 0 /* && (unsignedInput % 2 != 0) */ )
         {
           // bool array to safe the 0 or 1 at index:
           indexedBinary[index] = (unsignedInput % 2 != 0) ? true : false;
           formattedIndexed = (indexedBinary[index]) ? 1 : 0;
           // OUT:
           Console.Write($"\n {formattedIndexed} x 2^{index}");

           // if 1 at index:
           if (indexedBinary[index])
           {  // calc: 2 to the power of index → OUT: value
             bitValue[index] = Math.Pow(2, index);
             Console.Write($" = {bitValue[index]}");
           }
           else
           {  // value at index = 0 → OUT: empty line
             bitValue[index] = 0;
           }

           // calc: sum:
           sumBitValue = sumBitValue + bitValue[index];
           // calc: input for next iteration:
           unsignedInput = unsignedInput / 2;
           index++;  // iterate index by 1
         }

         /*---[out:]--------------------- SOLUTION -----------------------------*/
         Console.Write("\n=====================================================" +
                       $"\n Dezimalzahl: {(savedInput.ToString().PadLeft(cWidth - 22))}" +
                       $"\n Summe aller Bitwerte: {(sumBitValue.ToString().PadLeft(cWidth - 31))}" +
                       "\n-----------------------------------------------------");
         // OUT BONUS
         Console.Write("\n Binär: ");
         index = 31;
         do
         {
           if (!indexedBinary[index])
           {
             index--;
           }
           else
           {
             do
             {
               if ((index + 1) % 4 == 0)
                 Console.Write(" ");
               if (indexedBinary[index])
               {
                 Console.Write("1");
               }
               else
                 Console.Write("0");
               index--;
             } while (index >= 0);
           }
         } while (index >= 0);

         Console.Write("\n=====================================================");
       }
       else
         Console.Write("\n Abbrechen gewählt.");
     } while (!abort);
     /*-------------------------------- END --------------------------------*/
     Console.Write("\n Zum beenden Eingabetaste drücken..");
     Console.ReadLine();    //  wait for [enter]
     Console.Clear();       //
   }
 }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.