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 binarni broj s pokretnim zarezom u realni broj.Takođe, program je modifikovan tako da sada briše sve nepotrebne nule , a i prepoznaje negativan broj.(vidi sliku) Š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 okviru priloga imate i dokument sa više tehničke škole , koji je napisao mr. Veličković Zoran , a predstavlja uvod u brojevne sisteme i odličan je za razumevanje ove teme, i onome ko nije preterano dobar u matematici. Upotrebljen je i za izradu ovog programa.
Programski kod (revizija izvršena 09/02/2012):
U prilogu se nalazi Binary to Real.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 .Net framework 4.0 ili noviji.
/*
* Created by SharpDevelop.
* User: Peric Zeljko
* Date: 7.10.2011
* Time: 8:51
*
*
* Simple console program for conversion of
* Binary numbers to Real numbers
*
*/
using System;
namespace Binary_to_Decimal
{
class Program
{
public static void Main(string[] args)
{
//
// declaration of variables
//
string [] DecimalFloat = new string[2];
string Binary = "";
bool RealRelevance = false;
bool BinaryRelevance = false;
int Index = 0;
int Lenght = 0;
int Exponent = 0;
int Cipher = 0;
double BaseExponent = 0;
double RealNumber = 0;
double D = 0;
double F = 0;
string Negative = "";
string DecimalPart = "";
string FloatPart = "";
string Letter = "";
//
// not very desirable but necessary
// begining of program label
//
begining :
//
// Hello message to user
//
Console.Title = "Program Binary to Real ";
Console.SetWindowSize(80,40);
Console.ForegroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("****************************************************************************");
Console.WriteLine(" Binary to Real");
Console.WriteLine("****************************************************************************");
//
// Get number from Console
//
Console.WriteLine();
Console.Write("Enter binary number (exmpl: 1001) : ");
Binary = "";
Binary = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("****************************************************************************");
//
// Check if it is Real number
//
RealRelevance = false;
RealNumber = 0;
RealRelevance = double.TryParse(Binary,out RealNumber);
if (RealRelevance == true)
{
//
// clear sign plus or minus
//
if (Binary.Contains("-"))
{
Binary = Binary.TrimStart('-');
Negative = "- ";
RealNumber = -1 * RealNumber;
}
else if (!Binary.Contains("-") && !Binary.Contains("+"))
{
Negative = "";
}
else if (Binary.Contains("+"))
{
Binary = Binary.TrimStart('+');
Negative = "+ ";
RealNumber = double.Parse(Binary.ToString());
}
//
// Clears number from 0 if it is number with float point
// with all possibilites
//
// exmpl. 0001101,01010000 = 1101,0101
//
if ( Binary.Contains(","))
{
DecimalFloat = Binary.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")
{
Binary = "0";
}
if (DecimalPart !="0" && FloatPart =="0")
{
Binary = DecimalPart;
}
if (DecimalPart =="0" && FloatPart !="0")
{
Binary = DecimalPart + "," + FloatPart;
}
if (DecimalPart !="0" && FloatPart !="0")
{
Binary = DecimalPart + "," + FloatPart;
}
}
//
// if it is decimal number
//
else
{
Binary = Binary.TrimStart('0');
if (Binary =="")
{
Binary = "0";
}
}
}
else if (RealRelevance == false)
{
Console.WriteLine();
Console.WriteLine("Error , you didn't entered a number at all..");
Console.WriteLine();
//
// goto jump is not very desirable but necessary
//
goto end;
}
//
// Check Binary number relevance
//
Index = 0;
Lenght = 0;
Letter = "";
Lenght = Binary.Length;
for (Index=0;Index<Lenght ;Index = Index + 1)
{
Letter = Binary.Substring(Index,1);
if (Letter != "0" && Letter != "1" && Letter !=",")
{
BinaryRelevance = false;
Index = Lenght;
}
else
{
BinaryRelevance = true;
}
}
if (BinaryRelevance != true)
{
Console.WriteLine();
Console.WriteLine("Error , you didn't enter binary number ...");
Console.WriteLine();
//
// goto jump is not very desirable but necessary
//
goto end;
}
//
// Resolving conversion from Binary to Decimal
// If it is Float number, split number to Decimal and Float part
// exmpl. 11.011(2) DecimalPart = 11 and FloatPart = 011
//
if (Binary.Contains(","))
{
DecimalFloat = Binary.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
//
// Decimal part first
//
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
D = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = DecimalPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght - Index - 1;
Letter = DecimalPart.Substring(Index,1);
Cipher = int.Parse(Letter.ToString());
BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());
D = D + (Cipher * BaseExponent);
}
//
// Float part second
//
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
F = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = FloatPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Index+1;
Letter = FloatPart.Substring(Index,1);
BaseExponent = double.Parse(Math.Pow(2,-Exponent).ToString());
Cipher = int.Parse(Letter.ToString());
F = F + (Cipher * BaseExponent);
}
RealNumber = D + F;
//
// goto jump is not very desirable but necessary
//
//
goto write;
}
//
// Resolving conversion from Binary to Decimal
// If it is Decimal number
// exmpl. 1001
//
else
{
Exponent = 0;
Cipher = 0;
RealNumber = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = Binary.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght - Index - 1;
Letter = Binary.Substring(Index,1);
BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());
Cipher = int.Parse(Letter.ToString());
RealNumber = RealNumber + (Cipher * BaseExponent);
}
}
//
// not very desirable but necessary
// write label
//
write:
//
// Write result
//
if (RealNumber == 0)
{
Negative ="";
}
Console.WriteLine();
Console.WriteLine("Binary number : " + Negative + Binary);
Console.WriteLine();
Console.WriteLine("Real number : " + Negative + RealNumber.ToString());
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;
}
}
}
}