Mathematics‎ > ‎

Real to Binary

Kao što je i obećano na strani gde je obrađena tema binarni broj u decimalni broj , na ovoj strani imate rešenje problema realni broj  u binarni broj.
 
Što se tiče algoritma pokušajte da ga napišete sami na osnovu programa. ukoliko vas interesuje malo opširnije tema o brojevnim sistemima pogledajte sadržaj na sledećoj adresi

http://bs.wikipedia.org/wiki/Brojevni_sistem

Dole je priložen kod programa , koji i dalje nije apsolutno savršenstvo pa je u skladu s time u pripremi  program za konvertovanje broja unetog s tastature iz bilo kog brojevnog sistema u bilo koji drugi brojevni sistem.

 

 

 

 

U programu se koristi zarez umesto tačke , tako da ukoliko su kod vas lokalna podešavanja obrnuta ,

moraćete da korigujete program kako bi tačno radio.

Programski kod :

/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 09.02.2012
 * Time: 17:02
 * 
 * Simple console program for conversion of
 *      Real  to Binary numbers
 * check out what is the local setting for decimal separator
 * if it is "," everything is ok, if it is "." you need to correct program
 * change in code "," into "." and ',' into '.'
 * 
 */
using System;

namespace Real_to_Binary
{
    class Program
    {
        public static void Main(string[] args)
        {
            //
            // declaration of variables
            //
            string [] DecimalFloat =new string [2];
            string FloatPart = "";
            string DecimalPart = "";
            
            string Real = "";
            string Binary = "";
            
            string Letter = "";
            string Sign = "";
            
            double Cipher = 0;
            double Integral = 0;
            double RealNumber = 0;
            double DivisionResult = 0;
            double MultiplicationResult  = 0;
            double DecimalNumber = 0;
            double FloatNumber = 0;
            
            bool IsReal = false;

            
            // 
            // not very desirable but necessary
            // begining of program label
            //
    begining :
                
            //
            // Hello message to user
            //
            Console.Title = "Program Real to Binary ";
            Console.SetWindowSize(80,40);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Clear();
            Console.WriteLine("****************************************************************************");
            Console.WriteLine("                          Real to Binary");
            Console.WriteLine("****************************************************************************");
            
            // 
            // Get number from Console
            //
            Console.WriteLine();
              Console.Write("Enter Real number (exmpl: 2,375 = 10,011) : ");
             Real = "";
            Real = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine("****************************************************************************");
            Console.WriteLine();
            
            // 
            // Check if it is Real number
            // 
            IsReal = false;
            RealNumber = 0;
            IsReal = double.TryParse(Real,out RealNumber);
            if (IsReal == true)
            {
                //
                // clear sign plus or minus
                //
                if (Real.Contains("-"))
                {
                    Real = Real.TrimStart('-');
                    Sign = "- ";
                    RealNumber = -1 * RealNumber;
                }
                else if (!Real.Contains("-") && !Real.Contains("+"))
                {
                    Sign = "";
                }
                else  if (Real.Contains("+"))
                {
                    Real = Real.TrimStart('+');
                    Sign = "+ ";
                    RealNumber = double.Parse(Real.ToString());
                }
                
                //
                // Clears number from 0 
                // if it is number with float point with all possibilites
                //
                // exmpl. 0001101,01010000 = 1101,0101
                //
                if ( Real.Contains(",")) 
                {
                    DecimalFloat = Real.Split(',');
                    DecimalPart = DecimalFloat[0];
                    FloatPart = DecimalFloat[1];
                    
                    DecimalPart = DecimalPart.TrimStart('0');
                    if (DecimalPart =="")
                    {
                        DecimalPart = "0";
                    }
                    
                    FloatPart = FloatPart.TrimEnd('0');
                    if (FloatPart =="")
                    {
                        FloatPart = "0";
                    }
                    
                    if (DecimalPart =="0" && FloatPart =="0")
                    {
                        Real = "0";
                    }
                    if (DecimalPart !="0" && FloatPart =="0")
                    {
                        Real = DecimalPart;
                    }
                    if (DecimalPart =="0" && FloatPart !="0")
                    {
                        Real = DecimalPart + "," + FloatPart;
                    }
                    if (DecimalPart !="0" && FloatPart !="0")
                    {
                        Real = DecimalPart + "," + FloatPart;
                    }
                }
                
                //
                // if it is decimal number
                //
                else
                {
                    Real = Real.TrimStart('0');
                    if (Real =="")
                    {
                        Real = "0";
                    }
                }
            }
            
            else if (IsReal == false)
            {
                Console.WriteLine("Error , you didn't entered a number at all..");
                Console.WriteLine();
                
                //
                // goto jump is not very desirable but necessary
                //
                goto end;
            }
            
            //
            // Resolving conversion from Real to Decimal
            // If it is Float number, split number to Decimal and Float part
            // exmpl. 2,375(10) DecimalPart = 2 and FloatPart = 375
            //
            if (Real.Contains(","))
            {
                
                DecimalFloat = Real.Split(',');
                
                //    
                // Decimal part first
                //
                DecimalPart = DecimalFloat[0];
                DecimalNumber = double.Parse(DecimalPart);
                
                Binary = "";
                Integral = 0;
                DivisionResult = 0;
                MultiplicationResult = 0;
                Cipher = 0;
                while (DecimalNumber != 0 )
                {
                    DivisionResult = DecimalNumber/ 2;
                    Integral = Math.Truncate(DivisionResult);
                    
                    MultiplicationResult =  2 * Integral ;
                    Cipher = DecimalNumber - MultiplicationResult;
                    DecimalNumber = Integral;
                    
                    Binary = Binary.Insert(0,Cipher.ToString());
                }
                
                //
                // if DecimalPart number where zero
                //
                if (Binary == "")
                {
                    Binary = "0";
                }
                
                DecimalPart = Binary;
                
                //
                // Float part second
                //
                FloatPart = "0," + DecimalFloat[1];
                FloatNumber = double.Parse(FloatPart);
                  Binary = "";
                Integral = 0;
                MultiplicationResult = 0;
                while (FloatNumber != 0)
                {
                    MultiplicationResult = FloatNumber * 2;
                    Integral = Math.Truncate(MultiplicationResult);
                    FloatNumber =  MultiplicationResult - Integral ;
                    Binary = Binary + Integral.ToString();
                }
                FloatPart = Binary;
                
                Binary = "";
                Binary = DecimalPart + "," + FloatPart;
                //
                // goto jump is not very desirable but necessary
                // 
                //
                goto write;
            }
            
            //
            // Resolving conversion from Decimal to Binary
            // If it is Decimal number
            // exmpl. 1001
            //

            else 
            {
                DecimalNumber = RealNumber;
                
                Binary = "";
                Integral = 0;
                DivisionResult = 0;
                MultiplicationResult = 0;
                Cipher = 0;
                while (DecimalNumber != 0 )
                {
                    DivisionResult = DecimalNumber/ 2;
                    Integral = Math.Truncate(DivisionResult);
                    MultiplicationResult =  2 * Integral ;
                    Cipher = DecimalNumber - MultiplicationResult;
                    DecimalNumber = Integral;
                    Binary = Binary.Insert(0,Cipher.ToString());
                }
                //
                // if entered number where zero
                //
                if (Binary == "")
                {
                    Binary = "0";
                }

            }
            
    write:

            //
            // Write result
            //
            if (RealNumber == 0)
            {
                Sign ="";
            }
            Console.WriteLine();
            Console.WriteLine("Real number :  " + Sign + Real);
            Console.WriteLine();
            Console.WriteLine("Binary number   :  " + Sign + Binary);
            Console.WriteLine();
            
            // 
            // not very desirable but necessary
            // end of program label
            //
    end:
            Console.WriteLine("****************************************************************************");
            Console.WriteLine();
            Console.Write("Do you want to run program again y / n : ");
            Letter = Console.ReadLine();
            if ( Letter == "Y" || Letter == "y")
            {
                goto begining;
            }
    
        }
    }
}

U prilogu se nalazi Real to Binary.rar arhiva s kompletnim rešenjem pisanim za IDE SharpDevelop C,
koje možete preuzeti i u okviru koga se nalazi i Release folder sa exe fajlom programa.Neophodan netframework 4.0 ili noviji.
 
Sve najbolje,
Autor
 
 

 
Č
ċ
ď
Perić Željko,
12.02.2012. 06.50
Comments